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-06-30 19:06:46 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Application struct {
|
|
|
|
Logger *logrus.Logger
|
|
|
|
Conf config.Config
|
|
|
|
Mux *mux.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
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)).Methods("GET")
|
|
|
|
m.HandleFunc("/{name}", middlewares.AuthMiddleware(app.Conf)(Publish(app.Logger, app.Conf))).Methods("PUT")
|
|
|
|
// tar handlers
|
|
|
|
m.HandleFunc("/{name}/-/{tar}", PackageTarGet(app.Logger, app.Conf)).Methods("GET")
|
|
|
|
// tag handlers
|
|
|
|
m.HandleFunc("/-/package/{name}/dist-tags/{tag}", middlewares.AuthMiddleware(app.Conf)(DistTagDelete(app.Logger, app.Conf))).Methods("DELETE")
|
|
|
|
m.HandleFunc("/-/package/{name}/dist-tags/{tag}", middlewares.AuthMiddleware(app.Conf)(DistTagPut(app.Logger, app.Conf))).Methods("PUT")
|
|
|
|
m.HandleFunc("/-/package/{name}/dist-tags", DistTagGet(app.Logger, app.Conf)).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()
|
|
|
|
}
|