2022-05-11 22:47:31 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-01-04 05:36:56 +00:00
|
|
|
"database/sql"
|
2022-05-11 22:47:31 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
c "npm/internal/api/context"
|
|
|
|
h "npm/internal/api/http"
|
|
|
|
"npm/internal/api/middleware"
|
2023-01-04 05:36:56 +00:00
|
|
|
"npm/internal/entity/nginxtemplate"
|
2022-05-11 22:47:31 +00:00
|
|
|
)
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// GetNginxTemplates will return a list of Nginx Templates
|
|
|
|
// Route: GET /nginx-templates
|
|
|
|
func GetNginxTemplates() func(http.ResponseWriter, *http.Request) {
|
2022-05-11 22:47:31 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
items, err := nginxtemplate.List(pageInfo, middleware.GetFiltersFromContext(r))
|
2022-05-11 22:47:31 +00:00
|
|
|
if err != nil {
|
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
|
|
} else {
|
2023-01-04 05:36:56 +00:00
|
|
|
h.ResultResponseJSON(w, r, http.StatusOK, items)
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// GetNginxTemplate will return a single Nginx Template
|
|
|
|
// Route: GET /nginx-templates/{templateID}
|
|
|
|
func GetNginxTemplate() func(http.ResponseWriter, *http.Request) {
|
2022-05-11 22:47:31 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
2023-05-26 01:04:43 +00:00
|
|
|
var templateID uint
|
2022-05-11 22:47:31 +00:00
|
|
|
if templateID, err = getURLParamInt(r, "templateID"); err != nil {
|
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
item, err := nginxtemplate.GetByID(templateID)
|
|
|
|
switch err {
|
|
|
|
case sql.ErrNoRows:
|
2023-02-27 07:21:40 +00:00
|
|
|
h.NotFound(w, r)
|
2023-01-04 05:36:56 +00:00
|
|
|
case nil:
|
|
|
|
h.ResultResponseJSON(w, r, http.StatusOK, item)
|
|
|
|
default:
|
2022-05-11 22:47:31 +00:00
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// CreateNginxTemplate will create a Nginx Template
|
|
|
|
// Route: POST /nginx-templates
|
|
|
|
func CreateNginxTemplate() func(http.ResponseWriter, *http.Request) {
|
2022-05-11 22:47:31 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
var newNginxTemplate nginxtemplate.Model
|
|
|
|
err := json.Unmarshal(bodyBytes, &newNginxTemplate)
|
2022-05-11 22:47:31 +00:00
|
|
|
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)
|
2023-01-04 05:36:56 +00:00
|
|
|
newNginxTemplate.UserID = userID
|
2022-05-11 22:47:31 +00:00
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
if err = newNginxTemplate.Save(); err != nil {
|
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save Nginx Template: %s", err.Error()), nil)
|
2022-05-11 22:47:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
h.ResultResponseJSON(w, r, http.StatusOK, newNginxTemplate)
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// UpdateNginxTemplate updates a nginx template
|
|
|
|
// Route: PUT /nginx-templates/{templateID}
|
|
|
|
func UpdateNginxTemplate() func(http.ResponseWriter, *http.Request) {
|
2022-05-11 22:47:31 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
2023-05-26 01:04:43 +00:00
|
|
|
var templateID uint
|
2022-05-11 22:47:31 +00:00
|
|
|
if templateID, err = getURLParamInt(r, "templateID"); err != nil {
|
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-08 00:03:45 +00:00
|
|
|
// reconfigure, _ := getQueryVarBool(r, "reconfigure", false, false)
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
nginxTemplate, err := nginxtemplate.GetByID(templateID)
|
2023-02-27 07:21:40 +00:00
|
|
|
switch err {
|
|
|
|
case sql.ErrNoRows:
|
|
|
|
h.NotFound(w, r)
|
|
|
|
case nil:
|
2022-05-11 22:47:31 +00:00
|
|
|
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
2023-01-04 05:36:56 +00:00
|
|
|
err := json.Unmarshal(bodyBytes, &nginxTemplate)
|
2022-05-11 22:47:31 +00:00
|
|
|
if err != nil {
|
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
if err = nginxTemplate.Save(); err != nil {
|
2022-05-11 22:47:31 +00:00
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
h.ResultResponseJSON(w, r, http.StatusOK, nginxTemplate)
|
2023-02-27 07:21:40 +00:00
|
|
|
default:
|
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// DeleteNginxTemplate removes a nginx template
|
|
|
|
// Route: DELETE /nginx-templates/{templateID}
|
|
|
|
func DeleteNginxTemplate() func(http.ResponseWriter, *http.Request) {
|
2022-05-11 22:47:31 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
2023-05-26 01:04:43 +00:00
|
|
|
var templateID uint
|
2022-05-11 22:47:31 +00:00
|
|
|
if templateID, err = getURLParamInt(r, "templateID"); err != nil {
|
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
item, err := nginxtemplate.GetByID(templateID)
|
|
|
|
switch err {
|
|
|
|
case sql.ErrNoRows:
|
2023-02-27 07:21:40 +00:00
|
|
|
h.NotFound(w, r)
|
2023-01-04 05:36:56 +00:00
|
|
|
case nil:
|
|
|
|
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
|
|
|
|
default:
|
2022-05-11 22:47:31 +00:00
|
|
|
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|