-
Notifications
You must be signed in to change notification settings - Fork 53
/
http.go
139 lines (125 loc) · 3.81 KB
/
http.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
134
135
136
137
138
139
package main
import (
"context"
"net/http"
"runtime"
"strings"
"time"
"github.com/MixinNetwork/ocean.one/cache"
"github.com/MixinNetwork/ocean.one/config"
"github.com/MixinNetwork/ocean.one/persistence"
"github.com/dimfeld/httptreemux"
"github.com/gofrs/uuid/v5"
"github.com/gorilla/handlers"
"github.com/gorilla/websocket"
"github.com/unrolled/render"
)
type RequestHandler struct {
hub *cache.Hub
upgrader *websocket.Upgrader
router *httptreemux.TreeMux
}
func (handler *RequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer recover()
if r.URL.Path == "/_hc" {
render.New().JSON(w, http.StatusOK, map[string]interface{}{})
return
}
if r.URL.Path != "/" {
handler.router.ServeHTTP(w, r)
return
}
if strings.ToLower(r.Header.Get("Upgrade")) != "websocket" {
cp, err := persistence.ReadPropertyAsTime(r.Context(), CheckpointMixinNetworkSnapshots)
if err != nil {
render.New().JSON(w, http.StatusInternalServerError, map[string]interface{}{"error": err.Error()})
return
}
ac, err := persistence.CountPendingActions(r.Context())
if err != nil {
render.New().JSON(w, http.StatusInternalServerError, map[string]interface{}{"error": err.Error()})
return
}
tc, err := persistence.CountPendingTransfers(r.Context())
if err != nil {
render.New().JSON(w, http.StatusInternalServerError, map[string]interface{}{"error": err.Error()})
return
}
data := map[string]interface{}{
"build": config.BuildVersion + "-" + runtime.Version(),
"developers": "https://github.com/MixinNetwork/ocean.one",
"checkpoint": cp,
"actions": ac,
"transfers": tc,
}
render.New().JSON(w, http.StatusOK, map[string]interface{}{"data": data})
return
}
conn, err := handler.upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
defer conn.Close()
cid, err := uuid.NewV4()
if err != nil {
return
}
ctx, cancel := context.WithCancel(r.Context())
client, err := cache.NewClient(ctx, handler.hub, conn, cid.String(), cancel)
if err != nil {
return
}
if err := handler.hub.Register(ctx, client); err != nil {
return
}
defer handler.hub.Unregister(client)
go client.WritePump(ctx)
client.ReadPump(ctx)
}
func StartHTTP(ctx context.Context) error {
hub := cache.NewHub()
go hub.Run(ctx)
rh := &RequestHandler{
hub: hub,
upgrader: &websocket.Upgrader{
HandshakeTimeout: 60 * time.Second,
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
render.New().JSON(w, status, map[string]interface{}{"error": reason.Error()})
},
},
router: NewRouter(),
}
handler := handleContext(rh, ctx)
handler = handleCORS(handler)
handler = handlers.ProxyHeaders(handler)
server := &http.Server{Addr: ":7000", Handler: handler}
return server.ListenAndServe()
}
func handleContext(handler http.Handler, src context.Context) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := cache.SetupRedis(r.Context(), cache.Redis(src))
ctx = persistence.SetupSpanner(ctx, persistence.Spanner(src))
handler.ServeHTTP(w, r.WithContext(ctx))
})
}
func handleCORS(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin == "" {
handler.ServeHTTP(w, r)
return
}
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Add("Access-Control-Allow-Headers", "Content-Type,Authorization,Mixin-Conversation-ID")
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS,GET,POST,DELETE")
w.Header().Set("Access-Control-Max-Age", "600")
if r.Method == "OPTIONS" {
render.New().JSON(w, http.StatusOK, map[string]interface{}{})
} else {
handler.ServeHTTP(w, r)
}
})
}