-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (112 loc) · 3.01 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
131
132
133
package main
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/http"
"strings"
"text/template"
"time"
"github.com/gorilla/mux"
)
var certFile = "server.crt"
var keyFile = "server.key"
func main() {
r := mux.NewRouter()
r.HandleFunc("/stream", stream)
r.HandleFunc("/compile", compile)
// serve static files
ui := http.FileServer(http.Dir("./static/"))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", ui))
// r.Handle("/public/", http.StripPrefix("/public/", ui))
r.HandleFunc("/", home)
http.Handle("/", r)
lis, err := net.Listen("tcp", "127.0.0.1:6464")
if err != nil {
log.Fatal(err)
}
svr := &http.Server{}
svr.TLSConfig = &tls.Config{
Certificates: make([]tls.Certificate, 1),
}
svr.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(
certFile, keyFile)
if err != nil {
log.Fatal(err)
}
// Attaches http2
// http2.ConfigureServer(svr, &http2.Server{})
log.Println("Listening on:", lis.Addr())
tlsLis := tls.NewListener(lis, svr.TLSConfig)
log.Fatal(svr.Serve(tlsLis))
}
var tmpls *template.Template
var tplHome *template.Template
var cert tls.Certificate
func init() {
var err error
tmpls = template.New("nevermatch").Delims("{{{", "}}}")
cert, err = tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatal("cert failure:", err)
}
}
func home(w http.ResponseWriter, r *http.Request) {
// Put this here for easier development
tmpls := template.New("nevermatch").Delims("{{{", "}}}")
tplHome := template.Must(tmpls.ParseFiles("tmpl/index.html"))
tplHome.ExecuteTemplate(w, "index.html", nil)
}
func doCompile(body io.Reader) (io.Reader, error) {
cli := http.Client{}
urlStr := "https://dumass.local:12345"
slurp, err := cli.Post(urlStr, "application/json", body)
if err != nil {
return nil, err
}
fmt.Println("Received response as:", slurp.Proto)
return slurp.Body, nil
}
func mustCompile(r io.Reader, err error) bytes.Buffer {
if err != nil {
panic(err)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, r)
if err != nil {
panic(err)
}
return buf
}
func compile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
resp, err := doCompile(r.Body)
if err != nil {
log.Println("ERROR contacting wt:", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"could not contact upstream wellington"}`))
return
}
io.Copy(w, resp)
}
func stream(w http.ResponseWriter, r *http.Request) {
clientGone := w.(http.CloseNotifier).CloseNotify()
w.Header().Set("Content-Type", "text/html")
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
fmt.Fprintf(w, "<!-- # ~1KB of junk to force browsers to start rendering immediately: \n -->")
io.WriteString(w, strings.Repeat("<!--# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n-->", 13))
for {
fmt.Fprintf(w, "<p>%v</p>", time.Now())
w.(http.Flusher).Flush()
select {
case <-ticker.C:
case <-clientGone:
log.Printf("Client %v disconnected from the clock", r.RemoteAddr)
return
}
}
}