-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
87 lines (66 loc) · 2.45 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
83
84
85
86
87
package main
import (
"crypto/subtle"
"flag"
"net/http"
"os"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/porthos-rpc/porthos-playground/collector"
"github.com/porthos-rpc/porthos-playground/handlers"
"github.com/porthos-rpc/porthos-playground/storage"
"github.com/sirupsen/logrus"
)
func defaultValue(a, b string) string {
if len(a) == 0 {
return b
}
return a
}
func BasicAuth(handler http.HandlerFunc, username, password, realm string) http.HandlerFunc {
if username != "" && password != "" {
return func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorised.\n"))
return
}
handler(w, r)
}
}
return handler
}
func main() {
bindAddress := flag.String("bind", defaultValue(os.Getenv("BIND_ADDRESS"), ":3000"), "Bind Address.")
brokerURL := flag.String("broker", defaultValue(os.Getenv("BROKER_URL"), "amqp://"), "Broker URL.")
db := flag.String("db", defaultValue(os.Getenv("DB_PATH"), ":memory:"), "DB Path / Memory")
username := *flag.String("username", defaultValue(os.Getenv("USERNAME"), ""), "HTTP Username")
password := *flag.String("password", defaultValue(os.Getenv("PASSWORD"), ""), "HTTP Password")
realm := "eventials"
flag.Parse()
s := storage.NewStorage(storage.NewDb("sqlite3", *db))
c := collector.NewCollector(*brokerURL, s)
defer c.Close()
go c.Start()
serveMux := http.NewServeMux()
handler := handlers.NewHandler(*brokerURL)
defer handler.Close()
serveMux.HandleFunc("/", BasicAuth(handlers.IndexHandler, username, password, realm))
serveMux.HandleFunc("/api/services", BasicAuth(handlers.NewServicesHandler(s), username, password, realm))
serveMux.HandleFunc("/api/rpc", BasicAuth(handler.NewRPCHandler(), username, password, realm))
serveMux.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public"))))
server := &http.Server{
Addr: *bindAddress,
Handler: serveMux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
logrus.Infof("Listening to %s\n", *bindAddress)
logrus.Info("Hit CTRL-C to exit...")
if err := server.ListenAndServe(); err != nil {
logrus.WithError(err).Error("Failed to listen and serve")
}
}