2023-06-11 00:32:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-17 04:08:50 +00:00
|
|
|
"gosimplenpm/config"
|
|
|
|
"gosimplenpm/handler"
|
|
|
|
"gosimplenpm/middlewares"
|
2023-06-11 00:32:02 +00:00
|
|
|
"log"
|
2023-06-17 04:08:50 +00:00
|
|
|
"net/http"
|
2023-06-11 00:32:02 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type application struct {
|
|
|
|
logger *log.Logger
|
2023-06-17 04:08:50 +00:00
|
|
|
conf config.Config
|
2023-06-11 00:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *application) Routes() *mux.Router {
|
2023-06-17 04:08:50 +00:00
|
|
|
|
|
|
|
// 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)
|
2023-06-11 00:32:02 +00:00
|
|
|
|
|
|
|
// main handler
|
2023-06-17 04:08:50 +00:00
|
|
|
m.HandleFunc("/{name}", middlewares.AuthMiddleware(app.conf)(handler.Get)).Methods("GET")
|
|
|
|
m.HandleFunc("/{name}", middlewares.AuthMiddleware(app.conf)(handler.Publish)).Methods("PUT")
|
2023-06-11 00:32:02 +00:00
|
|
|
// tar handlers
|
|
|
|
m.HandleFunc("/{name}/-/{tar}", handler.Tar).Methods("GET")
|
|
|
|
// tag handlers
|
|
|
|
m.HandleFunc("/-/package/{name}/dist-tags/{tag}", handler.DistTagDelete).Methods("DELETE")
|
|
|
|
m.HandleFunc("/-/package/{name}/dist-tags/{tag}", handler.DistTagPut).Methods("PUT")
|
|
|
|
m.HandleFunc("/-/package/{name}/dist-tags", handler.DistTagGet).Methods("GET")
|
2023-06-17 04:08:50 +00:00
|
|
|
m.NotFoundHandler = http.HandlerFunc(handler.NotFound)
|
2023-06-11 00:32:02 +00:00
|
|
|
return m
|
|
|
|
}
|