You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"context""fmt""github.com/go-chi/chi/v5""github.com/swaggest/openapi-go/openapi31""github.com/swaggest/rest/nethttp""github.com/swaggest/rest/web"
swguiv5emb "github.com/swaggest/swgui/v5emb""github.com/swaggest/usecase""log""net/http"
)
funcmain() {
service:=web.NewService(openapi31.NewReflector())
apiService:=web.NewService(openapi31.NewReflector())
// service endpoint, `/foo`service.Get("/foo", func() usecase.Interactor {
u:=usecase.NewInteractor(func(ctx context.Context, input, output*string) error {
*output="foo"returnnil
})
u.SetTitle("Foo")
u.SetTags("Service")
returnu
}())
// apiService endpoint, `/api/bar` cause will be mounted onto service with `/api` laterapiService.Group(func(router chi.Router) {
router.Method(http.MethodGet, "/bar", nethttp.NewHandler(func() usecase.Interactor {
u:=usecase.NewInteractor(func(ctx context.Context, input, output*string) error {
*output="bar"returnnil
})
u.SetTitle("Bar")
u.SetTags("ApiService")
returnu
}()))
})
apiService.Route("/api", func(router chi.Router) {})
// mount apiService onto service, so we can expect all routes in apiService will prepend with `/api`service.Mount("/api", apiService)
// prepare the docspattern:="/docs"apiPattern:="/api/docs"apiService.Method(http.MethodGet, pattern+"/openapi.json", apiService.OpenAPICollector)
// prepare swgui in `/api/docs`// use `pattern` instead of `apiPattern` because apiService will be mounted under `/api`,// for some reason, swaggerJSONPath and basePath must prepend with `/api` else swgui will not workapiService.Mount(pattern, swguiv5emb.NewHandler(apiService.OpenAPISchema().Title(), apiPattern+"/openapi.json", apiPattern))
// prepare the docsadminPattern:="/admin/docs"service.Method(http.MethodGet, adminPattern+"/openapi.json", service.OpenAPICollector)
service.Mount(adminPattern, swguiv5emb.NewHandler(service.OpenAPISchema().Title(), adminPattern+"/openapi.json", adminPattern))
// servefmt.Println("Swagger UI at http://localhost:8888/admin/docs and http://localhost:8888/api/docs.")
iferr:=http.ListenAndServe("localhost:8888", service); err!=nil {
log.Fatal(err)
}
}
Expected:
the routes in /api/docs should prefix with /api
Actual:
when view document at /api/docs, the api listed do not contain /api prefix as it being mounted with pattern /api
I am looking for a solution for this.
Its either a way to make the Collector to have a prefix so I can set it manually
Or a way to mount *web.Service without a pattern, so I can use apiService.Route("/api", func (router chi.Router) {}) to wrap the API
Or any suggestion to make this happened?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I am aware of example of https://github.com/swaggest/rest/blob/master/_examples/mount/main.go and try to replicate it, but instead of having APIs in 1 doc, I wanted to separate into multiple docs.
Here's my code
Expected:
the routes in
/api/docs
should prefix with/api
Actual:
when view document at /api/docs, the api listed do not contain
/api
prefix as it being mounted with pattern/api
I am looking for a solution for this.
Its either a way to make the Collector to have a prefix so I can set it manually
Or a way to mount *web.Service without a pattern, so I can use
apiService.Route("/api", func (router chi.Router) {})
to wrap the APIOr any suggestion to make this happened?
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions