Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subfolder OpenAPI UI #80

Closed
Shackelford-Arden opened this issue Mar 20, 2024 · 3 comments
Closed

Subfolder OpenAPI UI #80

Shackelford-Arden opened this issue Mar 20, 2024 · 3 comments
Labels
feature-request New feature or request
Milestone

Comments

@Shackelford-Arden
Copy link

Is your feature request related to a problem? Please describe.

When I run my application behind a proxy, in lower environments, I access the API via a subfolder route. In this case, there is a proxy/router in between me and the application.

Example Config

...

  s := fuego.NewServer(
      fuego.WithPort(":8888"),
      fuego.WithOpenapiConfig(fuego.OpenapiConfig{
          DisableSwagger:   false,
	  DisableLocalSave: true,
	  SwaggerUrl:       "/api/docs",
	  JsonUrl:          "/api/docs/openapi.json",
    }),
  )

...

Running this locally, works as expected since there is not a load balancer/proxy fronting the requests.

Using the same config behind the proxy, I'd make open the docs at the following URL.

http://<load-balancer/proxy.fqdn>/fancy-app/api/docs

When I do this, seems like the UI (at index.html) redirects the browser to the following:

http://<load-balancer/proxy.fqdn>/api/docs

Obviously, this ends w/ a 404 since the load-balancer/proxy is not aware of the /api/docs route.

Describe the solution you'd like

I'd like to be able to have the API docs load when behind the load-balancer/proxy without much fuss :)

Describe alternatives you've considered

Looking around a little bit, it seems that there is a fuego.WithBasePath() func, but setting that must be setting something else as it doesn't appear to be honored in this context.

Seems like the fuego.WithBasePath() doesn't inject it's value into the index.html template to the value of basePath - See docs

Might be helpful to allow passing that value in and maybe even choosing a different router type (in this case, history may be helpful)?

Additional context

N/A

@Shackelford-Arden Shackelford-Arden added the feature-request New feature or request label Mar 20, 2024
@EwenQuim
Copy link
Member

Interesting issue, I'll see what I can do

@EwenQuim EwenQuim moved this to Todo in Roadmap Fuego 🔥 Mar 21, 2024
@EwenQuim EwenQuim added this to the v0.14 milestone Mar 22, 2024
@Shackelford-Arden
Copy link
Author

Ok, I dug in a bit more, and I believe this is, at the end of the day, a limitation of Stoplight itself, not Fuego :) I can mostly get it to work by adjusting the basePath in Stoplight, but it still attempts to re-write the URL, which is what's causing the "pain" :) I opened an issue w/ Stoplight upstream.

Just for sharing what I'd done, here is the custom UIHandler I had to get it to where it semi-works today:

func StoplightDocs(specURL string) http.Handler {
	uiPath := BaseUIPath
	if BaseUIPath == "" {
		uiPath = "/"
	}
	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>
<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>API Documentation</title>
	<script src="https://unpkg.com/@stoplight/elements/web-components.min.js"></script>
	<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements/styles.min.css" />
</head>
<body style="height: 100vh;">
	<elements-api
		basePath="` + uiPath + `"
		apiDescriptionUrl="` + BaseUIPath + specURL + `"
		layout="responsive"
		router="history"
		logo="https://go-fuego.github.io/fuego/img/logo.svg"
		tryItCredentialsPolicy="same-origin"
	/>
</body>
</html>`))
	})
}

Using the above, the docs load behind the sub-folder, but the URL gets re-written, so subsequent updates to the page/URL end up breaking.

I access:

http://<load-balancer/proxy.fqdn>/fancy-app/api/docs

Browser's URL gets updated to be:

http://<load-balancer/proxy.fqdn>/api/docs

All that said, I'll close this one out in favor of working w/ Stoplight folks upstream :) Until then, I now better understand how I can get some other API docs up for now.

@EwenQuim
Copy link
Member

Nice catch!

Happy to see that the possibility of using a custom UIHandler is really useful for some use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
Development

No branches or pull requests

2 participants