gosimplenpm/internal/handler/app.go

51 lines
1.6 KiB
Go
Raw Normal View History

2023-06-30 19:06:46 +00:00
package handler
import (
2023-11-25 02:17:09 +00:00
"gosimplenpm/internal/config"
"gosimplenpm/internal/middlewares"
2023-12-25 08:56:48 +00:00
"gosimplenpm/internal/storage"
2023-06-30 19:06:46 +00:00
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
type Application struct {
2023-12-25 08:56:48 +00:00
Logger *logrus.Logger
Conf config.Config
Mux *mux.Router
FSStorage storage.Storage
2023-06-30 19:06:46 +00:00
}
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
2023-12-25 08:56:48 +00:00
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")
2023-06-30 19:06:46 +00:00
// tar handlers
2023-12-25 08:56:48 +00:00
m.HandleFunc("/{name}/-/{tar}", PackageTarGet(app.Logger, app.Conf, app.FSStorage)).Methods("GET")
2023-06-30 19:06:46 +00:00
// tag handlers
2023-12-25 08:56:48 +00:00
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")
2023-06-30 19:06:46 +00:00
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()
}