ardanlabs / service

Starter-kit for writing services in Go using Kubernetes.
https://www.ardanlabs.com
Apache License 2.0
3.6k stars 651 forks source link

AutoClose for file system #120

Closed ghost closed 4 years ago

ghost commented 4 years ago

If I add the fileSystem with http.Handle in the routes, is it autoClosed when shutdown the API?

// routes.go

func API(shutdown chan os.Signal, log *log.Logger, db *sqlx.DB, authenticator *auth.Authenticator) http.Handler {

    app := web.NewApp(shutdown, mid.Logger(log), mid.Errors(log), mid.Metrics(), mid.Panics(log))

    check := Check{
        db: db,
    }
    app.Handle("GET", "/v1/health", check.Health)
        ...

    http.Handle("/files", http.FileServer(http.Dir(uploadPath)))  // This line

    return app
}
ardan-bkennedy commented 4 years ago

I am only load shedding the app side of things. My guess is http.Handle is adding a route to the default router (http.DefaultServerMux). If you want load shedding, you want to bind that to app.Handle. Assuming you want the /files route on port 80, you will really need a new http.Server bound to that port. Look at main.go:205 for more details.

ghost commented 4 years ago

When I check web.go and see the Handler only for api format, is that possible to add support for fileSystem?

// A type that handles an http request within our own little mini framework.
type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error

// A type that serve static files within our own little mini framework.
type Handler func(ctx context.Context, path string, root http.FileSystem) error
ardan-bkennedy commented 4 years ago

I apologize for not being clear in my last message :(

Look at this issue and someone has provided a link where they are using the file system. https://github.com/ardanlabs/service/issues/113

ghost commented 4 years ago

Yes, it's nice example for a new http.Handler create and all I need in this project. But later I think it will be better to wrap the Handler to support static files because of httptreemux (or httprouter) also do it.

ardan-bkennedy commented 4 years ago

You an look here for what you were requesting.

https://github.com/dgraph-io/travel/blob/master/cmd/travel-ui/internal/handlers/routes.go