forked from retrixe/octyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (72 loc) · 2.26 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/gorilla/handlers"
)
// OctyneVersion is the last version of Octyne this code is based on.
const OctyneVersion = "1.0.2"
func getPort(config Config) string {
if config.Port == 0 {
return ":42069"
}
return ":" + strconv.Itoa(int(config.Port))
}
func main() {
if len(os.Args) >= 2 && (os.Args[1] == "--version" || os.Args[1] == "-v") {
println("octyne version " + OctyneVersion)
return
}
log.SetPrefix("[Octyne] ")
// Read config.
var config Config
file, err := os.Open("config.json")
if err != nil {
panic("An error occurred while attempting to read config!\n" + err.Error())
}
contents, _ := ioutil.ReadAll(file)
err = json.Unmarshal(contents, &config)
if err != nil {
panic("An error occurred while attempting to read config!\n" + err.Error())
}
// Get a slice of server names.
servers := make([]string, 0, len(config.Servers))
for k := range config.Servers {
servers = append(servers, k)
}
log.Println("Config read successfully!")
// Setup daemon connector.
connector := InitializeConnector(config)
// This defer never actually gets called, hence commented.
// if connector.Authenticator.Redis != nil { defer connector.Authenticator.Redis.Close() }
// Run processes, passing the daemon connector.
for _, name := range servers {
go CreateProcess(name, config.Servers[name], connector)
}
// Listen.
port := getPort(config)
log.Println("Listening to port " + port[1:])
handler := handlers.CORS(
handlers.AllowedHeaders([]string{
"X-Requested-With", "Content-Type", "Authorization", "Username", "Password",
}),
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS", "PATCH", "DELETE"}),
handlers.AllowedOrigins([]string{"*"}),
)(connector.Router)
if !config.HTTPS.Enabled {
err = http.ListenAndServe(port, handler)
} else {
err = http.ListenAndServeTLS(port, config.HTTPS.Cert, config.HTTPS.Key, handler)
}
redisAuthenticator, isRedisAuthenticator := connector.Authenticator.(*RedisAuthenticator)
if isRedisAuthenticator && redisAuthenticator.Redis != nil { // Close Redis if needed.
if redisErr := redisAuthenticator.Redis.Close(); redisErr != nil {
println(redisErr)
}
}
log.Fatalln(err) // skipcq: GO-S0904
}