Skip to content

[REQ] [go-server] Separate utility code from Router and update chi logger middleware to use logger.go file #20817

@jarangutan

Description

@jarangutan

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

Currently, the router file has both the router and a bunch of utility code not used by the router itself within the same file. This makes it so that if a user wants to modify the router to add more middlewares then they lose out on updates to the utility code if they add it as part of their ignores to make the modification.

Additionally, go-server expose a logger.go file for both the chi/mux routers but only the mux router is actually using that logger.go file. The chi router hard codes the default logger middleware which does not allow the user to update the logger middleware without taking over the file. This leaves the user trying to use the chi router with an unused logger.go file and unable to add their own logger middleware.

Describe the solution you'd like

In the go-server template, separate the utility code starting from line 91 in the routers.moustache file into its own utility.moustache file. This should allow folks if they want to take control of the router to add more middlewares not supported without having to take control of the utility code.

In the go-server template, update the chi logger middleware line in routers.moustache to be router.Use(Logger) which would be the exported logger from the logger file

Then update the logger.go file to add template logic to return the chi middleware logger so the user has a chance to change it if they feel like it. Example of what that might look like:

{#mux}}
func Logger(inner http.Handler, name string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()

		inner.ServeHTTP(w, r)

		log.Printf(
			"%s %s %s %s",
			r.Method,
			r.RequestURI,
			name,
			time.Since(start),
		)
	})
}
{{/mux}}
{{#chi}}
func Logger(inner http.Handler) http.Handler {
	return middleware.Logger(inner)
}
{{/chi}}

Describe alternatives you've considered

Using a user defined template for my team: https://openapi-generator.tech/docs/customization

I figured I might not be the only one who would want this so thought I'd contribute back :>

Additional context

love the tool <3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions