forked from cristianoliveira/ergo
/
config.go
67 lines (56 loc) · 1.14 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package proxy
import (
"bufio"
"fmt"
"os"
"regexp"
)
type Service struct {
Name string
Url string
}
type Config struct {
Port string
Domain string
UrlPattern string
Verbose bool
Services []Service
}
func (c *Config) GetService(host string) *Service {
domainPattern := regexp.MustCompile(`(\w*\:\/\/)?(.+)` + c.Domain)
parts := domainPattern.FindAllString(host, -1)
for _, s := range c.Services {
if len(parts) > 0 && s.Name+c.Domain == parts[0] {
return &s
}
}
return nil
}
func NewConfig() *Config {
return &Config{
Port: "2000",
Domain: ".dev",
UrlPattern: `.*\.dev$`,
Services: nil,
}
}
func LoadConfig(filepath string) []Service {
file, e := os.Open(filepath)
defer file.Close()
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
services := []Service{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
declaration := regexp.MustCompile(`(\S+)`)
config := declaration.FindAllString(line, -1)
if config != nil {
name, url := config[0], config[1]
services = append(services, Service{Name: name, Url: url})
}
}
return services
}