- 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:
25
backend/internal/entity/nginxtemplate/filters.go
Normal file
25
backend/internal/entity/nginxtemplate/filters.go
Normal file
@ -0,0 +1,25 @@
|
||||
package nginxtemplate
|
||||
|
||||
import (
|
||||
"npm/internal/entity"
|
||||
)
|
||||
|
||||
var filterMapFunctions = make(map[string]entity.FilterMapFunction)
|
||||
|
||||
// getFilterMapFunctions is a map of functions that should be executed
|
||||
// during the filtering process, if a field is defined here then the value in
|
||||
// the filter will be given to the defined function and it will return a new
|
||||
// value for use in the sql query.
|
||||
func getFilterMapFunctions() map[string]entity.FilterMapFunction {
|
||||
// if len(filterMapFunctions) == 0 {
|
||||
// TODO: See internal/model/file_item.go:620 for an example
|
||||
// }
|
||||
|
||||
return filterMapFunctions
|
||||
}
|
||||
|
||||
// GetFilterSchema returns filter schema
|
||||
func GetFilterSchema() string {
|
||||
var m Model
|
||||
return entity.GetFilterSchema(m)
|
||||
}
|
129
backend/internal/entity/nginxtemplate/methods.go
Normal file
129
backend/internal/entity/nginxtemplate/methods.go
Normal file
@ -0,0 +1,129 @@
|
||||
package nginxtemplate
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
goerrors "errors"
|
||||
"fmt"
|
||||
|
||||
"npm/internal/database"
|
||||
"npm/internal/entity"
|
||||
"npm/internal/errors"
|
||||
"npm/internal/logger"
|
||||
"npm/internal/model"
|
||||
)
|
||||
|
||||
// GetByID finds a Host by ID
|
||||
func GetByID(id int) (Model, error) {
|
||||
var m Model
|
||||
err := m.LoadByID(id)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// Create will create a Host from this model
|
||||
func Create(host *Model) (int, error) {
|
||||
if host.ID != 0 {
|
||||
return 0, goerrors.New("Cannot create host template when model already has an ID")
|
||||
}
|
||||
|
||||
host.Touch(true)
|
||||
|
||||
db := database.GetInstance()
|
||||
// nolint: gosec
|
||||
result, err := db.NamedExec(`INSERT INTO `+fmt.Sprintf("`%s`", tableName)+` (
|
||||
created_on,
|
||||
modified_on,
|
||||
user_id,
|
||||
name,
|
||||
host_type,
|
||||
template,
|
||||
is_deleted
|
||||
) VALUES (
|
||||
:created_on,
|
||||
:modified_on,
|
||||
:user_id,
|
||||
:name,
|
||||
:host_type,
|
||||
:template,
|
||||
:is_deleted
|
||||
)`, host)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
last, lastErr := result.LastInsertId()
|
||||
if lastErr != nil {
|
||||
return 0, lastErr
|
||||
}
|
||||
|
||||
return int(last), nil
|
||||
}
|
||||
|
||||
// Update will Update a Host from this model
|
||||
func Update(host *Model) error {
|
||||
if host.ID == 0 {
|
||||
return goerrors.New("Cannot update host template when model doesn't have an ID")
|
||||
}
|
||||
|
||||
host.Touch(false)
|
||||
|
||||
db := database.GetInstance()
|
||||
// nolint: gosec
|
||||
_, err := db.NamedExec(`UPDATE `+fmt.Sprintf("`%s`", tableName)+` SET
|
||||
created_on = :created_on,
|
||||
modified_on = :modified_on,
|
||||
user_id = :user_id,
|
||||
name = :name,
|
||||
host_type = :host_type,
|
||||
template = :template,
|
||||
is_deleted = :is_deleted
|
||||
WHERE id = :id`, host)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// List will return a list of hosts
|
||||
func List(pageInfo model.PageInfo, filters []model.Filter) (ListResponse, error) {
|
||||
var result ListResponse
|
||||
var exampleModel Model
|
||||
|
||||
defaultSort := model.Sort{
|
||||
Field: "created_on",
|
||||
Direction: "ASC",
|
||||
}
|
||||
|
||||
db := database.GetInstance()
|
||||
if db == nil {
|
||||
return result, errors.ErrDatabaseUnavailable
|
||||
}
|
||||
|
||||
// Get count of items in this search
|
||||
query, params := entity.ListQueryBuilder(exampleModel, tableName, &pageInfo, defaultSort, filters, getFilterMapFunctions(), true)
|
||||
countRow := db.QueryRowx(query, params...)
|
||||
var totalRows int
|
||||
queryErr := countRow.Scan(&totalRows)
|
||||
if queryErr != nil && queryErr != sql.ErrNoRows {
|
||||
logger.Debug("%s -- %+v", query, params)
|
||||
return result, queryErr
|
||||
}
|
||||
|
||||
// Get rows
|
||||
items := make([]Model, 0)
|
||||
query, params = entity.ListQueryBuilder(exampleModel, tableName, &pageInfo, defaultSort, filters, getFilterMapFunctions(), false)
|
||||
err := db.Select(&items, query, params...)
|
||||
if err != nil {
|
||||
logger.Debug("%s -- %+v", query, params)
|
||||
return result, err
|
||||
}
|
||||
|
||||
result = ListResponse{
|
||||
Items: items,
|
||||
Total: totalRows,
|
||||
Limit: pageInfo.Limit,
|
||||
Offset: pageInfo.Offset,
|
||||
Sort: pageInfo.Sort,
|
||||
Filter: filters,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
73
backend/internal/entity/nginxtemplate/model.go
Normal file
73
backend/internal/entity/nginxtemplate/model.go
Normal file
@ -0,0 +1,73 @@
|
||||
package nginxtemplate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"npm/internal/database"
|
||||
"npm/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
tableName = "nginx_template"
|
||||
)
|
||||
|
||||
// Model is the user model
|
||||
type Model struct {
|
||||
ID int `json:"id" db:"id" filter:"id,integer"`
|
||||
CreatedOn types.DBDate `json:"created_on" db:"created_on" filter:"created_on,integer"`
|
||||
ModifiedOn types.DBDate `json:"modified_on" db:"modified_on" filter:"modified_on,integer"`
|
||||
UserID int `json:"user_id" db:"user_id" filter:"user_id,integer"`
|
||||
Name string `json:"name" db:"name" filter:"name,string"`
|
||||
Type string `json:"type" db:"type" filter:"type,string"`
|
||||
Template string `json:"template" db:"template" filter:"template,string"`
|
||||
IsDeleted bool `json:"is_deleted,omitempty" db:"is_deleted"`
|
||||
}
|
||||
|
||||
func (m *Model) getByQuery(query string, params []interface{}) error {
|
||||
return database.GetByQuery(m, query, params)
|
||||
}
|
||||
|
||||
// LoadByID will load from an ID
|
||||
func (m *Model) LoadByID(id int) error {
|
||||
query := fmt.Sprintf("SELECT * FROM `%s` WHERE id = ? AND is_deleted = ? LIMIT 1", tableName)
|
||||
params := []interface{}{id, 0}
|
||||
return m.getByQuery(query, params)
|
||||
}
|
||||
|
||||
// Touch will update model's timestamp(s)
|
||||
func (m *Model) Touch(created bool) {
|
||||
var d types.DBDate
|
||||
d.Time = time.Now()
|
||||
if created {
|
||||
m.CreatedOn = d
|
||||
}
|
||||
m.ModifiedOn = d
|
||||
}
|
||||
|
||||
// Save will save this model to the DB
|
||||
func (m *Model) Save() error {
|
||||
var err error
|
||||
|
||||
if m.UserID == 0 {
|
||||
return fmt.Errorf("User ID must be specified")
|
||||
}
|
||||
|
||||
if m.ID == 0 {
|
||||
m.ID, err = Create(m)
|
||||
} else {
|
||||
err = Update(m)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete will mark a template as deleted
|
||||
func (m *Model) Delete() bool {
|
||||
m.Touch(false)
|
||||
m.IsDeleted = true
|
||||
if err := m.Save(); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
15
backend/internal/entity/nginxtemplate/structs.go
Normal file
15
backend/internal/entity/nginxtemplate/structs.go
Normal file
@ -0,0 +1,15 @@
|
||||
package nginxtemplate
|
||||
|
||||
import (
|
||||
"npm/internal/model"
|
||||
)
|
||||
|
||||
// ListResponse is the JSON response for this list
|
||||
type ListResponse struct {
|
||||
Total int `json:"total"`
|
||||
Offset int `json:"offset"`
|
||||
Limit int `json:"limit"`
|
||||
Sort []model.Sort `json:"sort"`
|
||||
Filter []model.Filter `json:"filter,omitempty"`
|
||||
Items []Model `json:"items,omitempty"`
|
||||
}
|
Reference in New Issue
Block a user