- Added upstream objects
- Renamed host templates to nginx templates - Generate upstream templates - Better nginx error reporting when reloading - Use tparse for golang test reporting
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@ -45,13 +46,16 @@ func GetHost() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
hostObject, err := host.GetByID(hostID)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
item, err := host.GetByID(hostID)
|
||||
switch err {
|
||||
case sql.ErrNoRows:
|
||||
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
|
||||
case nil:
|
||||
// nolint: errcheck,gosec
|
||||
hostObject.Expand(getExpandFromContext(r))
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, hostObject)
|
||||
item.Expand(getExpandFromContext(r))
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -111,6 +115,11 @@ func UpdateHost() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = validator.ValidateHost(hostObject); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = hostObject.Save(false); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
@ -137,11 +146,14 @@ func DeleteHost() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
host, err := host.GetByID(hostID)
|
||||
if err != nil {
|
||||
item, err := host.GetByID(hostID)
|
||||
switch err {
|
||||
case sql.ErrNoRows:
|
||||
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, host.Delete())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@ -8,13 +9,12 @@ import (
|
||||
c "npm/internal/api/context"
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/entity/host"
|
||||
"npm/internal/entity/hosttemplate"
|
||||
"npm/internal/entity/nginxtemplate"
|
||||
)
|
||||
|
||||
// GetHostTemplates will return a list of Host Templates
|
||||
// Route: GET /host-templates
|
||||
func GetHostTemplates() func(http.ResponseWriter, *http.Request) {
|
||||
// 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 {
|
||||
@ -22,18 +22,18 @@ func GetHostTemplates() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
hosts, err := hosttemplate.List(pageInfo, middleware.GetFiltersFromContext(r))
|
||||
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, hosts)
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, items)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetHostTemplate will return a single Host Template
|
||||
// Route: GET /host-templates/{templateID}
|
||||
func GetHostTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
// 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 int
|
||||
@ -42,23 +42,26 @@ func GetHostTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
host, err := hosttemplate.GetByID(templateID)
|
||||
if err != nil {
|
||||
item, err := nginxtemplate.GetByID(templateID)
|
||||
switch err {
|
||||
case sql.ErrNoRows:
|
||||
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CreateHostTemplate will create a Host Template
|
||||
// Route: POST /host-templates
|
||||
func CreateHostTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
// 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 newHostTemplate hosttemplate.Model
|
||||
err := json.Unmarshal(bodyBytes, &newHostTemplate)
|
||||
var newNginxTemplate nginxtemplate.Model
|
||||
err := json.Unmarshal(bodyBytes, &newNginxTemplate)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
@ -66,20 +69,20 @@ func CreateHostTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
|
||||
// Get userID from token
|
||||
userID, _ := r.Context().Value(c.UserIDCtxKey).(int)
|
||||
newHostTemplate.UserID = userID
|
||||
newNginxTemplate.UserID = userID
|
||||
|
||||
if err = newHostTemplate.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save Host Template: %s", err.Error()), nil)
|
||||
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, newHostTemplate)
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, newNginxTemplate)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateHostTemplate updates a host template
|
||||
// Route: PUT /host-templates/{templateID}
|
||||
func UpdateHostTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
// 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 int
|
||||
@ -90,30 +93,30 @@ func UpdateHostTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
|
||||
// reconfigure, _ := getQueryVarBool(r, "reconfigure", false, false)
|
||||
|
||||
hostTemplate, err := hosttemplate.GetByID(templateID)
|
||||
nginxTemplate, err := nginxtemplate.GetByID(templateID)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
err := json.Unmarshal(bodyBytes, &hostTemplate)
|
||||
err := json.Unmarshal(bodyBytes, &nginxTemplate)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = hostTemplate.Save(); err != nil {
|
||||
if err = nginxTemplate.Save(); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, hostTemplate)
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, nginxTemplate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteHostTemplate removes a host template
|
||||
// Route: DELETE /host-templates/{templateID}
|
||||
func DeleteHostTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
// 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 int
|
||||
@ -122,11 +125,14 @@ func DeleteHostTemplate() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
hostTemplate, err := host.GetByID(templateID)
|
||||
if err != nil {
|
||||
item, err := nginxtemplate.GetByID(templateID)
|
||||
switch err {
|
||||
case sql.ErrNoRows:
|
||||
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, hostTemplate.Delete())
|
||||
}
|
||||
}
|
||||
}
|
@ -95,8 +95,8 @@ func replaceIncomingSchemas(swaggerSchema []byte) []byte {
|
||||
str = strings.ReplaceAll(str, `"{{schema.CreateHost}}"`, schema.CreateHost())
|
||||
str = strings.ReplaceAll(str, `"{{schema.UpdateHost}}"`, schema.UpdateHost())
|
||||
|
||||
str = strings.ReplaceAll(str, `"{{schema.CreateHostTemplate}}"`, schema.CreateHostTemplate())
|
||||
str = strings.ReplaceAll(str, `"{{schema.UpdateHostTemplate}}"`, schema.UpdateHostTemplate())
|
||||
str = strings.ReplaceAll(str, `"{{schema.CreateNginxTemplate}}"`, schema.CreateNginxTemplate())
|
||||
str = strings.ReplaceAll(str, `"{{schema.UpdateNginxTemplate}}"`, schema.UpdateNginxTemplate())
|
||||
|
||||
str = strings.ReplaceAll(str, `"{{schema.CreateStream}}"`, schema.CreateStream())
|
||||
str = strings.ReplaceAll(str, `"{{schema.UpdateStream}}"`, schema.UpdateStream())
|
||||
@ -104,5 +104,7 @@ func replaceIncomingSchemas(swaggerSchema []byte) []byte {
|
||||
str = strings.ReplaceAll(str, `"{{schema.CreateDNSProvider}}"`, schema.CreateDNSProvider())
|
||||
str = strings.ReplaceAll(str, `"{{schema.UpdateDNSProvider}}"`, schema.UpdateDNSProvider())
|
||||
|
||||
str = strings.ReplaceAll(str, `"{{schema.CreateUpstream}}"`, schema.CreateUpstream())
|
||||
|
||||
return []byte(str)
|
||||
}
|
||||
|
129
backend/internal/api/handler/upstreams.go
Normal file
129
backend/internal/api/handler/upstreams.go
Normal file
@ -0,0 +1,129 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/entity/upstream"
|
||||
"npm/internal/jobqueue"
|
||||
"npm/internal/logger"
|
||||
"npm/internal/nginx"
|
||||
"npm/internal/validator"
|
||||
)
|
||||
|
||||
// GetUpstreams will return a list of Upstreams
|
||||
// Route: GET /upstreams
|
||||
func GetUpstreams() 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 := upstream.List(pageInfo, middleware.GetFiltersFromContext(r), getExpandFromContext(r))
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
} else {
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, items)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetUpstream will return a single Upstream
|
||||
// Route: GET /upstreams/{upstreamID}
|
||||
func GetUpstream() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var upstreamID int
|
||||
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 sql.ErrNoRows:
|
||||
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
|
||||
case nil:
|
||||
// nolint: errcheck,gosec
|
||||
item.Expand(getExpandFromContext(r))
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CreateUpstream will create a Upstream
|
||||
// Route: POST /upstreams
|
||||
func CreateUpstream() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
|
||||
var newUpstream upstream.Model
|
||||
err := json.Unmarshal(bodyBytes, &newUpstream)
|
||||
if err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Get userID from token
|
||||
userID, _ := r.Context().Value(c.UserIDCtxKey).(int)
|
||||
newUpstream.UserID = userID
|
||||
|
||||
if err = validator.ValidateUpstream(newUpstream); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = newUpstream.Save(false); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save Upstream: %s", err.Error()), nil)
|
||||
return
|
||||
}
|
||||
|
||||
configureUpstream(newUpstream)
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, newUpstream)
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteUpstream removes a upstream
|
||||
// Route: DELETE /upstreams/{upstreamID}
|
||||
func DeleteUpstream() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var upstreamID int
|
||||
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 sql.ErrNoRows:
|
||||
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
|
||||
case nil:
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
|
||||
default:
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func configureUpstream(u upstream.Model) {
|
||||
err := jobqueue.AddJob(jobqueue.Job{
|
||||
Name: "NginxConfigureUpstream",
|
||||
Action: func() error {
|
||||
return nginx.ConfigureUpstream(u)
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("ConfigureUpstreamError", err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user