-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (99 loc) · 2.5 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
package main
import (
"io"
"log"
"net"
"net/http"
"os"
"strings"
"unicode"
"google.golang.org/grpc"
"github.com/badgerodon/httpcompression"
"github.com/badgerodon/simulator/builder/builderpb"
)
var (
grpcServer *grpc.Server
)
func main() {
err := run()
if err != nil {
log.Fatalln(err)
}
}
func run() error {
log.SetFlags(0)
grpcServer = grpc.NewServer()
builderpb.RegisterServiceServer(grpcServer, getBuildServer())
go func() {
li, err := net.Listen("tcp", "127.0.0.1:5001")
if err != nil {
log.Fatalln(err)
}
log.Println("starting gRPC server on :5001")
grpcServer.Serve(li)
}()
http.Handle("/ui/assets/", http.StripPrefix("/ui/assets/", httpcompression.FileServer(http.Dir("./ui/assets/"))))
http.Handle("/srv/", http.StripPrefix("/srv/", httpcompression.FileServer(http.Dir(builderDataDir))))
http.Handle("/", http.HandlerFunc(handleCatchAll))
addr := os.Getenv("ADDR")
if addr == "" {
addr = "127.0.0.1:5000"
}
log.Println("starting server on " + addr)
return http.ListenAndServe(addr, nil)
}
func makeFileHandler(filePath string) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res, req, filePath)
}
}
type nopWriteCloser struct {
io.Writer
}
func (w nopWriteCloser) Close() error {
return nil
}
func acceptsBrotli(r *http.Request) bool {
fs := strings.FieldsFunc(r.Header.Get("Accept-Encoding"), func(r rune) bool {
return unicode.IsSpace(r) || r == ','
})
for _, f := range fs {
if f == "br" {
return true
}
}
return false
}
func handleCatchAll(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
return
}
handleUI(w, r)
}
func handleUI(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>Simulator</title>
<link rel="stylesheet" type="text/css" href="/ui/assets/css/normalize.css" />
<link rel="stylesheet" type="text/css" href="/ui/assets/css/xterm.css" />
<link rel="stylesheet" type="text/css" href="/ui/assets/css/main.css" />
</head>
<body>
<header>
<h1>Simulator</h1>
</header>
<section id="terminal-container">
</section>
<footer>
©2017 Caleb Doxsey
</footer>
<script src="/ui/assets/js/xterm.js"></script>
<script src="/ui/assets/js/main.js"></script>
</body>
</html>`)
}