mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-06-18 18:16:26 +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,25 +0,0 @@
|
||||
package certificate
|
||||
|
||||
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)
|
||||
}
|
@ -1,142 +1,54 @@
|
||||
package certificate
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"npm/internal/database"
|
||||
"npm/internal/entity"
|
||||
"npm/internal/errors"
|
||||
"npm/internal/jobqueue"
|
||||
"npm/internal/logger"
|
||||
"npm/internal/model"
|
||||
|
||||
"github.com/rotisserie/eris"
|
||||
)
|
||||
|
||||
// GetByID finds a row 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 row from this model
|
||||
func Create(certificate *Model) (int, error) {
|
||||
if certificate.ID != 0 {
|
||||
return 0, eris.New("Cannot create certificate when model already has an ID")
|
||||
}
|
||||
|
||||
certificate.Touch(true)
|
||||
|
||||
db := database.GetInstance()
|
||||
// nolint: gosec
|
||||
result, err := db.NamedExec(`INSERT INTO `+fmt.Sprintf("`%s`", tableName)+` (
|
||||
created_on,
|
||||
modified_on,
|
||||
user_id,
|
||||
type,
|
||||
certificate_authority_id,
|
||||
dns_provider_id,
|
||||
name,
|
||||
domain_names,
|
||||
expires_on,
|
||||
status,
|
||||
error_message,
|
||||
meta,
|
||||
is_ecc,
|
||||
is_deleted
|
||||
) VALUES (
|
||||
:created_on,
|
||||
:modified_on,
|
||||
:user_id,
|
||||
:type,
|
||||
:certificate_authority_id,
|
||||
:dns_provider_id,
|
||||
:name,
|
||||
:domain_names,
|
||||
:expires_on,
|
||||
:status,
|
||||
:error_message,
|
||||
:meta,
|
||||
:is_ecc,
|
||||
:is_deleted
|
||||
)`, certificate)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
last, lastErr := result.LastInsertId()
|
||||
if lastErr != nil {
|
||||
return 0, lastErr
|
||||
}
|
||||
|
||||
return int(last), nil
|
||||
}
|
||||
|
||||
// Update will Update a Auth from this model
|
||||
func Update(certificate *Model) error {
|
||||
if certificate.ID == 0 {
|
||||
return eris.New("Cannot update certificate when model doesn't have an ID")
|
||||
}
|
||||
|
||||
certificate.Touch(false)
|
||||
|
||||
db := database.GetInstance()
|
||||
// nolint: gosec
|
||||
_, err := db.NamedExec(`UPDATE `+fmt.Sprintf("`%s`", tableName)+` SET
|
||||
created_on = :created_on,
|
||||
modified_on = :modified_on,
|
||||
type = :type,
|
||||
user_id = :user_id,
|
||||
certificate_authority_id = :certificate_authority_id,
|
||||
dns_provider_id = :dns_provider_id,
|
||||
name = :name,
|
||||
domain_names = :domain_names,
|
||||
expires_on = :expires_on,
|
||||
status = :status,
|
||||
error_message = :error_message,
|
||||
meta = :meta,
|
||||
is_ecc = :is_ecc,
|
||||
is_deleted = :is_deleted
|
||||
WHERE id = :id`, certificate)
|
||||
|
||||
return err
|
||||
// GetByStatus will select rows that are ready for requesting
|
||||
func GetByStatus(status string) ([]Model, error) {
|
||||
items := make([]Model, 0)
|
||||
db := database.GetDB()
|
||||
result := db.
|
||||
Joins("INNER JOIN certificate_authority ON certificate_authority.id = certificate.certificate_authority_id AND certificate_authority.is_deleted = ?", 0).
|
||||
Where("type IN ?", []string{"http", "dns"}).
|
||||
Where("status = ?", status).
|
||||
Where("certificate_authority_id > ?", 0).
|
||||
Find(&items)
|
||||
return items, result.Error
|
||||
}
|
||||
|
||||
// List will return a list of certificates
|
||||
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: "name",
|
||||
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 {
|
||||
@ -148,7 +60,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (Lis
|
||||
}
|
||||
}
|
||||
|
||||
result = ListResponse{
|
||||
result = entity.ListResponse{
|
||||
Items: items,
|
||||
Total: totalRows,
|
||||
Limit: pageInfo.Limit,
|
||||
@ -160,33 +72,6 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (Lis
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetByStatus will select rows that are ready for requesting
|
||||
func GetByStatus(status string) ([]Model, error) {
|
||||
models := make([]Model, 0)
|
||||
db := database.GetInstance()
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
SELECT
|
||||
t.*
|
||||
FROM "%s" t
|
||||
INNER JOIN "certificate_authority" c ON c."id" = t."certificate_authority_id"
|
||||
WHERE
|
||||
t."type" IN ("http", "dns") AND
|
||||
t."status" = ? AND
|
||||
t."certificate_authority_id" > 0 AND
|
||||
t."is_deleted" = 0
|
||||
`, tableName)
|
||||
|
||||
params := []interface{}{StatusReady}
|
||||
err := db.Select(&models, query, params...)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
logger.Error("GetByStatusError", err)
|
||||
logger.Debug("Query: %s -- %+v", query, params)
|
||||
}
|
||||
|
||||
return models, err
|
||||
}
|
||||
|
||||
// AddPendingJobs is intended to be used at startup to add
|
||||
// anything pending to the JobQueue just once, based on
|
||||
// the database row status
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"npm/internal/acme"
|
||||
"npm/internal/config"
|
||||
"npm/internal/database"
|
||||
"npm/internal/entity"
|
||||
"npm/internal/entity/certificateauthority"
|
||||
"npm/internal/entity/dnsprovider"
|
||||
"npm/internal/entity/user"
|
||||
@ -22,8 +23,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
tableName = "certificate"
|
||||
|
||||
// TypeCustom custom cert type
|
||||
TypeCustom = "custom"
|
||||
// TypeHTTP http cert type
|
||||
@ -43,54 +42,40 @@ const (
|
||||
StatusProvided = "provided"
|
||||
)
|
||||
|
||||
// Model is the user model
|
||||
// Model is the 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"`
|
||||
ExpiresOn types.NullableDBDate `json:"expires_on" db:"expires_on" filter:"expires_on,integer"`
|
||||
Type string `json:"type" db:"type" filter:"type,string"`
|
||||
UserID int `json:"user_id" db:"user_id" filter:"user_id,integer"`
|
||||
CertificateAuthorityID int `json:"certificate_authority_id" db:"certificate_authority_id" filter:"certificate_authority_id,integer"`
|
||||
DNSProviderID int `json:"dns_provider_id" db:"dns_provider_id" filter:"dns_provider_id,integer"`
|
||||
Name string `json:"name" db:"name" filter:"name,string"`
|
||||
DomainNames types.JSONB `json:"domain_names" db:"domain_names" filter:"domain_names,string"`
|
||||
Status string `json:"status" db:"status" filter:"status,string"`
|
||||
ErrorMessage string `json:"error_message" db:"error_message" filter:"error_message,string"`
|
||||
Meta types.JSONB `json:"-" db:"meta"`
|
||||
IsECC bool `json:"is_ecc" db:"is_ecc" filter:"is_ecc,bool"`
|
||||
IsDeleted bool `json:"is_deleted,omitempty" db:"is_deleted"`
|
||||
entity.ModelBase
|
||||
ExpiresOn types.NullableDBDate `json:"expires_on" gorm:"column:expires_on" filter:"expires_on,integer"`
|
||||
Type string `json:"type" gorm:"column:type" filter:"type,string"`
|
||||
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
|
||||
CertificateAuthorityID uint `json:"certificate_authority_id" gorm:"column:certificate_authority_id" filter:"certificate_authority_id,integer"`
|
||||
DNSProviderID uint `json:"dns_provider_id" gorm:"column:dns_provider_id" filter:"dns_provider_id,integer"`
|
||||
Name string `json:"name" gorm:"column:name" filter:"name,string"`
|
||||
DomainNames types.JSONB `json:"domain_names" gorm:"column:domain_names" filter:"domain_names,string"`
|
||||
Status string `json:"status" gorm:"column:status" filter:"status,string"`
|
||||
ErrorMessage string `json:"error_message" gorm:"column:error_message" filter:"error_message,string"`
|
||||
Meta types.JSONB `json:"-" gorm:"column:meta"`
|
||||
IsECC bool `json:"is_ecc" gorm:"column:is_ecc" filter:"is_ecc,bool"`
|
||||
// Expansions:
|
||||
CertificateAuthority *certificateauthority.Model `json:"certificate_authority,omitempty"`
|
||||
DNSProvider *dnsprovider.Model `json:"dns_provider,omitempty"`
|
||||
User *user.Model `json:"user,omitempty"`
|
||||
CertificateAuthority *certificateauthority.Model `json:"certificate_authority,omitempty" gorm:"-"`
|
||||
DNSProvider *dnsprovider.Model `json:"dns_provider,omitempty" gorm:"-"`
|
||||
User *user.Model `json:"user,omitempty" gorm:"-"`
|
||||
}
|
||||
|
||||
func (m *Model) getByQuery(query string, params []interface{}) error {
|
||||
return database.GetByQuery(m, query, params)
|
||||
// TableName overrides the table name used by gorm
|
||||
func (Model) TableName() string {
|
||||
return "certificate"
|
||||
}
|
||||
|
||||
// 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
|
||||
func (m *Model) LoadByID(id uint) error {
|
||||
db := database.GetDB()
|
||||
result := db.First(&m, id)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// Save will save this model to the DB
|
||||
func (m *Model) Save() error {
|
||||
var err error
|
||||
|
||||
if m.UserID == 0 {
|
||||
return eris.Errorf("User ID must be specified")
|
||||
}
|
||||
@ -108,26 +93,22 @@ func (m *Model) Save() error {
|
||||
// ensure name is trimmed of whitespace
|
||||
m.Name = strings.TrimSpace(m.Name)
|
||||
|
||||
if m.ID == 0 {
|
||||
m.ID, err = Create(m)
|
||||
} else {
|
||||
err = Update(m)
|
||||
}
|
||||
|
||||
return err
|
||||
db := database.GetDB()
|
||||
result := db.Save(m)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// Delete will mark a certificate as deleted
|
||||
// Delete will mark row as deleted
|
||||
func (m *Model) Delete() bool {
|
||||
m.Touch(false)
|
||||
m.IsDeleted = true
|
||||
if err := m.Save(); err != nil {
|
||||
if m.ID == 0 {
|
||||
// Can't delete a new object
|
||||
return false
|
||||
}
|
||||
db := database.GetDB()
|
||||
result := db.Delete(m)
|
||||
return result.Error == nil
|
||||
|
||||
// todo: delete from acme.sh as well
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Validate will make sure the data given is expected. This object is a bit complicated,
|
||||
@ -304,8 +285,8 @@ func (m *Model) GetTemplate() Template {
|
||||
|
||||
return Template{
|
||||
ID: m.ID,
|
||||
CreatedOn: m.CreatedOn.Time.String(),
|
||||
ModifiedOn: m.ModifiedOn.Time.String(),
|
||||
CreatedAt: fmt.Sprintf("%d", m.CreatedAt), // todo: nice date string
|
||||
UpdatedAt: fmt.Sprintf("%d", m.UpdatedAt), // todo: nice date string
|
||||
ExpiresOn: m.ExpiresOn.AsString(),
|
||||
Type: m.Type,
|
||||
UserID: m.UserID,
|
||||
|
@ -1,15 +0,0 @@
|
||||
package certificate
|
||||
|
||||
import (
|
||||
"npm/internal/model"
|
||||
)
|
||||
|
||||
// ListResponse is the JSON response for users 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"`
|
||||
}
|
@ -2,14 +2,14 @@ package certificate
|
||||
|
||||
// Template is the model given to the template parser, converted from the Model
|
||||
type Template struct {
|
||||
ID int
|
||||
CreatedOn string
|
||||
ModifiedOn string
|
||||
ID uint
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
ExpiresOn string
|
||||
Type string
|
||||
UserID int
|
||||
CertificateAuthorityID int
|
||||
DNSProviderID int
|
||||
UserID uint
|
||||
CertificateAuthorityID uint
|
||||
DNSProviderID uint
|
||||
Name string
|
||||
DomainNames []string
|
||||
Status string
|
||||
|
Reference in New Issue
Block a user