-
Notifications
You must be signed in to change notification settings - Fork 5
/
webserver.go
65 lines (50 loc) · 1.19 KB
/
webserver.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
package main
import (
"fmt"
"net/http"
"strings"
)
type Webserver struct {
port uint64
client *Client
}
func StartWebserver(port uint64) {
client, err := NewClient()
if err != nil {
panic(err)
}
webserver := &Webserver{port, client}
http.Handle("/", http.FileServer(http.Dir("./web")))
http.HandleFunc("/book/", webserver.book)
http.HandleFunc("/chains", webserver.chains)
localPath := fmt.Sprintf(":%v", port)
if err := http.ListenAndServe(localPath, nil); err != nil {
panic(err)
}
}
func (ws *Webserver) book(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
chunks := strings.Split(path, "/")
if len(chunks) != 4 || chunks[0] != "" || chunks[1] != "book" || chunks[2] == "" || chunks[3] == "" {
err := "invalid path"
w.Write([]byte(err))
return
}
symbol1 := chunks[2]
symbol2 := chunks[3]
bookData, err := ws.client.GetBook(symbol1, symbol2)
if err != nil {
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(bookData))
}
func (ws *Webserver) chains(w http.ResponseWriter, r *http.Request) {
amt := uint64(10)
chainData, err := ws.client.DumpChains(amt)
if err != nil {
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(chainData))
}