From 24694ecbae967ec75794db04b4c98a0680ce3fce Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 17 Feb 2024 03:05:22 -0500 Subject: [PATCH] feat: create new swagger package --- api/s5/embed/.gitkeep | 0 api/s5/s5.go | 61 +++++-------------------------- api/{s5 => swagger}/generate.go | 6 --- api/swagger/swagger.go | 65 +++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 58 deletions(-) delete mode 100644 api/s5/embed/.gitkeep rename api/{s5 => swagger}/generate.go (95%) create mode 100644 api/swagger/swagger.go diff --git a/api/s5/embed/.gitkeep b/api/s5/embed/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/api/s5/s5.go b/api/s5/s5.go index b62f0a2..7974580 100644 --- a/api/s5/s5.go +++ b/api/s5/s5.go @@ -5,14 +5,12 @@ import ( "context" "crypto/ed25519" "crypto/rand" - "embed" _ "embed" "encoding/base64" "encoding/hex" "errors" "fmt" "io" - "io/fs" "math" "mime/multipart" "net/http" @@ -21,12 +19,12 @@ import ( "strings" "time" + "git.lumeweb.com/LumeWeb/portal/api/swagger" + "git.lumeweb.com/LumeWeb/portal/metadata" "git.lumeweb.com/LumeWeb/portal/storage" - "github.com/getkin/kin-openapi/openapi3" - "git.lumeweb.com/LumeWeb/libs5-go/encoding" s5libmetadata "git.lumeweb.com/LumeWeb/libs5-go/metadata" "git.lumeweb.com/LumeWeb/libs5-go/node" @@ -60,12 +58,7 @@ var ( ) //go:embed swagger.yaml -var spec []byte - -//go:generate go run generate.go - -//go:embed embed -var swagfs embed.FS +var swagSpec []byte type S5API struct { config *viper.Viper @@ -143,7 +136,7 @@ func (s S5API) Stop(ctx context.Context) error { return nil } -func (s *S5API) Routes() *httprouter.Router { +func (s *S5API) Routes() (*httprouter.Router, error) { authMiddlewareOpts := middleware.AuthMiddlewareOptions{ Identity: s.identity, Accounts: s.accounts, @@ -161,38 +154,8 @@ func (s *S5API) Routes() *httprouter.Router { tusCors := BuildTusCors() - loader := openapi3.NewLoader() - doc, err := loader.LoadFromData(spec) - if err != nil { - panic(err) - } - - if err = doc.Validate(loader.Context); err != nil { - panic(err) - } - - jsonDoc, err := doc.MarshalJSON() - - if err != nil { - panic(err) - } - wrappedTusHandler := middleware.ApplyMiddlewares(tusOptionsHandler, tusCors, authMw) - swaggerFiles, _ := fs.Sub(swagfs, "embed") - swaggerServ := http.FileServer(http.FS(swaggerFiles)) - swaggerHandler := func(c jape.Context) { - swaggerServ.ServeHTTP(c.ResponseWriter, c.Request) - } - - swaggerStrip := func(next http.Handler) http.Handler { - return http.StripPrefix("/swagger", next) - } - - swaggerRedirect := func(jc jape.Context) { - http.Redirect(jc.ResponseWriter, jc.Request, "/swagger/", http.StatusMovedPermanently) - } - routes := map[string]jape.Handler{ // Account API "GET /s5/account/register": s.accountRegisterChallenge, @@ -232,20 +195,14 @@ func (s *S5API) Routes() *httprouter.Router { "GET /s5/registry": middleware.ApplyMiddlewares(s.registryQuery, authMw), "POST /s5/registry": middleware.ApplyMiddlewares(s.registrySet, authMw), "GET /s5/registry/subscription": middleware.ApplyMiddlewares(s.registrySubscription, authMw), - - "GET /swagger.json": byteHandler(jsonDoc), - "GET /swagger": swaggerRedirect, - "GET /swagger/*path": middleware.ApplyMiddlewares(swaggerHandler, swaggerStrip), } - return s.protocol.Node().Services().HTTP().GetHttpRouter(routes) -} - -func byteHandler(b []byte) jape.Handler { - return func(c jape.Context) { - c.ResponseWriter.Header().Set("Content-Type", "application/json") - c.ResponseWriter.Write(b) + routes, err := swagger.Swagger(swagSpec, routes) + if err != nil { + return nil, err } + + return s.protocol.Node().Services().HTTP().GetHttpRouter(routes), nil } type s5TusJwtResponseWriter struct { diff --git a/api/s5/generate.go b/api/swagger/generate.go similarity index 95% rename from api/s5/generate.go rename to api/swagger/generate.go index 0756a62..10f8e5d 100644 --- a/api/s5/generate.go +++ b/api/swagger/generate.go @@ -108,10 +108,4 @@ func main() { if _, err := newinitFile.Write(newInit); err != nil { log.Fatalf("unable to write to swagger-initializer.js: %v", err) } - newcv, err := os.Create("current_version.txt") - if err != nil { - log.Fatalf("can't update current_version.txt: %v", err) - } - defer newcv.Close() - newcv.WriteString(tag) } diff --git a/api/swagger/swagger.go b/api/swagger/swagger.go new file mode 100644 index 0000000..1303787 --- /dev/null +++ b/api/swagger/swagger.go @@ -0,0 +1,65 @@ +package swagger + +import ( + "embed" + "io/fs" + "net/http" + + "git.lumeweb.com/LumeWeb/portal/api/middleware" + + "go.sia.tech/jape" + + "github.com/getkin/kin-openapi/openapi3" +) + +//go:generate go run generate.go + +//go:embed embed +var swagfs embed.FS + +func byteHandler(b []byte) jape.Handler { + return func(c jape.Context) { + c.ResponseWriter.Header().Set("Content-Type", "application/json") + c.ResponseWriter.Write(b) + } +} + +func Swagger(spec []byte, routes map[string]jape.Handler) (map[string]jape.Handler, error) { + loader := openapi3.NewLoader() + doc, err := loader.LoadFromData(spec) + if err != nil { + return nil, err + } + + if err = doc.Validate(loader.Context); err != nil { + return nil, err + } + + jsonDoc, err := doc.MarshalJSON() + + if err != nil { + return nil, err + } + + swaggerFiles, _ := fs.Sub(swagfs, "embed") + swaggerServ := http.FileServer(http.FS(swaggerFiles)) + handler := func(c jape.Context) { + swaggerServ.ServeHTTP(c.ResponseWriter, c.Request) + } + + strip := func(next http.Handler) http.Handler { + return http.StripPrefix("/swagger", next) + } + + redirect := func(jc jape.Context) { + http.Redirect(jc.ResponseWriter, jc.Request, "/swagger/", http.StatusMovedPermanently) + } + + swagRoutes := map[string]jape.Handler{ + "GET /swagger.json": byteHandler(jsonDoc), + "GET /swagger": redirect, + "GET /swagger/*path": middleware.ApplyMiddlewares(handler, strip), + } + + return middleware.MergeRoutes(routes, swagRoutes), nil +}