-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb500c4
commit be8c341
Showing
17 changed files
with
313 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package humaapi | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/danielgtaylor/huma/v2" | ||
) | ||
|
||
// NewDummyHumaAPI creates a new Huma API with dummy adapter to be used only for OpenAPI object generation | ||
// combined with manually added endpoints for docs, schemas and spec. | ||
func NewDummyHumaAPI(config huma.Config) huma.API { | ||
config.OpenAPIPath = "" | ||
config.DocsPath = "" | ||
config.SchemasPath = "" | ||
return huma.NewAPI(config, dummyAdapter{}) | ||
} | ||
|
||
type dummyAdapter struct{} | ||
|
||
func (d dummyAdapter) Handle(*huma.Operation, func(ctx huma.Context)) { | ||
} | ||
|
||
func (d dummyAdapter) ServeHTTP(http.ResponseWriter, *http.Request) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,47 @@ | ||
package oapi_handlers | ||
|
||
import "github.com/danielgtaylor/huma/v2" | ||
import ( | ||
"context" | ||
"embed" | ||
"html/template" | ||
|
||
func getOpenApiTitle(humaApi huma.API) string { | ||
openApi := humaApi.OpenAPI() | ||
if openApi == nil { | ||
return "" | ||
} | ||
if openApi.Info == nil { | ||
return "" | ||
} | ||
return openApi.Info.Title | ||
"github.com/danielgtaylor/huma/v2" | ||
) | ||
|
||
//go:embed docs_page.gohtml | ||
var tmplFS embed.FS | ||
|
||
type pageData struct { | ||
Title string | ||
OpenAPIYamlPath string | ||
} | ||
|
||
// GetDocsAdapterHandler returns a handler that will return HTML page that renders OpenAPI spec from the specified | ||
// GetDocsHandler returns a handler that will return HTML page that renders OpenAPI spec from the specified | ||
// `openAPIYamlPath` URL. | ||
// The handler format is suitable for passing it directly | ||
// into Adapter.Handle() method or using with huma.StreamResponse | ||
func GetDocsAdapterHandler(humaApi huma.API, openAPIYamlPath string) func(ctx huma.Context) { | ||
return func(ctx huma.Context) { | ||
ctx.SetHeader("Content-Type", "text/html") | ||
title := "Elements in HTML" | ||
|
||
if oaTitle := getOpenApiTitle(humaApi); oaTitle != "" { | ||
title = oaTitle + " Reference" | ||
} | ||
_, _ = ctx.BodyWriter().Write([]byte(`<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="referrer" content="same-origin" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | ||
<title>` + title + `</title> | ||
<!-- Embed elements Elements via Web Component --> | ||
<link href="https://unpkg.com/@stoplight/elements@8.1.0/styles.min.css" rel="stylesheet" /> | ||
<script src="https://unpkg.com/@stoplight/elements@8.1.0/web-components.min.js" | ||
integrity="sha256-985sDMZYbGa0LDS8jYmC4VbkVlh7DZ0TWejFv+raZII=" | ||
crossorigin="anonymous"></script> | ||
</head> | ||
<body style="height: 100vh;"> | ||
<elements-api | ||
apiDescriptionUrl="` + openAPIYamlPath + `" | ||
router="hash" | ||
layout="sidebar" | ||
tryItCredentialsPolicy="same-origin" | ||
/> | ||
</body> | ||
</html>`)) | ||
// The handler format is suitable for passing it to huma or hureg registration functions. | ||
func GetDocsHandler(openAPI *huma.OpenAPI, openAPIYamlPath string) StreamResponseHandler[*struct{}] { | ||
tmpl, err := template.ParseFS(tmplFS, "docs_page.gohtml") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return func(ctx context.Context, _ *struct{}) (*huma.StreamResponse, error) { | ||
pageData := getPageData(openAPI, openAPIYamlPath) | ||
|
||
return &huma.StreamResponse{ | ||
Body: func(ctx huma.Context) { | ||
ctx.SetHeader("Content-Type", "text/html") | ||
_ = tmpl.Execute(ctx.BodyWriter(), pageData) | ||
}, | ||
}, nil | ||
} | ||
} | ||
|
||
// GetDocsTypedHandler returns a handler that will return HTML page that renders OpenAPI spec from the specified | ||
// `openAPIYamlPath` URL. | ||
// The handler format is suitable for passing it to huma or hureg registration functions. | ||
func GetDocsTypedHandler(humaApi huma.API, openAPIYamlPath string) TypedStreamHandler { | ||
adapterHandler := GetDocsAdapterHandler(humaApi, openAPIYamlPath) | ||
return getTypedStreamHandler(adapterHandler) | ||
func getPageData(openAPI *huma.OpenAPI, openAPIYamlPath string) (res pageData) { | ||
res.Title = "Elements in HTML" | ||
if openAPI.Info != nil && openAPI.Info.Title != "" { | ||
res.Title = openAPI.Info.Title + " Reference" | ||
} | ||
res.OpenAPIYamlPath = openAPIYamlPath | ||
return res | ||
} |
Oops, something went wrong.