mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-06-19 02:26:27 +00:00
Convert db backend to use Gorm, with basis for support
for Mysql and Postgres in addition to existing Sqlite
This commit is contained in:
@ -1,181 +1,40 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"npm/internal/database"
|
||||
"npm/internal/entity"
|
||||
"npm/internal/errors"
|
||||
"npm/internal/logger"
|
||||
"npm/internal/model"
|
||||
|
||||
"github.com/rotisserie/eris"
|
||||
)
|
||||
|
||||
// GetByID finds a Host by ID
|
||||
func GetByID(id int) (Model, error) {
|
||||
func GetByID(id uint) (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, eris.New("Cannot create host when model already has an ID")
|
||||
}
|
||||
|
||||
host.Touch(true)
|
||||
|
||||
db := database.GetInstance()
|
||||
// nolint: gosec
|
||||
result, err := db.NamedExec(`INSERT INTO `+tableName+` (
|
||||
created_on,
|
||||
modified_on,
|
||||
user_id,
|
||||
type,
|
||||
nginx_template_id,
|
||||
listen_interface,
|
||||
domain_names,
|
||||
upstream_id,
|
||||
proxy_scheme,
|
||||
proxy_host,
|
||||
proxy_port,
|
||||
certificate_id,
|
||||
access_list_id,
|
||||
ssl_forced,
|
||||
caching_enabled,
|
||||
block_exploits,
|
||||
allow_websocket_upgrade,
|
||||
http2_support,
|
||||
hsts_enabled,
|
||||
hsts_subdomains,
|
||||
paths,
|
||||
advanced_config,
|
||||
status,
|
||||
error_message,
|
||||
is_disabled,
|
||||
is_deleted
|
||||
) VALUES (
|
||||
:created_on,
|
||||
:modified_on,
|
||||
:user_id,
|
||||
:type,
|
||||
:nginx_template_id,
|
||||
:listen_interface,
|
||||
:domain_names,
|
||||
:upstream_id,
|
||||
:proxy_scheme,
|
||||
:proxy_host,
|
||||
:proxy_port,
|
||||
:certificate_id,
|
||||
:access_list_id,
|
||||
:ssl_forced,
|
||||
:caching_enabled,
|
||||
:block_exploits,
|
||||
:allow_websocket_upgrade,
|
||||
:http2_support,
|
||||
:hsts_enabled,
|
||||
:hsts_subdomains,
|
||||
:paths,
|
||||
:advanced_config,
|
||||
:status,
|
||||
:error_message,
|
||||
:is_disabled,
|
||||
:is_deleted
|
||||
)`, host)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
last, lastErr := result.LastInsertId()
|
||||
if lastErr != nil {
|
||||
return 0, lastErr
|
||||
}
|
||||
|
||||
logger.Debug("Created Host: %+v", host)
|
||||
|
||||
return int(last), nil
|
||||
}
|
||||
|
||||
// update will Update a Host from this model
|
||||
func update(host *Model) error {
|
||||
if host.ID == 0 {
|
||||
return eris.New("Cannot update host 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,
|
||||
type = :type,
|
||||
nginx_template_id = :nginx_template_id,
|
||||
listen_interface = :listen_interface,
|
||||
domain_names = :domain_names,
|
||||
upstream_id = :upstream_id,
|
||||
proxy_scheme = :proxy_scheme,
|
||||
proxy_host = :proxy_host,
|
||||
proxy_port = :proxy_port,
|
||||
certificate_id = :certificate_id,
|
||||
access_list_id = :access_list_id,
|
||||
ssl_forced = :ssl_forced,
|
||||
caching_enabled = :caching_enabled,
|
||||
block_exploits = :block_exploits,
|
||||
allow_websocket_upgrade = :allow_websocket_upgrade,
|
||||
http2_support = :http2_support,
|
||||
hsts_enabled = :hsts_enabled,
|
||||
hsts_subdomains = :hsts_subdomains,
|
||||
paths = :paths,
|
||||
advanced_config = :advanced_config,
|
||||
status = :status,
|
||||
error_message = :error_message,
|
||||
is_disabled = :is_disabled,
|
||||
is_deleted = :is_deleted
|
||||
WHERE id = :id`, host)
|
||||
|
||||
logger.Debug("Updated Host: %+v", host)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// List will return a list of hosts
|
||||
func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ListResponse, error) {
|
||||
var result ListResponse
|
||||
var exampleModel Model
|
||||
func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (entity.ListResponse, error) {
|
||||
var result entity.ListResponse
|
||||
|
||||
defaultSort := model.Sort{
|
||||
Field: "domain_names",
|
||||
Direction: "ASC",
|
||||
}
|
||||
|
||||
db := database.GetInstance()
|
||||
if db == nil {
|
||||
return result, errors.ErrDatabaseUnavailable
|
||||
}
|
||||
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters)
|
||||
|
||||
// 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
|
||||
var totalRows int64
|
||||
if res := dbo.Model(&Model{}).Count(&totalRows); res.Error != nil {
|
||||
return result, res.Error
|
||||
}
|
||||
|
||||
// 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
|
||||
if res := dbo.Find(&items); res.Error != nil {
|
||||
return result, res.Error
|
||||
}
|
||||
|
||||
if expand != nil {
|
||||
@ -187,7 +46,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (Lis
|
||||
}
|
||||
}
|
||||
|
||||
result = ListResponse{
|
||||
result = entity.ListResponse{
|
||||
Items: items,
|
||||
Total: totalRows,
|
||||
Limit: pageInfo.Limit,
|
||||
@ -201,32 +60,28 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (Lis
|
||||
|
||||
// GetUpstreamUseCount returns the number of hosts that are using
|
||||
// an upstream, and have not been deleted.
|
||||
func GetUpstreamUseCount(upstreamID int) int {
|
||||
db := database.GetInstance()
|
||||
query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE upstream_id = ? AND is_deleted = ?", tableName)
|
||||
countRow := db.QueryRowx(query, upstreamID, 0)
|
||||
var totalRows int
|
||||
queryErr := countRow.Scan(&totalRows)
|
||||
if queryErr != nil && queryErr != sql.ErrNoRows {
|
||||
logger.Debug("%s", query)
|
||||
func GetUpstreamUseCount(upstreamID uint) int64 {
|
||||
db := database.GetDB()
|
||||
|
||||
var count int64
|
||||
if result := db.Model(&Model{}).Where("upstream_id = ?", upstreamID).Count(&count); result.Error != nil {
|
||||
logger.Debug("GetUpstreamUseCount Error: %v", result.Error)
|
||||
return 0
|
||||
}
|
||||
return totalRows
|
||||
return count
|
||||
}
|
||||
|
||||
// GetCertificateUseCount returns the number of hosts that are using
|
||||
// a certificate, and have not been deleted.
|
||||
func GetCertificateUseCount(certificateID int) int {
|
||||
db := database.GetInstance()
|
||||
query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE certificate_id = ? AND is_deleted = ?", tableName)
|
||||
countRow := db.QueryRowx(query, certificateID, 0)
|
||||
var totalRows int
|
||||
queryErr := countRow.Scan(&totalRows)
|
||||
if queryErr != nil && queryErr != sql.ErrNoRows {
|
||||
logger.Debug("%s", query)
|
||||
func GetCertificateUseCount(certificateID uint) int64 {
|
||||
db := database.GetDB()
|
||||
|
||||
var count int64
|
||||
if result := db.Model(&Model{}).Where("certificate_id = ?", certificateID).Count(&count); result.Error != nil {
|
||||
logger.Debug("GetUpstreamUseCount Error: %v", result.Error)
|
||||
return 0
|
||||
}
|
||||
return totalRows
|
||||
return count
|
||||
}
|
||||
|
||||
// AddPendingJobs is intended to be used at startup to add
|
||||
|
Reference in New Issue
Block a user