This repository has been archived by the owner on Dec 27, 2020. It is now read-only.
/
CSSimulator.go
125 lines (106 loc) · 2.79 KB
/
CSSimulator.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
120
121
122
123
124
125
package main
import (
"CitySourcedAPI/config"
"CitySourcedAPI/data"
"CitySourcedAPI/docs"
"CitySourcedAPI/logs"
"CitySourcedAPI/request"
"flag"
"os"
"os/signal"
"fmt"
"io/ioutil"
"net/http"
"time"
)
var (
log = logs.Log
Debug = true
)
func main() {
// setOptions()
http.HandleFunc("/", homeHandler)
http.HandleFunc("/docs", docHandler)
http.HandleFunc("/api/", apiHandler)
http.ListenAndServe(fmt.Sprintf(":%d", config.Port()), nil)
}
func init() {
var port int64
flag.BoolVar(&Debug, "debug", false, "Activate debug prints to the console. Active if either this or the value in 'config.json' are set.")
flag.Int64Var(&port, "port", 0, "Port. If set, this flag will override the value in 'config.json'.")
flag.Parse()
logs.Init(Debug)
if err := config.Init("config.json", port); err != nil {
log.Error("Error loading config file: %s\n", err)
}
if err := data.Init("data.json"); err != nil {
log.Error("Error loading config file: %s\n", err)
}
log.Info("Running on port: %d", config.Port())
go SignalHandler(make(chan os.Signal, 1))
fmt.Println("Press Ctrl-C to shutdown...")
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
log.Info("[root] request")
if r.URL.Path != "/" {
errorHandler(w, r, http.StatusNotFound)
return
}
fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", docs.Home.Title, docs.Home.Body)
}
func docHandler(w http.ResponseWriter, r *http.Request) {
log.Info("[doc] request @ path %q\n", r.URL.Path)
if r.URL.Path != "/docs" {
errorHandler(w, r, http.StatusNotFound)
return
}
fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", docs.Detail.Title, docs.Detail.Body)
}
func errorHandler(w http.ResponseWriter, r *http.Request, status int) {
log.Info("Error for request: %#v", r)
w.WriteHeader(status)
if status == http.StatusNotFound {
fmt.Fprint(w, "custom 404")
}
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
log.Info("API Handler Request (%s)", r.Method)
start := time.Now()
if r.URL.Path != "/api/" || r.Method != "POST" {
errorHandler(w, r, http.StatusNotFound)
return
}
req, err := ioutil.ReadAll(r.Body)
if err != nil {
errorHandler(w, r, http.StatusBadRequest)
return
}
// decoder := xml.NewDecoder(r.Body)
// var stReq request.Request_Type
// err := decoder.Decode(&stReq)
// if err != nil {
// log.Error("Error decoding\n")
// errorHandler(w, r, http.StatusNotFound)
// }
log.Debug("api request - method: %v\n%#v\n", r.Method, string(req))
resp, err := request.Process(string(req), start)
fmt.Fprint(w, resp)
}
func SignalHandler(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
for s := <-c; ; s = <-c {
switch s {
case os.Interrupt:
fmt.Println("Ctrl-C Received!")
stop()
os.Exit(0)
case os.Kill:
fmt.Println("SIGKILL Received!")
stop()
os.Exit(1)
}
}
}
func stop() error {
return nil
}