Skip to content

Conversation

@halon176
Copy link

@halon176 halon176 commented May 7, 2024

Describe the PR
I added an example that allows generating and displaying Swagger documentation using http.NewServeMux

Relation issue
e.g. https://github.com/swaggo/gin-swagger/pull/123/files

Additional context
The Go version must be 1.22 or higher to be able to use all the new features.

func main() {
r := http.NewServeMux()

r.Handle("GET /swagger/", httpSwagger.Handler(
Copy link
Contributor

@ubogdan ubogdan Oct 21, 2024

Choose a reason for hiding this comment

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

Did you test the r.Handle route ? Looks to me like invalid according to the documentation

import (
	"fmt"
	"net/http"
)

type apiHandler struct{}

func (apiHandler) ServeHTTP(http.ResponseWriter, *http.Request) {}

func main() {
	mux := http.NewServeMux()
	mux.Handle("/api/", apiHandler{})
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		// The "/" pattern matches everything, so we need to check
		// that we're at the root here.
		if req.URL.Path != "/" {
			http.NotFound(w, req)
			return
		}
		fmt.Fprintf(w, "Welcome to the home page!")
	})
}

@ubogdan
Copy link
Contributor

ubogdan commented Oct 21, 2024

I want this example to work with (1.19 or newer).
What new features are you talking about?

@BasitAli
Copy link

I want this example to work with (1.19 or newer). What new features are you talking about?

These are the new routing enhancements launched announce with Go 1.22 in February 2024. I think it's a good idea to include this as an example in the repo.

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.

3 participants