Neue Domain kann per 301 auf eine andere Domain/URL umgeleitet werden, statt auf ein Backend zu routen (Use-Case: kvs.netcell-it.de → https://zkm.netcell-it.de, inkl. HTTPS). Variante (a): immer auf Ziel-Root (`redirect location`), pfad- unabhängig. - Migration 0043: domains.redirect_to text NOT NULL DEFAULT '' - Model + domains-Service (SELECT/INSERT/UPDATE/scan) - HAProxy-Generator: buildRedirectTo() sanitisiert (nur http(s), kein Whitespace/Quotes → sonst kein Redirect statt kaputter Config); Template emittiert `http-request redirect location <url> code 301 if hdr(host)`. Terminiert vor use_backend → Redirect-Domain routet auf kein Backend. http→https läuft über den vorhandenen :80-Redirect (zwei Hops, inkl. TLS). - UI: Feld „Weiterleitung (301) nach" im Domain-Formular (de/en) - Tests: Render-Zeile + buildRedirectTo-Sanitisierung Hinweis: Die Redirect-Domain braucht weiterhin ein eigenes TLS-Zert (ACME). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
149 lines
4.4 KiB
Go
149 lines
4.4 KiB
Go
// Package domains implements CRUD against the `domains` table.
|
|
package domains
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.netcell-it.de/projekte/edgeguard-native/internal/models"
|
|
)
|
|
|
|
var ErrNotFound = errors.New("domain not found")
|
|
|
|
type Repo struct {
|
|
Pool *pgxpool.Pool
|
|
}
|
|
|
|
func New(pool *pgxpool.Pool) *Repo { return &Repo{Pool: pool} }
|
|
|
|
const baseSelect = `
|
|
SELECT id, name, active, primary_backend_id, http_to_https,
|
|
hsts_enabled, hsts_max_age, hsts_subdomains, hsts_preload,
|
|
maintenance_mode, maintenance_message, www_redirect,
|
|
rate_limit_rps, max_body_kb, disable_h3,
|
|
notes, redirect_to, created_at, updated_at
|
|
FROM domains
|
|
`
|
|
|
|
func (r *Repo) List(ctx context.Context) ([]models.Domain, error) {
|
|
rows, err := r.Pool.Query(ctx, baseSelect+" ORDER BY name ASC")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := make([]models.Domain, 0, 16)
|
|
for rows.Next() {
|
|
d, err := scanDomain(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, *d)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (r *Repo) Get(ctx context.Context, id int64) (*models.Domain, error) {
|
|
row := r.Pool.QueryRow(ctx, baseSelect+" WHERE id = $1", id)
|
|
d, err := scanDomain(row)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
func (r *Repo) Create(ctx context.Context, d models.Domain) (*models.Domain, error) {
|
|
if d.HSTSMaxAge == 0 {
|
|
d.HSTSMaxAge = 31536000
|
|
}
|
|
row := r.Pool.QueryRow(ctx, `
|
|
INSERT INTO domains (name, active, primary_backend_id, http_to_https,
|
|
hsts_enabled, hsts_max_age, hsts_subdomains, hsts_preload,
|
|
maintenance_mode, maintenance_message, www_redirect,
|
|
rate_limit_rps, max_body_kb, disable_h3, notes, redirect_to)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
|
|
RETURNING id, name, active, primary_backend_id, http_to_https,
|
|
hsts_enabled, hsts_max_age, hsts_subdomains, hsts_preload,
|
|
maintenance_mode, maintenance_message, www_redirect,
|
|
rate_limit_rps, max_body_kb, disable_h3,
|
|
notes, redirect_to, created_at, updated_at`,
|
|
d.Name, d.Active, d.PrimaryBackendID, d.HTTPToHTTPS,
|
|
d.HSTSEnabled, d.HSTSMaxAge, d.HSTSSubdomains, d.HSTSPreload,
|
|
d.MaintenanceMode, d.MaintenanceMessage, d.WWWRedirect,
|
|
d.RateLimitRPS, d.MaxBodyKB, d.DisableH3, d.Notes, d.RedirectTo)
|
|
return scanDomain(row)
|
|
}
|
|
|
|
func (r *Repo) Update(ctx context.Context, id int64, d models.Domain) (*models.Domain, error) {
|
|
if d.HSTSMaxAge == 0 {
|
|
d.HSTSMaxAge = 31536000
|
|
}
|
|
row := r.Pool.QueryRow(ctx, `
|
|
UPDATE domains SET
|
|
name = $1,
|
|
active = $2,
|
|
primary_backend_id = $3,
|
|
http_to_https = $4,
|
|
hsts_enabled = $5,
|
|
hsts_max_age = $6,
|
|
hsts_subdomains = $7,
|
|
hsts_preload = $8,
|
|
maintenance_mode = $9,
|
|
maintenance_message = $10,
|
|
www_redirect = $11,
|
|
rate_limit_rps = $12,
|
|
max_body_kb = $13,
|
|
disable_h3 = $14,
|
|
notes = $15,
|
|
redirect_to = $16,
|
|
updated_at = NOW()
|
|
WHERE id = $17
|
|
RETURNING id, name, active, primary_backend_id, http_to_https,
|
|
hsts_enabled, hsts_max_age, hsts_subdomains, hsts_preload,
|
|
maintenance_mode, maintenance_message, www_redirect,
|
|
rate_limit_rps, max_body_kb, disable_h3,
|
|
notes, redirect_to, created_at, updated_at`,
|
|
d.Name, d.Active, d.PrimaryBackendID, d.HTTPToHTTPS,
|
|
d.HSTSEnabled, d.HSTSMaxAge, d.HSTSSubdomains, d.HSTSPreload,
|
|
d.MaintenanceMode, d.MaintenanceMessage, d.WWWRedirect,
|
|
d.RateLimitRPS, d.MaxBodyKB, d.DisableH3, d.Notes, d.RedirectTo, id)
|
|
out, err := scanDomain(row)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (r *Repo) Delete(ctx context.Context, id int64) error {
|
|
tag, err := r.Pool.Exec(ctx, `DELETE FROM domains WHERE id = $1`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func scanDomain(row interface{ Scan(...any) error }) (*models.Domain, error) {
|
|
var d models.Domain
|
|
if err := row.Scan(
|
|
&d.ID, &d.Name, &d.Active, &d.PrimaryBackendID, &d.HTTPToHTTPS,
|
|
&d.HSTSEnabled, &d.HSTSMaxAge, &d.HSTSSubdomains, &d.HSTSPreload,
|
|
&d.MaintenanceMode, &d.MaintenanceMessage, &d.WWWRedirect,
|
|
&d.RateLimitRPS, &d.MaxBodyKB, &d.DisableH3,
|
|
&d.Notes, &d.RedirectTo, &d.CreatedAt, &d.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
return &d, nil
|
|
}
|