Move away from dbmate, use PolySchema

This commit is contained in:
Jamie Curnow committed 2026-09-28 11:31:27 +10:00
1 parent 52399c42f1
commit 2a14f3f84c
99 files changed
+1583 -1691

No files matched your search

+2 -35
View File
@@ -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")
}
}
+1 -1
View File
@@ -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)
+169
View File
@@ -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)
}
}
}
+5 -105
View File
@@ -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
+1 -28
View File
@@ -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) {
+6 -108
View File
@@ -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)
}
+3 -2
View File
@@ -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 -8
View File
@@ -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
}
+6 -106
View File
@@ -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)
}
+1 -28
View File
@@ -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) {