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

How to serve my UI files? #1375

Closed
shyr opened this Issue Jan 12, 2018 · 7 comments

Comments

Projects
None yet
5 participants
@shyr
Copy link

shyr commented Jan 12, 2018

Problem statement

I generated server code using go-swagger with my swagger.yaml file like below.

$ swagger generate server --exclude-main -A myapp -t gen -f ./swagger.yaml

And I want to add new handler to serve my own UI files.
In this case, is middleware only solution to serve UI files? Or can I add new handler to serve files without middleware?

// Handler example which I want to add to swagger server
func pastaWorkspacePage(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "./static/workspace.html")
}

Environment

swagger version: 0.13.0
go version: 1.9.2
OS: MacOS 17.3.0 Darwin

@shyr

This comment has been minimized.

Copy link

shyr commented Jan 12, 2018

Anyway, I solved the problem using middleware.

func FileServerMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if strings.HasPrefix(r.URL.Path, "/api") {
			next.ServeHTTP(w, r)
		} else {
			http.FileServer(http.Dir("./static")).ServeHTTP(w, r)
		}
	})
}

But I'm not sure it a right and best solution.

@fredbi fredbi added the question label Jan 12, 2018

@macrael

This comment has been minimized.

Copy link

macrael commented Jan 19, 2018

I am also curious about best practices for hosting endpoints other than the API with this framework. For instance, we'd like to host the swagger UI and also our site's front end javascript, somewhere.

@casualjim

This comment has been minimized.

Copy link
Member

casualjim commented Jan 19, 2018

you can use golangs static content middleware to serve static files like it was described in the comment above

@casualjim

This comment has been minimized.

Copy link
Member

casualjim commented Jan 19, 2018

more info on using middlewares is found here: https://goswagger.io/use/middleware.html

That page also contains a link to a good explanation on how to create net/http middlewares

@keith6014

This comment has been minimized.

Copy link

keith6014 commented Mar 16, 2018

i too am curious. what is the best way to achieve this?

@fredbi

This comment has been minimized.

Copy link
Contributor

fredbi commented Mar 16, 2018

Take a look at cmd/swagger/commands/serve.go

@casualjim

This comment has been minimized.

Copy link
Member

casualjim commented Mar 17, 2018

Every swagger generated server comes with the redoc UI baked in at /{basepath}/docs

fredbi added a commit to fredbi/go-swagger that referenced this issue Mar 25, 2018

FAQ update
Updated FAQ in docs, with content gathered from "question" issues.

Corresponding issues may be now closed:
- fixes go-swagger#997
- fixes go-swagger#1334
- fixes go-swagger#1029
- fixes go-swagger#1375

fredbi added a commit to fredbi/go-swagger that referenced this issue Mar 25, 2018

FAQ update
Updated FAQ in docs, with content gathered from "question" issues.

Corresponding issues may be now closed:
- fixes go-swagger#997
- fixes go-swagger#1334
- fixes go-swagger#1029
- fixes go-swagger#1375

@fredbi fredbi referenced this issue Mar 25, 2018

Merged

FAQ update #1460

fredbi added a commit to fredbi/go-swagger that referenced this issue Mar 25, 2018

FAQ update
Updated FAQ in docs, with content gathered from "question" issues.

Corresponding issues may be now closed:
- fixes go-swagger#997
- fixes go-swagger#1334
- fixes go-swagger#1029
- fixes go-swagger#1375

@1ambda 1ambda referenced this issue May 29, 2018

Open

Project Setup: Development Tools #26

4 of 7 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment