2020-10-07 04:11:36 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-02-28 05:04:05 +00:00
|
|
|
"io"
|
2020-10-07 04:11:36 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
"go.jolheiser.com/gpm/database"
|
|
|
|
"go.jolheiser.com/gpm/go-gpm"
|
2020-10-07 04:11:36 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
"go.jolheiser.com/beaver"
|
|
|
|
)
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
var Version = "develop"
|
2020-10-07 04:11:36 +00:00
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
func New(token string, db *database.Database) *chi.Mux {
|
2020-10-07 04:11:36 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(middleware.RedirectSlashes)
|
|
|
|
r.Use(middleware.Recoverer)
|
|
|
|
r.Use(middleware.Timeout(30 * time.Second))
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
r.Get("/", handleHome(db))
|
|
|
|
r.Post("/", addUpdatePackage(db, token))
|
|
|
|
r.Patch("/", addUpdatePackage(db, token))
|
|
|
|
r.Delete("/", removePackage(db, token))
|
|
|
|
r.Get("/{name}", getPackage(db))
|
2020-10-07 04:11:36 +00:00
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
func handleHome(db *database.Database) func(res http.ResponseWriter, _ *http.Request) {
|
2020-10-07 04:11:36 +00:00
|
|
|
return func(res http.ResponseWriter, _ *http.Request) {
|
2021-02-28 05:04:05 +00:00
|
|
|
pkgs, err := db.Packages()
|
|
|
|
if err != nil {
|
|
|
|
beaver.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := json.Marshal(gpm.Info{
|
|
|
|
Version: Version,
|
|
|
|
NumPackages: len(pkgs),
|
|
|
|
Packages: pkgs,
|
2020-10-07 04:11:36 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
res.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = res.Write([]byte("{}"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = res.Write(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
func getPackage(db *database.Database) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
name := chi.URLParam(req, "name")
|
|
|
|
|
|
|
|
pkg, err := db.PackageJSON(name)
|
2020-10-07 04:11:36 +00:00
|
|
|
if err != nil {
|
2021-02-28 05:04:05 +00:00
|
|
|
res.WriteHeader(http.StatusNotFound)
|
|
|
|
_, _ = res.Write([]byte("{}"))
|
2020-10-07 04:11:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
_, _ = res.Write(pkg)
|
2020-10-07 04:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
func addUpdatePackage(db *database.Database, token string) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Header.Get(gpm.TokenHeader) != token {
|
|
|
|
res.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := io.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
res.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
2020-10-07 04:11:36 +00:00
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
var pkg gpm.Package
|
|
|
|
if err := json.Unmarshal(data, &pkg); err != nil {
|
|
|
|
res.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := db.PackageJSON(pkg.Name)
|
2020-10-07 04:11:36 +00:00
|
|
|
if err != nil {
|
|
|
|
res.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-02-28 05:04:05 +00:00
|
|
|
switch req.Method {
|
|
|
|
case http.MethodPost:
|
|
|
|
if exists != nil {
|
|
|
|
res.WriteHeader(http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case http.MethodPatch:
|
|
|
|
if exists == nil {
|
|
|
|
res.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.PutPackage(pkg); err != nil {
|
|
|
|
res.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Method {
|
|
|
|
case http.MethodPost:
|
|
|
|
res.WriteHeader(http.StatusCreated)
|
|
|
|
case http.MethodPatch:
|
|
|
|
res.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2020-10-07 04:11:36 +00:00
|
|
|
}
|
2021-02-28 05:04:05 +00:00
|
|
|
}
|
2020-10-07 04:11:36 +00:00
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
func removePackage(db *database.Database, token string) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Header.Get(gpm.TokenHeader) != token {
|
|
|
|
res.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := io.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
res.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
|
|
|
|
var pkg gpm.Package
|
|
|
|
if err := json.Unmarshal(data, &pkg); err != nil {
|
|
|
|
res.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.RemovePackage(pkg.Name); err != nil {
|
|
|
|
res.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2020-10-07 04:11:36 +00:00
|
|
|
}
|