mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2026-11-26 12:01:12 +00:00
Move away from dbmate, use PolySchema
This commit is contained in:
1 parent
52399c42f1
commit
2a14f3f84c
99 files changed
+1583
-1691
No files matched your search
@@ -2,32 +2,17 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/entity/accesslist"
|
||||
)
|
||||
|
||||
// GetAccessLists will return a list of Access Lists
|
||||
// Route: GET /access-lists
|
||||
func GetAccessLists() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
pageInfo, err := getPageInfoFromRequest(r)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
items, err := accesslist.List(pageInfo, middleware.GetFiltersFromContext(r))
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, items)
|
||||
}
|
||||
}
|
||||
return listHandler(accesslist.List)
|
||||
}
|
||||
|
||||
// GetAccessList will return a single access list
|
||||
@@ -54,25 +39,7 @@ func GetAccessList() func(http.ResponseWriter, *http.Request) {
|
||||
// Route: POST /access-lists
|
||||
func CreateAccessList() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
|
||||
var newItem accesslist.Model
|
||||
err := json.Unmarshal(bodyBytes, &newItem)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Get userID from token
|
||||
userID, _ := r.Context().Value(c.UserIDCtxKey).(uint)
|
||||
newItem.UserID = userID
|
||||
|
||||
if err = newItem.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save Access List: %s", err.Error()), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, newItem)
|
||||
createUserOwned(w, r, &accesslist.Model{}, "Access List")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -231,7 +231,7 @@ func RefreshToken() func(http.ResponseWriter, *http.Request) {
|
||||
// Route: POST /auth/sse
|
||||
func NewSSEToken() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
userID := r.Context().Value(c.UserIDCtxKey).(uint)
|
||||
userID, _ := r.Context().Value(c.UserIDCtxKey).(uint)
|
||||
|
||||
// Find user
|
||||
userObj, userErr := user.GetByID(userID)
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/entity"
|
||||
"npm/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// userOwned is an entity that belongs to the user that created it
|
||||
type userOwned interface {
|
||||
SetUserID(userID uint)
|
||||
Save() error
|
||||
}
|
||||
|
||||
// createUserOwned decodes the request body into item, assigns it to the
|
||||
// requesting user and saves it. label is used in the error message.
|
||||
func createUserOwned(w http.ResponseWriter, r *http.Request, item userOwned, label string) {
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
if err := json.Unmarshal(bodyBytes, item); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Get userID from token
|
||||
userID, _ := r.Context().Value(c.UserIDCtxKey).(uint)
|
||||
item.SetUserID(userID)
|
||||
|
||||
if err := item.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save %s: %s", label, err.Error()), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
}
|
||||
|
||||
// getNginxConfig returns a handler that loads an entity by the ID in the
|
||||
// paramName url param and responds with its nginx config from disk, as
|
||||
// plain text when format is "text" or JSON otherwise
|
||||
func getNginxConfig[T any](
|
||||
format string,
|
||||
paramName string,
|
||||
getByID func(uint) (T, error),
|
||||
getContent func(T) (string, error),
|
||||
) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := getURLParamInt(r, paramName)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := getByID(id)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
// Get the config from disk
|
||||
content, nErr := getContent(item)
|
||||
if nErr != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, nErr.Error(), nil)
|
||||
return
|
||||
}
|
||||
if format == "text" {
|
||||
h.ResultResponseText(w, http.StatusOK, content)
|
||||
return
|
||||
}
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, content)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getByIDHandler returns a handler that responds with the entity loaded by
|
||||
// the ID in the paramName url param
|
||||
func getByIDHandler[T any](paramName string, getByID func(uint) (T, error)) func(http.ResponseWriter, *http.Request) {
|
||||
return withEntity(paramName, getByID, func(w http.ResponseWriter, r *http.Request, item *T) {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
})
|
||||
}
|
||||
|
||||
// updateByIDHandler returns a handler that loads the entity by the ID in the
|
||||
// paramName url param, decodes the request body over it and saves it
|
||||
func updateByIDHandler[T any](
|
||||
paramName string,
|
||||
getByID func(uint) (T, error),
|
||||
save func(*T) error,
|
||||
) func(http.ResponseWriter, *http.Request) {
|
||||
return withEntity(paramName, getByID, func(w http.ResponseWriter, r *http.Request, item *T) {
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
if err := json.Unmarshal(bodyBytes, item); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := save(item); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
})
|
||||
}
|
||||
|
||||
// deleteByIDHandler returns a handler that loads the entity by the ID in the
|
||||
// paramName url param, deletes it and responds with the result of del
|
||||
func deleteByIDHandler[T, R any](
|
||||
paramName string,
|
||||
getByID func(uint) (T, error),
|
||||
del func(*T) R,
|
||||
) func(http.ResponseWriter, *http.Request) {
|
||||
return withEntity(paramName, getByID, func(w http.ResponseWriter, r *http.Request, item *T) {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, del(item))
|
||||
})
|
||||
}
|
||||
|
||||
// withEntity returns a handler that loads the entity by the ID in the
|
||||
// paramName url param and passes it to fn, responding with not found or
|
||||
// bad request when it can't be loaded
|
||||
func withEntity[T any](
|
||||
paramName string,
|
||||
getByID func(uint) (T, error),
|
||||
fn func(http.ResponseWriter, *http.Request, *T),
|
||||
) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := getURLParamInt(r, paramName)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := getByID(id)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
fn(w, r, &item)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// listHandler returns a handler that responds with a page of entities
|
||||
// matching the request's pagination and filters
|
||||
func listHandler(list func(model.PageInfo, []model.Filter) (entity.ListResponse, error)) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
pageInfo, err := getPageInfoFromRequest(r)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
items, err := list(pageInfo, middleware.GetFiltersFromContext(r))
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, items)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,144 +1,44 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/dnsproviders"
|
||||
"npm/internal/entity/dnsprovider"
|
||||
"npm/internal/errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GetDNSProviders will return a list of DNS Providers
|
||||
// Route: GET /dns-providers
|
||||
func GetDNSProviders() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
pageInfo, err := getPageInfoFromRequest(r)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
items, err := dnsprovider.List(pageInfo, middleware.GetFiltersFromContext(r))
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, items)
|
||||
}
|
||||
}
|
||||
return listHandler(dnsprovider.List)
|
||||
}
|
||||
|
||||
// GetDNSProvider will return a single DNS Provider
|
||||
// Route: GET /dns-providers/{providerID}
|
||||
func GetDNSProvider() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var providerID uint
|
||||
if providerID, err = getURLParamInt(r, "providerID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := dnsprovider.GetByID(providerID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return getByIDHandler("providerID", dnsprovider.GetByID)
|
||||
}
|
||||
|
||||
// CreateDNSProvider will create a DNS Provider
|
||||
// Route: POST /dns-providers
|
||||
func CreateDNSProvider() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
|
||||
var newItem dnsprovider.Model
|
||||
err := json.Unmarshal(bodyBytes, &newItem)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Get userID from token
|
||||
userID, _ := r.Context().Value(c.UserIDCtxKey).(uint)
|
||||
newItem.UserID = userID
|
||||
|
||||
if err = newItem.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save DNS Provider: %s", err.Error()), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, newItem)
|
||||
createUserOwned(w, r, &dnsprovider.Model{}, "DNS Provider")
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateDNSProvider updates a provider
|
||||
// Route: PUT /dns-providers/{providerID}
|
||||
func UpdateDNSProvider() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var providerID uint
|
||||
if providerID, err = getURLParamInt(r, "providerID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := dnsprovider.GetByID(providerID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
err := json.Unmarshal(bodyBytes, &item)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = item.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return updateByIDHandler("providerID", dnsprovider.GetByID, (*dnsprovider.Model).Save)
|
||||
}
|
||||
|
||||
// DeleteDNSProvider removes a provider
|
||||
// Route: DELETE /dns-providers/{providerID}
|
||||
func DeleteDNSProvider() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var providerID uint
|
||||
if providerID, err = getURLParamInt(r, "providerID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := dnsprovider.GetByID(providerID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return deleteByIDHandler("providerID", dnsprovider.GetByID, (*dnsprovider.Model).Delete)
|
||||
}
|
||||
|
||||
// GetAcmeshProviders will return a list of acme.sh providers
|
||||
|
||||
@@ -172,34 +172,7 @@ func DeleteHost() func(http.ResponseWriter, *http.Request) {
|
||||
// Route: GET /hosts/{hostID}/nginx-config
|
||||
// Route: GET /hosts/{hostID}/nginx-config.txt
|
||||
func GetHostNginxConfig(format string) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var hostID uint
|
||||
if hostID, err = getURLParamInt(r, "hostID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := host.GetByID(hostID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
// Get the config from disk
|
||||
content, nErr := nginx.GetHostConfigContent(item)
|
||||
if nErr != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, nErr.Error(), nil)
|
||||
return
|
||||
}
|
||||
if format == "text" {
|
||||
h.ResultResponseText(w, http.StatusOK, content)
|
||||
return
|
||||
}
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, content)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return getNginxConfig(format, "hostID", host.GetByID, nginx.GetHostConfigContent)
|
||||
}
|
||||
|
||||
func configureHost(hst host.Model) {
|
||||
|
||||
@@ -1,142 +1,40 @@
|
||||
//nolint:dupl // thin wiring of the shared CRUD helpers in crud.go
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/entity/nginxtemplate"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GetNginxTemplates will return a list of Nginx Templates
|
||||
// Route: GET /nginx-templates
|
||||
func GetNginxTemplates() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
pageInfo, err := getPageInfoFromRequest(r)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
items, err := nginxtemplate.List(pageInfo, middleware.GetFiltersFromContext(r))
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, items)
|
||||
}
|
||||
}
|
||||
return listHandler(nginxtemplate.List)
|
||||
}
|
||||
|
||||
// GetNginxTemplate will return a single Nginx Template
|
||||
// Route: GET /nginx-templates/{templateID}
|
||||
func GetNginxTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var templateID uint
|
||||
if templateID, err = getURLParamInt(r, "templateID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := nginxtemplate.GetByID(templateID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return getByIDHandler("templateID", nginxtemplate.GetByID)
|
||||
}
|
||||
|
||||
// CreateNginxTemplate will create a Nginx Template
|
||||
// Route: POST /nginx-templates
|
||||
func CreateNginxTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
|
||||
var newNginxTemplate nginxtemplate.Model
|
||||
err := json.Unmarshal(bodyBytes, &newNginxTemplate)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Get userID from token
|
||||
userID, _ := r.Context().Value(c.UserIDCtxKey).(uint)
|
||||
newNginxTemplate.UserID = userID
|
||||
|
||||
if err = newNginxTemplate.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save Nginx Template: %s", err.Error()), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, newNginxTemplate)
|
||||
createUserOwned(w, r, &nginxtemplate.Model{}, "Nginx Template")
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateNginxTemplate updates a nginx template
|
||||
// Route: PUT /nginx-templates/{templateID}
|
||||
func UpdateNginxTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var templateID uint
|
||||
if templateID, err = getURLParamInt(r, "templateID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// reconfigure, _ := getQueryVarBool(r, "reconfigure", false, false)
|
||||
|
||||
nginxTemplate, err := nginxtemplate.GetByID(templateID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
err := json.Unmarshal(bodyBytes, &nginxTemplate)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = nginxTemplate.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, nginxTemplate)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return updateByIDHandler("templateID", nginxtemplate.GetByID, (*nginxtemplate.Model).Save)
|
||||
}
|
||||
|
||||
// DeleteNginxTemplate removes a nginx template
|
||||
// Route: DELETE /nginx-templates/{templateID}
|
||||
func DeleteNginxTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var templateID uint
|
||||
if templateID, err = getURLParamInt(r, "templateID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := nginxtemplate.GetByID(templateID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return deleteByIDHandler("templateID", nginxtemplate.GetByID, (*nginxtemplate.Model).Delete)
|
||||
}
|
||||
@@ -35,12 +35,13 @@ func NotFound() func(http.ResponseWriter, *http.Request) {
|
||||
}
|
||||
|
||||
err := tryRead(assetsSub, path, w)
|
||||
if err == errIsDir {
|
||||
switch err {
|
||||
case errIsDir:
|
||||
err = tryRead(assetsSub, defaultFile, w)
|
||||
if err != nil {
|
||||
h.NotFound(w, r)
|
||||
}
|
||||
} else if err == nil {
|
||||
case nil:
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/entity/auth"
|
||||
@@ -15,17 +14,15 @@ import (
|
||||
njwt "npm/internal/jwt"
|
||||
"npm/internal/logger"
|
||||
|
||||
chiMiddleware "github.com/go-chi/chi/v5/middleware"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// getRequestIPAddress will use X-FORWARDED-FOR header if it exists
|
||||
// otherwise it will use RemoteAddr
|
||||
// getRequestIPAddress will use the client IP set by the router's client IP
|
||||
// middleware if it exists, otherwise it will use RemoteAddr
|
||||
func getRequestIPAddress(r *http.Request) string {
|
||||
// this Get is case insensitive
|
||||
xff := r.Header.Get("X-FORWARDED-FOR")
|
||||
if xff != "" {
|
||||
ip, _, _ := strings.Cut(xff, ",")
|
||||
return strings.TrimSpace(ip)
|
||||
if ip := chiMiddleware.GetClientIP(r.Context()); ip != "" {
|
||||
return ip
|
||||
}
|
||||
return r.RemoteAddr
|
||||
}
|
||||
|
||||
@@ -1,140 +1,40 @@
|
||||
//nolint:dupl // thin wiring of the shared CRUD helpers in crud.go
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/entity/stream"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GetStreams will return a list of Streams
|
||||
// Route: GET /hosts/streams
|
||||
func GetStreams() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
pageInfo, err := getPageInfoFromRequest(r)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
hosts, err := stream.List(pageInfo, middleware.GetFiltersFromContext(r))
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, hosts)
|
||||
}
|
||||
}
|
||||
return listHandler(stream.List)
|
||||
}
|
||||
|
||||
// GetStream will return a single Streams
|
||||
// Route: GET /hosts/streams/{hostID}
|
||||
func GetStream() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var hostID uint
|
||||
if hostID, err = getURLParamInt(r, "hostID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := stream.GetByID(hostID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return getByIDHandler("hostID", stream.GetByID)
|
||||
}
|
||||
|
||||
// CreateStream will create a Stream
|
||||
// Route: POST /hosts/steams
|
||||
func CreateStream() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
|
||||
var newHost stream.Model
|
||||
err := json.Unmarshal(bodyBytes, &newHost)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Get userID from token
|
||||
userID, _ := r.Context().Value(c.UserIDCtxKey).(uint)
|
||||
newHost.UserID = userID
|
||||
|
||||
if err = newHost.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save Stream: %s", err.Error()), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, newHost)
|
||||
createUserOwned(w, r, &stream.Model{}, "Stream")
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateStream updates a stream
|
||||
// Route: PUT /hosts/streams/{hostID}
|
||||
func UpdateStream() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var hostID uint
|
||||
if hostID, err = getURLParamInt(r, "hostID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
host, err := stream.GetByID(hostID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
err := json.Unmarshal(bodyBytes, &host)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = host.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, host)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return updateByIDHandler("hostID", stream.GetByID, (*stream.Model).Save)
|
||||
}
|
||||
|
||||
// DeleteStream removes a stream
|
||||
// Route: DELETE /hosts/streams/{hostID}
|
||||
func DeleteStream() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var hostID uint
|
||||
if hostID, err = getURLParamInt(r, "hostID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := stream.GetByID(hostID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return deleteByIDHandler("hostID", stream.GetByID, (*stream.Model).Delete)
|
||||
}
|
||||
@@ -171,34 +171,7 @@ func DeleteUpstream() func(http.ResponseWriter, *http.Request) {
|
||||
// Route: GET /upstreams/{upstreamID}/nginx-config
|
||||
// Route: GET /upstreams/{upstreamID}/nginx-config.txt
|
||||
func GetUpstreamNginxConfig(format string) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var upstreamID uint
|
||||
if upstreamID, err = getURLParamInt(r, "upstreamID"); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := upstream.GetByID(upstreamID)
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
h.NotFound(w, r)
|
||||
case nil:
|
||||
// Get the config from disk
|
||||
content, nErr := nginx.GetUpstreamConfigContent(item)
|
||||
if nErr != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, nErr.Error(), nil)
|
||||
return
|
||||
}
|
||||
if format == "text" {
|
||||
h.ResultResponseText(w, http.StatusOK, content)
|
||||
return
|
||||
}
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, content)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
return getNginxConfig(format, "upstreamID", upstream.GetByID, nginx.GetUpstreamConfigContent)
|
||||
}
|
||||
|
||||
func configureUpstream(u upstream.Model) {
|
||||
|
||||
@@ -70,7 +70,7 @@ func Enforce(permissions ...string) func(http.Handler) http.Handler {
|
||||
|
||||
var userCapabilities []string
|
||||
if found {
|
||||
userCapabilities = cacheItem.([]string)
|
||||
userCapabilities, _ = cacheItem.([]string)
|
||||
} else {
|
||||
// Get from db and store it
|
||||
userCapabilities, err = user.GetCapabilities(userID)
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestBodyContext(t *testing.T) {
|
||||
|
||||
// Create a test handler that checks the context for the body data
|
||||
handler := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
|
||||
bodyData := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
bodyData, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
assert.Equal(t, body, bodyData)
|
||||
})
|
||||
|
||||
|
||||
@@ -58,16 +58,13 @@ func listQuerySort(
|
||||
return ctx, 0, ""
|
||||
}
|
||||
|
||||
// Split sort fields up in to slice
|
||||
sorts := strings.Split(sortString, ",")
|
||||
for _, sortItem := range sorts {
|
||||
// Split sort fields up
|
||||
for sortItem := range strings.SplitSeq(sortString, ",") {
|
||||
if strings.Contains(sortItem, ".") {
|
||||
theseItems := strings.Split(sortItem, ".")
|
||||
|
||||
switch strings.ToLower(theseItems[1]) {
|
||||
case "desc":
|
||||
fallthrough
|
||||
case "descending":
|
||||
case "desc", "descending":
|
||||
theseItems[1] = "DESC"
|
||||
default:
|
||||
theseItems[1] = "ASC"
|
||||
|
||||
@@ -38,8 +38,8 @@ func SSEAuth(next http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
iss, _ := token.Get("iss")
|
||||
if iss != "sse" {
|
||||
iss, ok := token.Issuer()
|
||||
if !ok || iss != "sse" {
|
||||
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"npm/internal/api/middleware"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// resourceRoutes describes the standard list/create/get/update/delete
|
||||
// endpoints for an entity
|
||||
type resourceRoutes struct {
|
||||
// idParam is the url param name for a specific item, eg "hostID"
|
||||
idParam string
|
||||
// model is the entity used to validate list query sorting and filtering
|
||||
model any
|
||||
// viewCapability is required to list and get items
|
||||
viewCapability string
|
||||
// manageCapability is required to create, update and delete items
|
||||
manageCapability string
|
||||
createSchema string
|
||||
// updateSchema is optional, the request body isn't validated when empty
|
||||
updateSchema string
|
||||
list http.HandlerFunc
|
||||
create http.HandlerFunc
|
||||
get http.HandlerFunc
|
||||
update http.HandlerFunc
|
||||
del http.HandlerFunc
|
||||
// manageItem optionally registers extra routes on a specific item that
|
||||
// require manageCapability
|
||||
manageItem func(chi.Router)
|
||||
}
|
||||
|
||||
// mount registers the resource's routes on r
|
||||
func (rr resourceRoutes) mount(r chi.Router) {
|
||||
// List
|
||||
r.With(
|
||||
middleware.Enforce(rr.viewCapability),
|
||||
middleware.ListQuery(rr.model),
|
||||
).Get("/", rr.list)
|
||||
|
||||
// Create
|
||||
r.With(middleware.Enforce(rr.manageCapability), middleware.EnforceRequestSchema(rr.createSchema)).
|
||||
Post("/", rr.create)
|
||||
|
||||
// Specific Item
|
||||
r.Route("/{"+rr.idParam+":[0-9]+}", func(r chi.Router) {
|
||||
r.With(middleware.Enforce(rr.viewCapability)).
|
||||
Get("/", rr.get)
|
||||
r.With(middleware.Enforce(rr.manageCapability)).Route("/", func(r chi.Router) {
|
||||
r.Delete("/", rr.del)
|
||||
if rr.updateSchema != "" {
|
||||
r.With(middleware.EnforceRequestSchema(rr.updateSchema)).Put("/", rr.update)
|
||||
} else {
|
||||
r.Put("/", rr.update)
|
||||
}
|
||||
if rr.manageItem != nil {
|
||||
rr.manageItem(r)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
+119
-188
@@ -53,7 +53,9 @@ func NewRouter() http.Handler {
|
||||
corss.Handler,
|
||||
middleware.Cors(r),
|
||||
middleware.Options(r),
|
||||
chiMiddleware.RealIP,
|
||||
// nginx in front of the api overwrites X-Forwarded-For with the
|
||||
// connecting address, so the last value is the only trusted one
|
||||
chiMiddleware.ClientIPFromHeader("X-Forwarded-For"),
|
||||
chiMiddleware.Recoverer,
|
||||
chiMiddleware.Throttle(5),
|
||||
middleware.PrettyPrint,
|
||||
@@ -170,51 +172,35 @@ func applyRoutes(r chi.Router) chi.Router {
|
||||
})
|
||||
|
||||
// Access Lists
|
||||
r.With(middleware.EnforceSetup()).Route("/access-lists", func(r chi.Router) {
|
||||
// List
|
||||
r.With(
|
||||
middleware.Enforce(user.CapabilityAccessListsView),
|
||||
middleware.ListQuery(accesslist.Model{}),
|
||||
).Get("/", handler.GetAccessLists())
|
||||
|
||||
// Create
|
||||
r.With(middleware.Enforce(user.CapabilityAccessListsManage), middleware.EnforceRequestSchema(schema.CreateAccessList())).
|
||||
Post("/", handler.CreateAccessList())
|
||||
|
||||
// Specific Item
|
||||
r.Route("/{accessListID:[0-9]+}", func(r chi.Router) {
|
||||
r.With(middleware.Enforce(user.CapabilityAccessListsView)).
|
||||
Get("/", handler.GetAccessList())
|
||||
r.With(middleware.Enforce(user.CapabilityAccessListsManage)).Route("/", func(r chi.Router) {
|
||||
r.Delete("/{accessListID:[0-9]+}", handler.DeleteAccessList())
|
||||
r.With(middleware.EnforceRequestSchema(schema.UpdateAccessList())).
|
||||
Put("/{accessListID:[0-9]+}", handler.UpdateAccessList())
|
||||
})
|
||||
})
|
||||
})
|
||||
r.With(middleware.EnforceSetup()).Route("/access-lists", resourceRoutes{
|
||||
idParam: "accessListID",
|
||||
model: accesslist.Model{},
|
||||
viewCapability: user.CapabilityAccessListsView,
|
||||
manageCapability: user.CapabilityAccessListsManage,
|
||||
createSchema: schema.CreateAccessList(),
|
||||
updateSchema: schema.UpdateAccessList(),
|
||||
list: handler.GetAccessLists(),
|
||||
create: handler.CreateAccessList(),
|
||||
get: handler.GetAccessList(),
|
||||
update: handler.UpdateAccessList(),
|
||||
del: handler.DeleteAccessList(),
|
||||
}.mount)
|
||||
|
||||
// DNS Providers
|
||||
r.With(middleware.EnforceSetup()).Route("/dns-providers", func(r chi.Router) {
|
||||
// List
|
||||
r.With(
|
||||
middleware.Enforce(user.CapabilityDNSProvidersView),
|
||||
middleware.ListQuery(dnsprovider.Model{}),
|
||||
).Get("/", handler.GetDNSProviders())
|
||||
|
||||
// Create
|
||||
r.With(middleware.Enforce(user.CapabilityDNSProvidersManage), middleware.EnforceRequestSchema(schema.CreateDNSProvider())).
|
||||
Post("/", handler.CreateDNSProvider())
|
||||
|
||||
// Specific Item
|
||||
r.Route("/{providerID:[0-9]+}", func(r chi.Router) {
|
||||
r.With(middleware.Enforce(user.CapabilityDNSProvidersView)).
|
||||
Get("/{providerID:[0-9]+}", handler.GetDNSProvider())
|
||||
r.With(middleware.Enforce(user.CapabilityDNSProvidersManage)).Route("/", func(r chi.Router) {
|
||||
r.Delete("/", handler.DeleteDNSProvider())
|
||||
r.With(middleware.EnforceRequestSchema(schema.UpdateDNSProvider())).
|
||||
Put("/{providerID:[0-9]+}", handler.UpdateDNSProvider())
|
||||
})
|
||||
})
|
||||
resourceRoutes{
|
||||
idParam: "providerID",
|
||||
model: dnsprovider.Model{},
|
||||
viewCapability: user.CapabilityDNSProvidersView,
|
||||
manageCapability: user.CapabilityDNSProvidersManage,
|
||||
createSchema: schema.CreateDNSProvider(),
|
||||
updateSchema: schema.UpdateDNSProvider(),
|
||||
list: handler.GetDNSProviders(),
|
||||
create: handler.CreateDNSProvider(),
|
||||
get: handler.GetDNSProvider(),
|
||||
update: handler.UpdateDNSProvider(),
|
||||
del: handler.DeleteDNSProvider(),
|
||||
}.mount(r)
|
||||
|
||||
// List Acme DNS Providers
|
||||
r.With(middleware.Enforce(user.CapabilityDNSProvidersView)).Route("/acmesh", func(r chi.Router) {
|
||||
@@ -224,161 +210,106 @@ func applyRoutes(r chi.Router) chi.Router {
|
||||
})
|
||||
|
||||
// Certificate Authorities
|
||||
r.With(middleware.EnforceSetup()).Route("/certificate-authorities", func(r chi.Router) {
|
||||
// List
|
||||
r.With(
|
||||
middleware.Enforce(user.CapabilityCertificateAuthoritiesView),
|
||||
middleware.ListQuery(certificateauthority.Model{}),
|
||||
).Get("/", handler.GetCertificateAuthorities())
|
||||
|
||||
// Create
|
||||
r.With(middleware.Enforce(user.CapabilityCertificateAuthoritiesManage), middleware.EnforceRequestSchema(schema.CreateCertificateAuthority())).
|
||||
Post("/", handler.CreateCertificateAuthority())
|
||||
|
||||
// Specific Item
|
||||
r.Route("/{caID:[0-9]+}", func(r chi.Router) {
|
||||
r.With(middleware.Enforce(user.CapabilityCertificateAuthoritiesView)).
|
||||
Get("/", handler.GetCertificateAuthority())
|
||||
|
||||
r.With(middleware.EnforceRequestSchema(schema.UpdateCertificateAuthority())).
|
||||
Put("/", handler.UpdateCertificateAuthority())
|
||||
r.With(middleware.Enforce(user.CapabilityCertificateAuthoritiesManage)).
|
||||
Delete("/", handler.DeleteCertificateAuthority())
|
||||
|
||||
r.With(middleware.Enforce(user.CapabilityCertificateAuthoritiesManage)).Route("/", func(r chi.Router) {
|
||||
r.Delete("/{caID:[0-9]+}", handler.DeleteCertificateAuthority())
|
||||
r.With(middleware.EnforceRequestSchema(schema.UpdateCertificateAuthority())).
|
||||
Put("/{caID:[0-9]+}", handler.UpdateCertificateAuthority())
|
||||
})
|
||||
})
|
||||
})
|
||||
r.With(middleware.EnforceSetup()).Route("/certificate-authorities", resourceRoutes{
|
||||
idParam: "caID",
|
||||
model: certificateauthority.Model{},
|
||||
viewCapability: user.CapabilityCertificateAuthoritiesView,
|
||||
manageCapability: user.CapabilityCertificateAuthoritiesManage,
|
||||
createSchema: schema.CreateCertificateAuthority(),
|
||||
updateSchema: schema.UpdateCertificateAuthority(),
|
||||
list: handler.GetCertificateAuthorities(),
|
||||
create: handler.CreateCertificateAuthority(),
|
||||
get: handler.GetCertificateAuthority(),
|
||||
update: handler.UpdateCertificateAuthority(),
|
||||
del: handler.DeleteCertificateAuthority(),
|
||||
}.mount)
|
||||
|
||||
// Certificates
|
||||
r.With(middleware.EnforceSetup()).Route("/certificates", func(r chi.Router) {
|
||||
// List
|
||||
r.With(
|
||||
middleware.Enforce(user.CapabilityCertificatesView),
|
||||
middleware.ListQuery(certificate.Model{}),
|
||||
).Get("/", handler.GetCertificates())
|
||||
|
||||
// Create
|
||||
r.With(middleware.Enforce(user.CapabilityCertificatesManage), middleware.EnforceRequestSchema(schema.CreateCertificate())).
|
||||
Post("/", handler.CreateCertificate())
|
||||
|
||||
// Specific Item
|
||||
r.Route("/{certificateID:[0-9]+}", func(r chi.Router) {
|
||||
r.With(middleware.Enforce(user.CapabilityCertificatesView)).
|
||||
Get("/", handler.GetCertificate())
|
||||
r.With(middleware.Enforce(user.CapabilityCertificatesManage)).Route("/", func(r chi.Router) {
|
||||
r.Delete("/", handler.DeleteCertificate())
|
||||
r.Put("/", handler.UpdateCertificate())
|
||||
// r.With(middleware.EnforceRequestSchema(schema.UpdateCertificate())).
|
||||
// Put("/", handler.UpdateCertificate())
|
||||
r.Post("/renew", handler.RenewCertificate())
|
||||
r.Get("/download", handler.DownloadCertificate())
|
||||
})
|
||||
})
|
||||
})
|
||||
r.With(middleware.EnforceSetup()).Route("/certificates", resourceRoutes{
|
||||
idParam: "certificateID",
|
||||
model: certificate.Model{},
|
||||
viewCapability: user.CapabilityCertificatesView,
|
||||
manageCapability: user.CapabilityCertificatesManage,
|
||||
createSchema: schema.CreateCertificate(),
|
||||
// updateSchema: schema.UpdateCertificate(),
|
||||
list: handler.GetCertificates(),
|
||||
create: handler.CreateCertificate(),
|
||||
get: handler.GetCertificate(),
|
||||
update: handler.UpdateCertificate(),
|
||||
del: handler.DeleteCertificate(),
|
||||
manageItem: func(r chi.Router) {
|
||||
r.Post("/renew", handler.RenewCertificate())
|
||||
r.Get("/download", handler.DownloadCertificate())
|
||||
},
|
||||
}.mount)
|
||||
|
||||
// Hosts
|
||||
r.With(middleware.EnforceSetup()).Route("/hosts", func(r chi.Router) {
|
||||
// List
|
||||
r.With(
|
||||
middleware.Enforce(user.CapabilityHostsView),
|
||||
middleware.ListQuery(host.Model{}),
|
||||
).Get("/", handler.GetHosts())
|
||||
|
||||
// Create
|
||||
r.With(middleware.Enforce(user.CapabilityHostsManage), middleware.EnforceRequestSchema(schema.CreateHost())).
|
||||
Post("/", handler.CreateHost())
|
||||
|
||||
// Specific Item
|
||||
r.Route("/{hostID:[0-9]+}", func(r chi.Router) {
|
||||
r.With(middleware.Enforce(user.CapabilityHostsView)).
|
||||
Get("/", handler.GetHost())
|
||||
r.With(middleware.Enforce(user.CapabilityHostsManage)).Route("/", func(r chi.Router) {
|
||||
r.Delete("/", handler.DeleteHost())
|
||||
r.With(middleware.EnforceRequestSchema(schema.UpdateHost())).
|
||||
Put("/", handler.UpdateHost())
|
||||
r.Get("/nginx-config", handler.GetHostNginxConfig("json"))
|
||||
r.Get("/nginx-config.txt", handler.GetHostNginxConfig("text"))
|
||||
})
|
||||
})
|
||||
})
|
||||
r.With(middleware.EnforceSetup()).Route("/hosts", resourceRoutes{
|
||||
idParam: "hostID",
|
||||
model: host.Model{},
|
||||
viewCapability: user.CapabilityHostsView,
|
||||
manageCapability: user.CapabilityHostsManage,
|
||||
createSchema: schema.CreateHost(),
|
||||
updateSchema: schema.UpdateHost(),
|
||||
list: handler.GetHosts(),
|
||||
create: handler.CreateHost(),
|
||||
get: handler.GetHost(),
|
||||
update: handler.UpdateHost(),
|
||||
del: handler.DeleteHost(),
|
||||
manageItem: func(r chi.Router) {
|
||||
r.Get("/nginx-config", handler.GetHostNginxConfig("json"))
|
||||
r.Get("/nginx-config.txt", handler.GetHostNginxConfig("text"))
|
||||
},
|
||||
}.mount)
|
||||
|
||||
// Nginx Templates
|
||||
r.With(middleware.EnforceSetup()).Route("/nginx-templates", func(r chi.Router) {
|
||||
// List
|
||||
r.With(
|
||||
middleware.Enforce(user.CapabilityNginxTemplatesView),
|
||||
middleware.ListQuery(nginxtemplate.Model{}),
|
||||
).Get("/", handler.GetNginxTemplates())
|
||||
|
||||
// Create
|
||||
r.With(middleware.Enforce(user.CapabilityNginxTemplatesManage), middleware.EnforceRequestSchema(schema.CreateNginxTemplate())).
|
||||
Post("/", handler.CreateNginxTemplate())
|
||||
|
||||
// Specific Item
|
||||
r.Route("/{templateID:[0-9]+}", func(r chi.Router) {
|
||||
r.With(middleware.Enforce(user.CapabilityNginxTemplatesView)).
|
||||
Get("/", handler.GetNginxTemplates())
|
||||
r.With(middleware.Enforce(user.CapabilityHostsManage)).Route("/", func(r chi.Router) {
|
||||
r.Delete("/", handler.DeleteNginxTemplate())
|
||||
r.With(middleware.EnforceRequestSchema(schema.UpdateNginxTemplate())).
|
||||
Put("/", handler.UpdateNginxTemplate())
|
||||
})
|
||||
})
|
||||
})
|
||||
r.With(middleware.EnforceSetup()).Route("/nginx-templates", resourceRoutes{
|
||||
idParam: "templateID",
|
||||
model: nginxtemplate.Model{},
|
||||
viewCapability: user.CapabilityNginxTemplatesView,
|
||||
manageCapability: user.CapabilityNginxTemplatesManage,
|
||||
createSchema: schema.CreateNginxTemplate(),
|
||||
updateSchema: schema.UpdateNginxTemplate(),
|
||||
list: handler.GetNginxTemplates(),
|
||||
create: handler.CreateNginxTemplate(),
|
||||
get: handler.GetNginxTemplate(),
|
||||
update: handler.UpdateNginxTemplate(),
|
||||
del: handler.DeleteNginxTemplate(),
|
||||
}.mount)
|
||||
|
||||
// Streams
|
||||
r.With(middleware.EnforceSetup()).Route("/streams", func(r chi.Router) {
|
||||
// List
|
||||
r.With(
|
||||
middleware.Enforce(user.CapabilityStreamsView),
|
||||
middleware.ListQuery(stream.Model{}),
|
||||
).Get("/", handler.GetStreams())
|
||||
|
||||
// Create
|
||||
r.With(middleware.Enforce(user.CapabilityStreamsManage), middleware.EnforceRequestSchema(schema.CreateStream())).
|
||||
Post("/", handler.CreateStream())
|
||||
|
||||
// Specific Item
|
||||
r.Route("/{hostID:[0-9]+}", func(r chi.Router) {
|
||||
r.With(middleware.Enforce(user.CapabilityStreamsView)).
|
||||
Get("/", handler.GetStream())
|
||||
r.With(middleware.Enforce(user.CapabilityHostsManage)).Route("/", func(r chi.Router) {
|
||||
r.Delete("/", handler.DeleteStream())
|
||||
r.With(middleware.EnforceRequestSchema(schema.UpdateStream())).
|
||||
Put("/", handler.UpdateStream())
|
||||
})
|
||||
})
|
||||
})
|
||||
r.With(middleware.EnforceSetup()).Route("/streams", resourceRoutes{
|
||||
idParam: "hostID",
|
||||
model: stream.Model{},
|
||||
viewCapability: user.CapabilityStreamsView,
|
||||
manageCapability: user.CapabilityStreamsManage,
|
||||
createSchema: schema.CreateStream(),
|
||||
updateSchema: schema.UpdateStream(),
|
||||
list: handler.GetStreams(),
|
||||
create: handler.CreateStream(),
|
||||
get: handler.GetStream(),
|
||||
update: handler.UpdateStream(),
|
||||
del: handler.DeleteStream(),
|
||||
}.mount)
|
||||
|
||||
// Upstreams
|
||||
r.With(middleware.EnforceSetup()).Route("/upstreams", func(r chi.Router) {
|
||||
// List
|
||||
r.With(
|
||||
middleware.Enforce(user.CapabilityHostsView),
|
||||
middleware.ListQuery(upstream.Model{}),
|
||||
).Get("/", handler.GetUpstreams())
|
||||
|
||||
// Create
|
||||
r.With(middleware.Enforce(user.CapabilityHostsManage), middleware.EnforceRequestSchema(schema.CreateUpstream())).
|
||||
Post("/", handler.CreateUpstream())
|
||||
|
||||
// Specific Item
|
||||
r.Route("/{upstreamID:[0-9]+}", func(r chi.Router) {
|
||||
r.With(middleware.Enforce(user.CapabilityHostsView)).
|
||||
Get("/", handler.GetUpstream())
|
||||
r.With(middleware.Enforce(user.CapabilityHostsManage)).Route("/", func(r chi.Router) {
|
||||
r.Delete("/", handler.DeleteUpstream())
|
||||
r.With(middleware.EnforceRequestSchema(schema.UpdateUpstream())).
|
||||
Put("/", handler.UpdateUpstream())
|
||||
r.Get("/nginx-config", handler.GetUpstreamNginxConfig("json"))
|
||||
r.Get("/nginx-config.txt", handler.GetUpstreamNginxConfig("text"))
|
||||
})
|
||||
})
|
||||
})
|
||||
r.With(middleware.EnforceSetup()).Route("/upstreams", resourceRoutes{
|
||||
idParam: "upstreamID",
|
||||
model: upstream.Model{},
|
||||
viewCapability: user.CapabilityHostsView,
|
||||
manageCapability: user.CapabilityHostsManage,
|
||||
createSchema: schema.CreateUpstream(),
|
||||
updateSchema: schema.UpdateUpstream(),
|
||||
list: handler.GetUpstreams(),
|
||||
create: handler.CreateUpstream(),
|
||||
get: handler.GetUpstream(),
|
||||
update: handler.UpdateUpstream(),
|
||||
del: handler.DeleteUpstream(),
|
||||
manageItem: func(r chi.Router) {
|
||||
r.Get("/nginx-config", handler.GetUpstreamNginxConfig("json"))
|
||||
r.Get("/nginx-config.txt", handler.GetUpstreamNginxConfig("text"))
|
||||
},
|
||||
}.mount)
|
||||
})
|
||||
|
||||
return r
|
||||
|
||||
@@ -3,7 +3,6 @@ package api
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"npm/internal/config"
|
||||
@@ -21,8 +20,7 @@ var (
|
||||
// Tear up/down
|
||||
func TestMain(m *testing.M) {
|
||||
config.Init(&version, &commit)
|
||||
code := m.Run()
|
||||
os.Exit(code)
|
||||
m.Run()
|
||||
}
|
||||
|
||||
func TestGetHealthz(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user