Use upstream in host config

This commit is contained in:
Jamie Curnow 2023-01-06 11:42:02 +10:00
parent bc6825c148
commit 17a108f75f
6 changed files with 49 additions and 25 deletions

View File

@ -156,9 +156,9 @@ INSERT INTO `nginx_template` (
{{#unless Host.IsDisabled}} {{#unless Host.IsDisabled}}
server { server {
set $forward_scheme {{Host.ForwardScheme}}; set $forward_scheme {{Host.ForwardScheme}} http; # todo
set $server ""{{Host.ForwardHost}}""; set $server ""{{Host.ForwardHost}}""; # todo
set $port {{Host.ForwardPort}}; set $port {{Host.ForwardPort}} 80; # todo
{{#if Config.Ipv4}} {{#if Config.Ipv4}}
listen 80; listen 80;
@ -168,11 +168,13 @@ server {
{{/if}} {{/if}}
{{#if Certificate.ID}} {{#if Certificate.ID}}
{{#if Config.Ipv4}}
listen 443 ssl {{#if Host.HTTP2Support}}http2{{/if}}; listen 443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}} {{/if}}
{{#if Config.Ipv6}} {{#if Config.Ipv6}}
listen [::]:443 ssl {{#if Host.HTTP2Support}}http2{{/if}}; listen [::]:443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}} {{/if}}
{{/if}}
server_name {{#each Host.DomainNames}}{{this}} {{/each}}; server_name {{#each Host.DomainNames}}{{this}} {{/each}};
@ -222,6 +224,8 @@ server {
# default location: # default location:
location / { location / {
proxy_http_version 1.1;
{{#if Host.AccessListID}} {{#if Host.AccessListID}}
# Authorization # Authorization
auth_basic ""Authorization required""; auth_basic ""Authorization required"";
@ -245,11 +249,22 @@ server {
{{#if Host.AllowWebsocketUpgrade}} {{#if Host.AllowWebsocketUpgrade}}
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection; proxy_set_header Connection $http_connection;
proxy_http_version 1.1;
{{/if}} {{/if}}
# Proxy! # Proxy!
include conf.d/include/proxy.conf; add_header X-Served-By $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
{{#if Upstream.ID}}
# upstream
proxy_pass $forward_scheme://npm_upstream_{{Upstream.ID}};
{{else}}
# proxy
proxy_pass $forward_scheme://$server:$port;
{{/if}}
} }
# Legacy Custom Configuration # Legacy Custom Configuration

View File

@ -55,7 +55,7 @@ type Model struct {
Certificate *certificate.Model `json:"certificate,omitempty"` Certificate *certificate.Model `json:"certificate,omitempty"`
NginxTemplate *nginxtemplate.Model `json:"nginx_template,omitempty"` NginxTemplate *nginxtemplate.Model `json:"nginx_template,omitempty"`
User *user.Model `json:"user,omitempty"` User *user.Model `json:"user,omitempty"`
Upstream *upstream.Model `json:"upstream,omitempty"` Upstream upstream.Model `json:"upstream,omitempty"`
} }
func (m *Model) getByQuery(query string, params []interface{}) error { func (m *Model) getByQuery(query string, params []interface{}) error {
@ -119,7 +119,7 @@ func (m *Model) Expand(items []string) error {
if m.UpstreamID > 0 { if m.UpstreamID > 0 {
var u upstream.Model var u upstream.Model
u, err = upstream.GetByID(m.UpstreamID) u, err = upstream.GetByID(m.UpstreamID)
m.Upstream = &u m.Upstream = u
} }
if util.SliceContainsItem(items, "user") && m.ID > 0 { if util.SliceContainsItem(items, "user") && m.ID > 0 {
@ -140,6 +140,12 @@ func (m *Model) Expand(items []string) error {
m.NginxTemplate = &templ m.NginxTemplate = &templ
} }
if util.SliceContainsItem(items, "upstream") && m.UpstreamID > 0 {
var ups upstream.Model
ups, err = upstream.GetByID(m.UpstreamID)
m.Upstream = ups
}
return err return err
} }
@ -171,6 +177,7 @@ func (m *Model) GetTemplate() Template {
Status: m.Status, Status: m.Status,
ErrorMessage: m.ErrorMessage, ErrorMessage: m.ErrorMessage,
IsDisabled: m.IsDisabled, IsDisabled: m.IsDisabled,
Upstream: m.Upstream,
} }
return t return t

View File

@ -1,13 +1,6 @@
package host package host
type TemplateUpstream struct { import "npm/internal/entity/upstream"
Hostname string
Port int
BalanceMethod string
MaxFails int
FailTimeout int
AdvancedConfig string
}
// Template is the model given to the template parser, converted from the Model // Template is the model given to the template parser, converted from the Model
type Template struct { type Template struct {
@ -34,5 +27,5 @@ type Template struct {
AdvancedConfig string AdvancedConfig string
Status string Status string
ErrorMessage string ErrorMessage string
Upstreams []TemplateUpstream Upstream upstream.Model
} }

View File

@ -14,7 +14,7 @@ import (
// ConfigureHost will attempt to write nginx conf and reload nginx // ConfigureHost will attempt to write nginx conf and reload nginx
func ConfigureHost(h host.Model) error { func ConfigureHost(h host.Model) error {
// nolint: errcheck, gosec // nolint: errcheck, gosec
h.Expand([]string{"certificate", "nginxtemplate"}) h.Expand([]string{"certificate", "nginxtemplate", "upstream"})
var certificateTemplate certificate.Template var certificateTemplate certificate.Template
if h.Certificate != nil { if h.Certificate != nil {
@ -22,10 +22,15 @@ func ConfigureHost(h host.Model) error {
} }
data := TemplateData{ data := TemplateData{
Certificate: certificateTemplate,
ConfDir: fmt.Sprintf("%s/nginx/hosts", config.Configuration.DataFolder), ConfDir: fmt.Sprintf("%s/nginx/hosts", config.Configuration.DataFolder),
Config: Config{ // todo
Ipv4: true,
Ipv6: false,
},
DataDir: config.Configuration.DataFolder, DataDir: config.Configuration.DataFolder,
Host: h.GetTemplate(), Host: h.GetTemplate(),
Certificate: certificateTemplate, Upstream: h.Upstream,
} }
filename := fmt.Sprintf("%s/host_%d.conf", data.ConfDir, h.ID) filename := fmt.Sprintf("%s/host_%d.conf", data.ConfDir, h.ID)

View File

@ -92,7 +92,7 @@ server {
Certificate: test.cert.GetTemplate(), Certificate: test.cert.GetTemplate(),
} }
output, err := generateHostConfig(template, templateData) output, err := renderTemplate(template, templateData)
assert.Equal(t, test.want.err, err) assert.Equal(t, test.want.err, err)
assert.Equal(t, test.want.output, output) assert.Equal(t, test.want.output, output)
}) })

View File

@ -13,25 +13,29 @@ import (
"github.com/aymerick/raymond" "github.com/aymerick/raymond"
) )
type Config struct {
Ipv4 bool
Ipv6 bool
}
// TemplateData is a struct // TemplateData is a struct
type TemplateData struct { type TemplateData struct {
ConfDir string ConfDir string
Config Config
DataDir string DataDir string
Host host.Template Host host.Template
Certificate certificate.Template Certificate certificate.Template
Upstream upstream.Model Upstream upstream.Model
} }
func generateHostConfig(template string, data TemplateData) (string, error) { func renderTemplate(template string, data TemplateData) (string, error) {
logger.Debug("Rendering Template - Template: %s", template) logger.Debug("Rendering Template - Template: %s", template)
logger.Debug("Rendering Template - Data: %+v", data) logger.Debug("Rendering Template - Data: %+v", data)
return raymond.Render(template, data) return raymond.Render(template, data)
// todo: apply some post processing to this config, stripe trailing whitespace from lines and then remove groups of 2+ \n's so the config looks nicer
} }
func writeTemplate(filename, template string, data TemplateData) error { func writeTemplate(filename, template string, data TemplateData) error {
output, err := generateHostConfig(template, data) output, err := renderTemplate(template, data)
if err != nil { if err != nil {
output = fmt.Sprintf("# Template Error: %s", err.Error()) output = fmt.Sprintf("# Template Error: %s", err.Error())
} }