Skip to content

added example with http.NewServeMux #116

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants