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