package handler import ( "gosimplenpm/internal/config" "gosimplenpm/internal/middlewares" "gosimplenpm/internal/storage" "net/http" "time" "github.com/gorilla/mux" "github.com/sirupsen/logrus" ) type Application struct { Logger *logrus.Logger Conf config.Config Mux *mux.Router FSStorage storage.Storage } func (app *Application) Routes() { // Need to use UseEncodedPath as shown here https://github.com/gorilla/mux/blob/master/mux.go#L269 m := mux.NewRouter().StrictSlash(true).UseEncodedPath() m.Use(middlewares.LogMiddleware(app.Logger)) // main handler m.HandleFunc("/{name}", GetPackage(app.Logger, app.Conf, app.FSStorage)).Methods("GET") m.HandleFunc("/{name}", middlewares.AuthMiddleware(app.Conf)(Publish(app.Logger, app.Conf, app.FSStorage))).Methods("PUT") // tar handlers m.HandleFunc("/{name}/-/{tar}", PackageTarGet(app.Logger, app.Conf, app.FSStorage)).Methods("GET") // tag handlers m.HandleFunc("/-/package/{name}/dist-tags/{tag}", middlewares.AuthMiddleware(app.Conf)(DistTagDelete(app.Logger, app.Conf, app.FSStorage))).Methods("DELETE") m.HandleFunc("/-/package/{name}/dist-tags/{tag}", middlewares.AuthMiddleware(app.Conf)(DistTagPut(app.Logger, app.Conf, app.FSStorage))).Methods("PUT") m.HandleFunc("/-/package/{name}/dist-tags", DistTagGet(app.Logger, app.Conf, app.FSStorage)).Methods("GET") m.NotFoundHandler = http.HandlerFunc(NotFound) app.Mux = m } func (app *Application) Start() error { app.Routes() server := &http.Server{ Addr: app.Conf.IpAddress, Handler: app.Mux, ReadTimeout: 4 * time.Second, WriteTimeout: 4 * time.Second, } return server.ListenAndServe() }