This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (150 loc) · 4.1 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main;
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"log"
"os"
"regexp"
"strconv"
. "publish/backend-protobuf/go"
"github.com/joho/godotenv"
"github.com/golang/protobuf/proto"
"github.com/julienschmidt/httprouter"
"github.com/nats-io/go-nats"
)
const MaxBiteSize = 1024 * 1024 * 10
var listen string
var natsHost string
var secret []byte
var nats_conn *nats.Conn
func main() {
// Load .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
listen = os.Getenv("LISTEN")
natsHost = os.Getenv("NATS")
s := os.Getenv("SECRET")
secret = []byte(s)
//NATS
n, err := nats.Connect(natsHost)
if err != nil {
log.Fatal(err)
return
}
nats_conn = n
// Routes
router := httprouter.New()
router.PUT("/conversation/:key/start/:start", AuthMiddleware(PutBite)) // bites
router.PUT("/conversation/:key/start/:start/user", AuthMiddleware(PutBiteUser)) // bite_users
// Start server
log.Printf("starting server on %s", listen)
log.Fatal(http.ListenAndServe(listen, router))
}
type RawClient struct {
UserId string `json:"userid"`
ClientId string `json:"clientid"`
}
func AuthMiddleware(next httprouter.Handle) httprouter.Handle {
return func (w http.ResponseWriter, r *http.Request, p httprouter.Params) {
ua := r.Header.Get("X-User-Claim")
if ua == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
var client RawClient
err := json.Unmarshal([]byte(ua), &client)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
context := context.WithValue(r.Context(), "user", client)
next(w, r.WithContext(context), p)
}
}
// TODO: ensure security of regexp
var validConversationRegexp = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
func validConversation(conversation string) bool {
return validConversationRegexp.MatchString(conversation)
}
func ParseStartString(start string) (uint64, error) {
return strconv.ParseUint(start, 10, 64)
}
// Route handlers
func PutBite(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
client := r.Context().Value("user").(RawClient)
c := Client {
Key: client.UserId,
Client: client.ClientId,
}
start, err := ParseStartString(p.ByName("start"))
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
key := p.ByName("key")
if !validConversation(key) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
reader := http.MaxBytesReader(w, r.Body, MaxBiteSize)
body, err := ioutil.ReadAll(reader)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
b := Bite {
Start: start,
Key: key,
Data: body,
Client: &c,
}
out, err := proto.Marshal(&b)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
nats_conn.Publish("new_bite", out)
w.WriteHeader(200)
}
func PutBiteUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
client := r.Context().Value("user").(RawClient)
c := Client {
Key: client.UserId,
Client: client.ClientId,
}
start, err := ParseStartString(p.ByName("start"))
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
key := p.ByName("key")
if !validConversation(key) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
reader := http.MaxBytesReader(w, r.Body, MaxBiteSize)
body, err := ioutil.ReadAll(reader)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
b := Bite {
Start: start,
Key: key,
Data: body,
Client: &c,
}
out, err := proto.Marshal(&b)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
nats_conn.Publish("new_bite_user", out)
w.WriteHeader(200)
}