-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (75 loc) · 2.14 KB
/
main.go
File metadata and controls
85 lines (75 loc) · 2.14 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/spf13/viper"
)
//var host []map[interface{}]interface{}
var host []Hosts
func main() {
config := config()
var server = http.NewServeMux()
for i := 0; i < len(config)-1; i++ {
webContent, err := ioutil.ReadFile(config[i].file)
if err != nil {
log.Fatalln(err)
}
server = http.NewServeMux()
server.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(webContent)
})
if config[i].tls {
go http.ListenAndServeTLS(config[i].ipaddress+":"+fmt.Sprint(config[i].port), config[i].cert, config[i].key, server)
} else {
go http.ListenAndServe(config[i].ipaddress+":"+fmt.Sprint(config[i].port), server)
}
}
lastConfigItem := len(config) - 1
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
webContent, err := ioutil.ReadFile(config[lastConfigItem].file)
if err != nil {
log.Fatalln(err)
}
rw.Write(webContent)
})
if config[lastConfigItem].tls {
http.ListenAndServeTLS(config[lastConfigItem].ipaddress+":"+fmt.Sprint(config[lastConfigItem].port), config[lastConfigItem].cert, config[lastConfigItem].key, nil)
} else {
http.ListenAndServe(config[lastConfigItem].ipaddress+":"+fmt.Sprint(config[lastConfigItem].port), nil)
}
}
func config() []Hosts {
if info, err := os.Stat("config.yaml"); err != nil || info.Size() == 0 {
log.Fatalln("Error, config file clould not be found")
}
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
err := viper.ReadInConfig()
if err != nil {
log.Fatalln(err)
}
hostsInterface := viper.Get("hosts").([]interface{})
host = make([]Hosts, len(hostsInterface))
for i, item := range hostsInterface {
hostMap := item.(map[interface{}]interface{})
host[i].file = hostMap["file"].(string)
host[i].ipaddress = hostMap["ipaddress"].(string)
host[i].port = hostMap["port"].(int)
host[i].tls = hostMap["tls"].(bool)
host[i].cert = hostMap["cert"].(string)
host[i].key = hostMap["key"].(string)
}
return host
}
type Hosts struct {
file string
ipaddress string
port int
tls bool
cert string
key string
}