mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-08-28 03:30:05 +00:00
136 lines
3.6 KiB
Go
136 lines
3.6 KiB
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/host"
|
|
"npm/internal/validator"
|
|
)
|
|
|
|
// GetHosts will return a list of Hosts
|
|
// Route: GET /hosts
|
|
func GetHosts() 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 := host.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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetHost will return a single Host
|
|
// Route: GET /hosts/{hostID}
|
|
func GetHost() func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var err error
|
|
var hostID int
|
|
if hostID, err = getURLParamInt(r, "hostID"); err != nil {
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
return
|
|
}
|
|
|
|
host, err := host.GetByID(hostID)
|
|
if err != nil {
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
} else {
|
|
h.ResultResponseJSON(w, r, http.StatusOK, host)
|
|
}
|
|
}
|
|
}
|
|
|
|
// CreateHost will create a Host
|
|
// Route: POST /hosts
|
|
func CreateHost() func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
|
|
|
var newHost host.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).(int)
|
|
newHost.UserID = userID
|
|
|
|
if err = validator.ValidateHost(newHost); err != nil {
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
return
|
|
}
|
|
|
|
if err = newHost.Save(); err != nil {
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save Host: %s", err.Error()), nil)
|
|
return
|
|
}
|
|
|
|
h.ResultResponseJSON(w, r, http.StatusOK, newHost)
|
|
}
|
|
}
|
|
|
|
// UpdateHost updates a host
|
|
// Route: PUT /hosts/{hostID}
|
|
func UpdateHost() func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var err error
|
|
var hostID int
|
|
if hostID, err = getURLParamInt(r, "hostID"); err != nil {
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
return
|
|
}
|
|
|
|
host, err := host.GetByID(hostID)
|
|
if err != nil {
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
} else {
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// DeleteHost removes a host
|
|
// Route: DELETE /hosts/{hostID}
|
|
func DeleteHost() func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var err error
|
|
var hostID int
|
|
if hostID, err = getURLParamInt(r, "hostID"); err != nil {
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
return
|
|
}
|
|
|
|
host, err := host.GetByID(hostID)
|
|
if err != nil {
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
} else {
|
|
h.ResultResponseJSON(w, r, http.StatusOK, host.Delete())
|
|
}
|
|
}
|
|
}
|