Simplify host configuration

This commit is contained in:
Trevor Slocum 2020-10-30 13:36:55 -07:00
parent 8aec55c459
commit 5637f27b45
3 changed files with 29 additions and 38 deletions

View file

@ -24,9 +24,7 @@ certificates:
# Hosts and paths to serve # Hosts and paths to serve
hosts: hosts:
- gemini.rocks:
name: gemini.rocks
paths:
- -
path: /sites path: /sites
root: /home/gemini.rocks/data/sites root: /home/gemini.rocks/data/sites
@ -42,9 +40,7 @@ hosts:
- -
path: / path: /
root: /home/gemini.rocks/data/home root: /home/gemini.rocks/data/home
- twins.rocketnine.space:
name: twins.rocketnine.space
paths:
- -
path: /sites path: /sites
root: /home/twins/data/sites root: /home/twins/data/sites

View file

@ -26,11 +26,6 @@ type pathConfig struct {
cmd []string cmd []string
} }
type hostConfig struct {
Name string
Paths []*pathConfig
}
type certConfig struct { type certConfig struct {
Cert string Cert string
Key string Key string
@ -41,7 +36,7 @@ type serverConfig struct {
Certificates []*certConfig Certificates []*certConfig
Hosts []*hostConfig Hosts map[string][]*pathConfig
hostname string hostname string
port int port int
@ -83,8 +78,8 @@ func readconfig(configPath string) error {
} }
} }
for _, host := range config.Hosts { for _, paths := range config.Hosts {
for _, serve := range host.Paths { for _, serve := range paths {
if serve.Path == "" { if serve.Path == "" {
log.Fatal("path must be specified in serve entry") log.Fatal("path must be specified in serve entry")
} else if (serve.Root != "" && (serve.Proxy != "" || serve.Command != "")) || } else if (serve.Root != "" && (serve.Proxy != "" || serve.Command != "")) ||

View file

@ -261,13 +261,13 @@ func handleConn(c net.Conn) {
} }
var matchedHost bool var matchedHost bool
for _, host := range config.Hosts { for hostname := range config.Hosts {
if requestHostname != host.Name { if requestHostname != hostname {
continue continue
} }
matchedHost = true matchedHost = true
for _, serve := range host.Paths { for _, serve := range config.Hosts[hostname] {
if serve.Proxy != "" { if serve.Proxy != "" {
if serve.r != nil && serve.r.Match(pathBytes) { if serve.r != nil && serve.r.Match(pathBytes) {
serveProxy(c, requestData, serve.Proxy) serveProxy(c, requestData, serve.Proxy)