-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.go
116 lines (95 loc) · 2.89 KB
/
runner.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"crypto/tls"
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"github.com/CodeCollaborate/Server/modules/config"
"github.com/CodeCollaborate/Server/modules/dbfs"
"github.com/CodeCollaborate/Server/modules/handlers"
"github.com/CodeCollaborate/Server/modules/rabbitmq"
"github.com/CodeCollaborate/Server/utils"
"golang.org/x/crypto/acme/autocert"
)
/**
* Runner.go starts the server. It initializes processes and begins listening for websocket requests.
*/
var logDir = flag.String("log_dir", "./data/logs/", "log file location")
func main() {
flag.Parse()
config.EnableLoggingToFile(*logDir)
err := config.LoadConfig()
if err != nil {
utils.LogFatal("Failed to load configuration", err, nil)
}
cfg := config.GetConfig()
// Get working directory
dir, err := os.Getwd()
utils.LogFatal("Could not get working directory", err, nil)
utils.LogInfo("Working directory initalized", utils.LogFields{
"Working Directory": dir,
})
// Creates a NewControl block for multithreading control
AMQPControl := utils.NewControl(1)
// RabbitMQ uses "Exchanges" as containers for Queues, and ours is initialized here.
rabbitmq.SetupRabbitExchange(
&rabbitmq.AMQPConnCfg{
ConnCfg: cfg.ConnectionConfig["RabbitMQ"],
Exchanges: []rabbitmq.AMQPExchCfg{
{
ExchangeName: cfg.ServerConfig.Name,
Durable: true,
},
},
Control: AMQPControl,
},
)
dbfs.Dbfs = new(dbfs.DatabaseImpl)
http.HandleFunc("/ws/", handlers.NewWSConn)
addr := fmt.Sprintf(":%d", cfg.ServerConfig.Port)
//_, certErr := os.Stat("config/TLS/cert.pem")
//_, keyErr := os.Stat("config/TLS/key.pem")
//useTLS := certErr == nil && keyErr == nil
utils.LogInfo("Starting server", utils.LogFields{
"Address": addr,
"Host": cfg.ServerConfig.Host,
"TLS": cfg.ServerConfig.UseTLS,
})
go func() {
addr := fmt.Sprintf("0.0.0.0:%d", cfg.ServerConfig.Port+1)
err := http.ListenAndServe(addr, nil)
if err != nil {
utils.LogError("Failed to start pprof", err, utils.LogFields{
"Address": addr,
})
} else {
utils.LogError("pprof debugging server started at 0.0.0.0:8000", err, utils.LogFields{
"Address": addr,
})
}
}()
if cfg.ServerConfig.UseTLS {
dirCache := autocert.DirCache("certs")
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(cfg.ServerConfig.Host), //your domain here
Cache: dirCache, //folder for storing certificates
}
server := &http.Server{
Addr: addr,
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
server.ListenAndServeTLS("", "") //key and cert are comming from Let's Encrypt
} else {
err = http.ListenAndServe(addr, nil)
}
utils.LogError("Could not bind to port", err, nil)
// Kill the SetupRabbitExchange thread (Multithreading control)
defer func() {
AMQPControl.Exit <- true
}()
}