Add some error handling to the scripted proxy functions.
This commit is contained in:
parent
1d3dad77a4
commit
7d317743bb
21
main.go
21
main.go
|
@ -69,9 +69,9 @@ func main() {
|
|||
// Read system "base" config file if it exists
|
||||
if config.System.Base != "" {
|
||||
var systemConfig Config
|
||||
path := "services/" + config.System.Base + ".json"
|
||||
fp := "services/" + config.System.Base + ".json"
|
||||
|
||||
file, err := os.ReadFile(path)
|
||||
file, err := os.ReadFile(fp)
|
||||
if err != nil {
|
||||
fmt.Println("Cannot read system base config file")
|
||||
fmt.Println(err)
|
||||
|
@ -90,7 +90,6 @@ func main() {
|
|||
|
||||
// Parse user services
|
||||
for name, service := range config.Services {
|
||||
var b strings.Builder
|
||||
s := ServiceConfig{
|
||||
Config: make(map[string]interface{}),
|
||||
}
|
||||
|
@ -99,8 +98,9 @@ func main() {
|
|||
if service.Provider == "" { service.Provider = "system" }
|
||||
|
||||
// Read service config file
|
||||
fmt.Fprintf(&b, "services/%s/%s.json", name, service.Provider)
|
||||
c, err := os.ReadFile(b.String())
|
||||
fp := fmt.Sprintf("services/%s/%s.json",
|
||||
name, service.Provider)
|
||||
c, err := os.ReadFile(fp)
|
||||
if err != nil {
|
||||
switch err := err.(type) {
|
||||
case *os.PathError:
|
||||
|
@ -143,7 +143,11 @@ func main() {
|
|||
fmt.Println("Error: No port defined for proxy to", s.Proxy)
|
||||
return
|
||||
}
|
||||
AddProxy(s.Proxy, ports[0])
|
||||
err = AddProxy(s.Proxy, ports[0])
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -157,12 +161,11 @@ func main() {
|
|||
// Generate service config templates
|
||||
for name, s := range config.Services {
|
||||
for _, f := range s.ConfigFiles {
|
||||
var b strings.Builder
|
||||
f := strings.Split(f, ":")
|
||||
|
||||
// Read service template file
|
||||
fmt.Fprintf(&b, "services/%s/%s", name, f[0])
|
||||
t, err := os.ReadFile(b.String())
|
||||
fp := fmt.Sprintf("services/%s/%s", name, f[0])
|
||||
t, err := os.ReadFile(fp)
|
||||
if err != nil {
|
||||
fmt.Println("No template for service: ", err)
|
||||
return
|
||||
|
|
42
scripted.go
42
scripted.go
|
@ -1,10 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Scripted proxy errors
|
||||
var ErrConversion = errors.New("port is not a number")
|
||||
var ErrDuplicate = errors.New("duplicate entry")
|
||||
|
||||
type ProxyError struct {
|
||||
Domain string
|
||||
Port string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *ProxyError) Error() string {
|
||||
return fmt.Sprintf("Proxy for %s:%s: %s",
|
||||
e.Domain, e.Port, e.Err.Error())
|
||||
}
|
||||
|
||||
func (e *ProxyError) Unwrap() error { return e.Err }
|
||||
|
||||
func conversionError(domain, port string) *ProxyError {
|
||||
return &ProxyError{domain, port, ErrConversion}
|
||||
}
|
||||
|
||||
func duplicateError(domain, port string) *ProxyError {
|
||||
return &ProxyError{domain, port, ErrDuplicate}
|
||||
}
|
||||
|
||||
type Proxy struct {
|
||||
Domain string
|
||||
Port int
|
||||
|
@ -19,26 +45,26 @@ func ScriptedInit() {
|
|||
func GetProxy() ([]Proxy) {
|
||||
var p []Proxy
|
||||
for port, domain := range proxyList {
|
||||
p = append(p, Proxy{Domain: domain, Port: port})
|
||||
p = append(p, Proxy{domain, port})
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func AddProxy(domain string, port string) {
|
||||
func AddProxy(domain, port string) (error) {
|
||||
p, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Printf("Error: proxy for %s, port is not a number: %s\n", domain, port)
|
||||
return
|
||||
return conversionError(domain, port)
|
||||
}
|
||||
|
||||
conflict, exists := proxyList[p]
|
||||
if exists == true {
|
||||
fmt.Println("Error: Conflicting proxy settings:")
|
||||
fmt.Printf(" %s:%s\n", conflict, p)
|
||||
fmt.Printf(" %s:%s\n", domain, p)
|
||||
return
|
||||
fmt.Printf(" %s:%d\n", conflict, p)
|
||||
fmt.Printf(" %s:%d\n", domain, p)
|
||||
return duplicateError(domain, port)
|
||||
}
|
||||
|
||||
proxyList[p] = domain
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue