-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
90 lines (73 loc) · 2.19 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
package main
import (
"fmt"
"github.com/gorilla/mux"
pageGen "html/template"
"log"
"net/http"
"os"
"strconv"
"time"
)
const (
jsonType = "application/json"
htmlType = "text/html"
)
var (
publicUri = ""
gen *pageGen.Template = nil
)
// initialize globals
func initialize() error {
publicUri = os.Getenv("public_uri")
gen = pageGen.Must(pageGen.ParseGlob("views/*.html"))
return nil
}
func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration {
if len(val) > 0 {
parsedVal, parseErr := strconv.Atoi(val)
if parseErr == nil && parsedVal >= 0 {
return time.Duration(parsedVal) * time.Second
}
}
duration, durationErr := time.ParseDuration(val)
if durationErr != nil {
return fallback
}
return duration
}
func main() {
readTimeout := parseIntOrDurationValue(os.Getenv("read_timeout"), 10*time.Second)
writeTimeout := parseIntOrDurationValue(os.Getenv("write_timeout"), 10*time.Second)
var err error
err = initialize()
if err != nil {
log.Fatal("failed to initialize the gateway, error: ", err.Error())
}
log.Printf("successfully initialized gateway")
s := &http.Server{
Addr: fmt.Sprintf(":%d", 8082),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
MaxHeaderBytes: 1 << 20, // Max header of 1MB
}
r := mux.NewRouter()
// Template
r.HandleFunc("/", dashboardPageHandler)
r.HandleFunc("/flow/info", flowInfoPageHandler)
r.HandleFunc("/flow/requests", flowRequestsPageHandler)
r.HandleFunc("/flow/request/monitor", flowRequestMonitorPageHandler)
// API request
r.HandleFunc("/api/flow/list", listFlowsHandler)
r.HandleFunc("/api/flow/info", flowDescHandler)
r.HandleFunc("/api/flow/requests", listFlowRequestsHandler)
r.HandleFunc("/api/flow/request/traces", requestTracesHandler)
r.HandleFunc("/api/flow/request/execute", executeRequestHandler)
r.HandleFunc("/api/flow/request/pause", pauseRequestHandler)
r.HandleFunc("/api/flow/request/resume", resumeRequestHandler)
r.HandleFunc("/api/flow/request/stop", stopRequestHandler)
// Static content
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets/static/"))))
http.Handle("/", r)
log.Fatal(s.ListenAndServe())
}