Add basic proxy scripting support.
This commit is contained in:
parent
308889be5f
commit
a6b00f4c39
|
@ -25,7 +25,7 @@
|
||||||
"volumes": [
|
"volumes": [
|
||||||
"/data/docker/navidrome:/data",
|
"/data/docker/navidrome:/data",
|
||||||
"/data/music:/music:ro"
|
"/data/music:/music:ro"
|
||||||
]
|
],
|
||||||
},
|
},
|
||||||
"forgejo": {
|
"forgejo": {
|
||||||
"enable": "true",
|
"enable": "true",
|
||||||
|
|
68
main.go
68
main.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
@ -25,6 +26,7 @@ type SystemConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type NetworkingConfig struct {
|
type NetworkingConfig struct {
|
||||||
|
proxy map[int]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServiceConfig struct {
|
type ServiceConfig struct {
|
||||||
|
@ -36,6 +38,7 @@ type ServiceConfig struct {
|
||||||
Src string
|
Src string
|
||||||
Ports []string
|
Ports []string
|
||||||
Volumes []string
|
Volumes []string
|
||||||
|
Proxy string
|
||||||
|
|
||||||
Provider string
|
Provider string
|
||||||
Packages []string
|
Packages []string
|
||||||
|
@ -84,10 +87,15 @@ func main() {
|
||||||
mergo.Merge(&config, systemConfig, mergo.WithOverride)
|
mergo.Merge(&config, systemConfig, mergo.WithOverride)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse services
|
// Initialize any needed maps in the config
|
||||||
|
config.Networking.proxy = make(map[int]string)
|
||||||
|
|
||||||
|
// Parse user services
|
||||||
for name, service := range config.Services {
|
for name, service := range config.Services {
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
var s ServiceConfig
|
s := ServiceConfig{
|
||||||
|
Config: make(map[string]interface{}),
|
||||||
|
}
|
||||||
|
|
||||||
// If no provider specified, try system
|
// If no provider specified, try system
|
||||||
if service.Provider == "" { service.Provider = "system" }
|
if service.Provider == "" { service.Provider = "system" }
|
||||||
|
@ -101,11 +109,14 @@ func main() {
|
||||||
// No system config to merge
|
// No system config to merge
|
||||||
// Rely solely on the user config
|
// Rely solely on the user config
|
||||||
fmt.Println(" Warning: No system config for service: " + name)
|
fmt.Println(" Warning: No system config for service: " + name)
|
||||||
|
continue
|
||||||
default:
|
default:
|
||||||
fmt.Println("Error reading service config: ", err)
|
fmt.Println("Error reading service config: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
{
|
||||||
// Parse service config
|
// Parse service config
|
||||||
err = hjson.Unmarshal(c, &s)
|
err = hjson.Unmarshal(c, &s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -114,13 +125,62 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mergo.Merge(&s, service, mergo.WithOverride)
|
mergo.Merge(&s, service, mergo.WithOverride)
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Initialize the config map if it wasn't already
|
||||||
|
if s.Config == map[string]interface{} {
|
||||||
|
s.Config = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
config.Services[name] = s
|
config.Services[name] = s
|
||||||
|
|
||||||
// Add service packages to the global list
|
// Add service packages to the global list
|
||||||
config.System.Packages = append(config.System.Packages, s.Packages...)
|
config.System.Packages = append(config.System.Packages, s.Packages...)
|
||||||
|
|
||||||
|
// If a proxy definition exists, append it
|
||||||
|
if s.Proxy != "" {
|
||||||
|
ports := strings.Split(s.Ports[0], ":")
|
||||||
|
if len(ports) == 0 {
|
||||||
|
fmt.Println("Error: No port defined for proxy to", s.Proxy)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
port, err := strconv.Atoi(ports[0])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Printf("Error: proxy for %s has an invalid port definition: %s\n", s.Proxy, ports)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conflict, exists := config.Networking.proxy[port]
|
||||||
|
if exists == true {
|
||||||
|
fmt.Println("Error: Conflicting proxy settings:")
|
||||||
|
fmt.Printf(" %s:%s\n", conflict, port)
|
||||||
|
fmt.Printf(" %s:%s\n", s.Proxy, port)
|
||||||
|
}
|
||||||
|
|
||||||
|
config.Networking.proxy[port] = s.Proxy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle any special case (scripted) configs
|
||||||
|
{
|
||||||
|
// Expand Networking.proxy to Services.proxy.Config.scripted_proxy
|
||||||
|
type proxy struct {
|
||||||
|
Domain string
|
||||||
|
Port int
|
||||||
|
}
|
||||||
|
var p []proxy
|
||||||
|
for port, domain := range config.Networking.proxy {
|
||||||
|
p = append(p, proxy{Domain: domain, Port: port})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate service config files
|
config.Services["proxy"].Config["scripted_proxy"] = p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate service config templates
|
||||||
|
for name, s := range config.Services {
|
||||||
for _, f := range s.ConfigFiles {
|
for _, f := range s.ConfigFiles {
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
f := strings.Split(f, ":")
|
f := strings.Split(f, ":")
|
||||||
|
|
|
@ -4,7 +4,14 @@
|
||||||
|
|
||||||
# https://caddyserver.com/docs/caddyfile/patterns
|
# https://caddyserver.com/docs/caddyfile/patterns
|
||||||
{{range .Config.proxy}}
|
{{range .Config.proxy}}
|
||||||
{{.domain}} {
|
{{.Domain}} {
|
||||||
reverse_proxy localhost:{{.port}}
|
reverse_proxy localhost:{{.Port}}
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
# Proxied services
|
||||||
|
{{range .Config.scripted_proxy}}
|
||||||
|
{{.Domain}} {
|
||||||
|
reverse_proxy localhost:{{.Port}}
|
||||||
}
|
}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
Loading…
Reference in a new issue