forked from lyddonb/trajectory
/
main.go
130 lines (101 loc) · 3 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
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
126
127
128
129
130
package main
import (
"crypto/sha1"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
auth "github.com/abbot/go-http-auth"
"github.com/lyddonb/trajectory/db"
"github.com/lyddonb/trajectory/pipe"
"github.com/lyddonb/trajectory/rest"
)
const (
TASK_PREFIX = "/api/tasks/"
STAT_PREFIX = "/api/stats/"
)
var (
redisPort = flag.String("redis-port", "6379", "Port that redis is hosted on.")
redisHost = flag.String("redis-host", "127.0.0.1", "Host address that redis is hosted on.")
tcpListenerPort = flag.String("listener-port", "1300", "Port the tcp listener is listening on.")
webHostPort = flag.String("web-host-port", "3000", "Port the web application is exposed on.")
)
type LoginConfig struct {
Username string
Password string
}
func middleware(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
for _, m := range middleware {
h = m(h)
}
// TODO: Get this to only be setup once.
authenticator := auth.NewBasicAuthenticator(
"trajectory.com", GetSecret(getLoginConfig()))
return auth.JustCheck(authenticator, h)
}
func setupTasks(pool db.DBPool, writeToFile bool) {
router := rest.SetupTaskRouter(pool, TASK_PREFIX, writeToFile, middleware)
http.Handle(TASK_PREFIX, router)
}
func setupStats(pool db.DBPool, writeToFile bool) {
router := rest.SetupStatRouter(pool, STAT_PREFIX, writeToFile, middleware)
http.Handle(STAT_PREFIX, router)
}
func GetSecret(loginConfigs []LoginConfig) auth.SecretProvider {
return func(user string, realm string) string {
for _, loginConfig := range loginConfigs {
if loginConfig.Username == user {
h := sha1.New()
io.WriteString(h, loginConfig.Password)
hashed := base64.StdEncoding.EncodeToString(h.Sum(nil))
hashed = fmt.Sprintf("{SHA}%s", hashed)
return hashed
}
}
return ""
}
}
func getLoginConfig() []LoginConfig {
file, err := os.Open("login.json")
loginConfig := []LoginConfig{}
if err != nil || file == nil {
fmt.Println("Error loading login config: ", err)
return loginConfig
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&loginConfig)
if err != nil {
fmt.Println("Error decoding login config: ", err)
}
return loginConfig
}
func setupWeb() {
authenticator := auth.NewBasicAuthenticator(
"trajectory.com", GetSecret(getLoginConfig()))
http.HandleFunc(
"/",
authenticator.Wrap(func(
res http.ResponseWriter, req *auth.AuthenticatedRequest) {
http.FileServer(http.Dir("./web/")).ServeHTTP(res, &req.Request)
}))
}
func main() {
flag.Parse()
fmt.Println("Redis connection: ", *redisHost, *redisPort)
// Stand up redis pool.
pool := db.StartDB(*redisHost+":"+*redisPort, "")
go func() {
listener := pipe.MakeConnection("tcp", ":"+*tcpListenerPort)
taskPipeline := pipe.NewTaskPipeline(pool)
pipe.Listen(listener, taskPipeline)
}()
writeToFile := false
setupStats(pool, writeToFile)
setupTasks(pool, writeToFile)
setupWeb()
fmt.Println("Listen on :", *webHostPort)
http.ListenAndServe(":"+*webHostPort, nil)
}