这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@weiser
Copy link
Contributor

@weiser weiser commented Aug 18, 2020

Previously, the /docs endpoint was static, meaning that if my huma-powered app was meant to be accessed from /myapp/* the docs could not be accessed.

This PR addresses that (in a pretty hacky way) by:

  1. enabling a path to be prefixed to where the docs are mounted.
  2. templating the rapi, redoc and swagger DOMs so that we need a title and a path to the openapi.json file.

This work should invalidate #8 .

@weiser weiser mentioned this pull request Aug 18, 2020
Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments, nice work! 👍

I also think this middleware needs to be updated:
https://github.com/danielgtaylor/huma/blob/master/middleware.go#L295-L316

docs.go Outdated
// RapiDocTemplate is the template used to generate the RapiDoc. It needs two args to render:
// 1. the title
// 2. the path to the openapi.yaml file
var RapiDocTemplate = `<!doctype html>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be mutable? If not, we may want a func to return the string. Could also just be a func that renders the template, so you can take in args to describe the inputs instead of using comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, It'd be better to just export a function that would take args, instead of forcing a consumer of the module to know about the internals of a template.

I initially took the "export templates" approach because it helped me minimize the changes in all of the docs-Handlers in docs.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

functionized this: 2852753

options.go Outdated
Comment on lines 379 to 382
//
// DEPRECATED! Use `DocsDomType` instead!
func DocsHandler(f Handler) RouterOption {
fmt.Println("This option is deprecated, use `DocsDomType` instead")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be supported without deprecating it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, yes. Where it gets weird is if you use the DocsRoutePrefix RouterOption to change the prefix of the where the docs live.

E.g.

NewServer("api", "version", DocsRoutePrefix("/prefix"), DocsHandler(ReDocHandler(title))))

Since DocsHandler is just setting a function to call when /docs is hit, and ReDocHandler isn't aware of the new prefix, it felt weird to have both DocsHandlers and DocsRoutePrefixes.

A way to get around that would be to call:

NewServer("api", "version", DocsRoutePrefix("/prefix"), DocsDomType("redoc")))

which would give the user the ability to use ReDoc, while also ensuring that the docs are reachable at /prefix/docs.

Which is why I went for the deprecation route.

options.go Outdated
}

// DocsDomType sets the presentation for the docs UI. Valid values are:
// "rapi" (default), "redoc", or "swagger"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constants or a custom type for the inputs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should be constants.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constantized stuff at 1c74420

@weiser
Copy link
Contributor Author

weiser commented Aug 20, 2020

I also think this middleware needs to be updated:

@danielgtaylor I updated the addservicelinkmiddleware at: e1babc9. there's some stuff that could be DRY'd up, but that would come at the cost of changing the interface of the methods involved in the commit. I learned toward not changing the interface, but understand that it'd probably be better to modify the interface (at the risk of breaking anything that depends on that method)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants