mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat: open API v1 and postman collection
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/apikeys"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/database"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
db := database.Connect(config.DBHost, config.DBName)
|
||||
service := apikeys.NewService(db)
|
||||
ctx := context.Background()
|
||||
|
||||
switch os.Args[1] {
|
||||
case "create":
|
||||
fs := flag.NewFlagSet("create", flag.ExitOnError)
|
||||
name := fs.String("name", "dashboard-read-api", "integration client name")
|
||||
environment := fs.String("env", config.AppEnv, "environment label")
|
||||
permissions := fs.String("permissions", "", "comma separated permission codes")
|
||||
allArea := fs.Bool("all-area", true, "grant all areas")
|
||||
areaIDs := fs.String("area-ids", "", "comma separated area ids")
|
||||
allLocation := fs.Bool("all-location", true, "grant all locations")
|
||||
locationIDs := fs.String("location-ids", "", "comma separated location ids")
|
||||
fs.Parse(os.Args[2:])
|
||||
|
||||
permissionCodes := apikeys.DefaultDashboardPermissions()
|
||||
if strings.TrimSpace(*permissions) != "" {
|
||||
permissionCodes = splitCSV(*permissions)
|
||||
}
|
||||
|
||||
issued, err := service.Create(ctx, apikeys.CreateInput{
|
||||
Name: *name,
|
||||
Environment: *environment,
|
||||
PermissionCodes: permissionCodes,
|
||||
AllArea: *allArea,
|
||||
AreaIDs: parseUintCSV(*areaIDs),
|
||||
AllLocation: *allLocation,
|
||||
LocationIDs: parseUintCSV(*locationIDs),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("name: %s\n", issued.Record.Name)
|
||||
fmt.Printf("environment: %s\n", issued.Record.Environment)
|
||||
fmt.Printf("prefix: %s\n", issued.Record.KeyPrefix)
|
||||
fmt.Printf("status: %s\n", issued.Record.Status)
|
||||
fmt.Printf("api_key: %s\n", issued.Key)
|
||||
case "list":
|
||||
fs := flag.NewFlagSet("list", flag.ExitOnError)
|
||||
environment := fs.String("env", "", "filter by environment")
|
||||
fs.Parse(os.Args[2:])
|
||||
|
||||
records, err := service.List(ctx, *environment)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, record := range records {
|
||||
fmt.Printf("%s\t%s\t%s\t%s\tareas=%t\tlocations=%t\n",
|
||||
record.Environment,
|
||||
record.KeyPrefix,
|
||||
record.Status,
|
||||
record.Name,
|
||||
record.AllArea,
|
||||
record.AllLocation,
|
||||
)
|
||||
}
|
||||
case "revoke":
|
||||
fs := flag.NewFlagSet("revoke", flag.ExitOnError)
|
||||
environment := fs.String("env", config.AppEnv, "environment label")
|
||||
prefix := fs.String("prefix", "", "key prefix to revoke")
|
||||
fs.Parse(os.Args[2:])
|
||||
|
||||
if err := service.Revoke(ctx, *environment, *prefix); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("revoked %s/%s\n", *environment, *prefix)
|
||||
default:
|
||||
usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Println("usage:")
|
||||
fmt.Println(" go run ./cmd/api-key create [flags]")
|
||||
fmt.Println(" go run ./cmd/api-key list [flags]")
|
||||
fmt.Println(" go run ./cmd/api-key revoke -env <environment> -prefix <prefix>")
|
||||
}
|
||||
|
||||
func splitCSV(raw string) []string {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(raw, ",")
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
part = strings.TrimSpace(part)
|
||||
if part != "" {
|
||||
out = append(out, part)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseUintCSV(raw string) []uint {
|
||||
parts := splitCSV(raw)
|
||||
if len(parts) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
values := make([]uint, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
var value uint
|
||||
if _, err := fmt.Sscanf(part, "%d", &value); err == nil && value > 0 {
|
||||
values = append(values, value)
|
||||
}
|
||||
}
|
||||
return values
|
||||
}
|
||||
@@ -9,12 +9,14 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/apikeys"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/cache"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/database"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/sso/session"
|
||||
sso "gitlab.com/mbugroup/lti-api.git/internal/modules/sso/verifier"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/readapi"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/route"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
@@ -131,6 +133,7 @@ func setupDatabase() *gorm.DB {
|
||||
}
|
||||
|
||||
func setupRoutes(app *fiber.App, db *gorm.DB, rdb *redis.Client) {
|
||||
middleware.SetAPIKeyAuthenticator(apikeys.NewService(db))
|
||||
|
||||
// route.Routes(app, db)
|
||||
// app.Use(utils.NotFoundHandler)
|
||||
@@ -169,6 +172,8 @@ func setupRoutes(app *fiber.App, db *gorm.DB, rdb *redis.Client) {
|
||||
return c.Status(status).JSON(body)
|
||||
})
|
||||
|
||||
readAPIRoutes := app.Group("/api")
|
||||
readapi.RegisterRoutes(readAPIRoutes)
|
||||
route.Routes(app, db)
|
||||
app.Use(utils.NotFoundHandler)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/cache"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/readapi"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/route"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func main() {
|
||||
root, err := findRepoRoot()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
readapi.PrimeBuildConfig()
|
||||
cache.SetRedis(redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"}))
|
||||
app := fiber.New(config.FiberConfig())
|
||||
app.Get("/healthz", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"status": "ok", "service": "api", "version": config.Version})
|
||||
})
|
||||
app.Get("/readyz", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"status": "ok", "db": "up", "redis": "up"})
|
||||
})
|
||||
route.Routes(app, nil)
|
||||
|
||||
artifacts, err := readapi.BuildArtifactsFromApp(app)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
files := map[string][]byte{
|
||||
filepath.Join(root, "docs", "openapi", "read-api.json"): artifacts.OpenAPIJSON,
|
||||
filepath.Join(root, "docs", "openapi", "read-api.yaml"): artifacts.OpenAPIYAML,
|
||||
filepath.Join(root, "docs", "postman", "read-api.collection.json"): artifacts.PostmanCollectionJSON,
|
||||
filepath.Join(root, "docs", "postman", "read-api.environment.json"): artifacts.PostmanEnvironmentJSON,
|
||||
}
|
||||
|
||||
for path, body := range files {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := os.WriteFile(path, body, 0o644); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("wrote %s\n", path)
|
||||
}
|
||||
}
|
||||
|
||||
func findRepoRoot() (string, error) {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
current := wd
|
||||
for {
|
||||
if _, err := os.Stat(filepath.Join(current, "go.mod")); err == nil {
|
||||
return current, nil
|
||||
}
|
||||
parent := filepath.Dir(current)
|
||||
if parent == current {
|
||||
return "", fmt.Errorf("go.mod not found from %s", wd)
|
||||
}
|
||||
current = parent
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user