Skip to content

Commit

Permalink
Exports the default OpenAPI HTML template as a function so it can be …
Browse files Browse the repository at this point in the history
…customized

to any framework-specific handler
  • Loading branch information
EwenQuim committed Dec 13, 2024
1 parent 359fde0 commit 8964e3f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 5 additions & 5 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func Handle(s *Server, path string, controller http.Handler, options ...func(*Ba
return Register(s, Route[any, any]{
BaseRoute: BaseRoute{
Path: path,
FullName: FuncName(controller),
FullName: funcName(controller),
},
}, controller)
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func registerFuegoController[T, B any, Contexted ctx[B]](s *Server, method, path
Method: method,
Path: path,
Params: make(map[string]OpenAPIParam),
FullName: FuncName(controller),
FullName: funcName(controller),
Operation: openapi3.NewOperation(),
OpenAPI: s.OpenAPI,
}
Expand All @@ -199,7 +199,7 @@ func registerStdController(s *Server, method, path string, controller func(http.
route := BaseRoute{
Method: method,
Path: path,
FullName: FuncName(controller),
FullName: funcName(controller),
Operation: openapi3.NewOperation(),
OpenAPI: s.OpenAPI,
}
Expand All @@ -218,8 +218,8 @@ func withMiddlewares(controller http.Handler, middlewares ...func(http.Handler)
return controller
}

// FuncName returns the name of a function and the name with package path
func FuncName(f interface{}) string {
// funcName returns the name of a function and the name with package path
func funcName(f interface{}) string {
return strings.TrimSuffix(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), "-fm")
}

Expand Down
10 changes: 7 additions & 3 deletions openapi_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import (
func DefaultOpenAPIHandler(specURL string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte(`<!doctype html>
_, _ = w.Write([]byte(DefaultOpenAPIHTML(specURL)))
})
}

func DefaultOpenAPIHTML(specURL string) string {
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand All @@ -27,6 +32,5 @@ func DefaultOpenAPIHandler(specURL string) http.Handler {
tryItCredentialsPolicy="same-origin"
/>
</body>
</html>`))
})
</html>`
}

0 comments on commit 8964e3f

Please sign in to comment.