/
server.go
119 lines (93 loc) · 2.3 KB
/
server.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
117
118
119
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/hoisie/web"
// register this postgres driver with the SQL module
_ "net/http/pprof"
_ "github.com/lib/pq"
)
type (
Server struct {
Via *Via
AllowedCountries map[string]bool
Host string
Port int
}
)
const (
expiry int = 3600
)
var (
Debug bool
Parallel bool
)
func Splash(ctx *web.Context) {
ctx.ContentType("image/jpeg")
http.ServeFile(ctx, ctx.Request, "./splash.jpg")
}
func parse_flags() {
flag.BoolVar(&Debug, "debug", false, "toggle debugging on/off")
flag.BoolVar(&Parallel, "par", false, "turn on parallel execution")
flag.Parse()
}
func test_connection() {
// redis
fmt.Printf("loading services")
}
func Options(ctx *web.Context, route string) string {
ctx.SetHeader("Access-Control-Allow-Origin", "*", false)
ctx.SetHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, If-None-Match", false)
ctx.SetHeader("Access-control-Allow-Methods", "GET, PUT, POST, DELETE", false)
ctx.ContentType("application/json")
return "{}"
}
func main() {
parse_flags()
var config ViaConfig
var configFile string
args := flag.Args()
if len(args) < 1 {
configFile = "production.json"
} else {
configFile = args[0]
}
log.Print("loading config from " + configFile + "... ")
config, err := LoadConfig(configFile)
if err != nil {
log.Printf("failed: %s\n", configFile, err.Error())
return
}
// Handle SIGINT and SIGKILL
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGABRT)
go func() {
for sig := range c {
log.Printf("received %v, exiting...", sig)
os.Exit(1)
}
}()
procs := runtime.NumCPU()
runtime.GOMAXPROCS(procs)
log.Printf("starting server, running on %d cores...", procs)
via := NewVia(Debug, expiry, config.DataDir)
server := Server{Via: via, Host: config.Host, Port: config.Port, AllowedCountries: config.AllowedCountries}
// Basic
web.Get("/", Splash)
web.Get("/status", server.GetServerStatus)
// Dmatrix
web.Post("/matrix/", server.PostMatrix)
// Path
web.Post("/paths", server.PostPaths)
web.Match("OPTIONS", "/(.*)", Options)
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
web.Run(fmt.Sprintf("%s:%d", config.Host, config.Port))
}