5
0
Fork 0
backend-publish/main.go

180 lines
4.1 KiB
Go
Raw Normal View History

2019-02-06 13:26:35 +08:00
package main;
import (
2019-02-24 04:02:26 +08:00
"context"
"encoding/json"
2019-02-06 13:26:35 +08:00
"io/ioutil"
"net/http"
"log"
2019-02-18 23:43:35 +08:00
"os"
2019-02-06 13:26:35 +08:00
"regexp"
"strconv"
2019-02-18 23:43:35 +08:00
"github.com/joho/godotenv"
2019-02-06 13:26:35 +08:00
"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
2019-02-10 22:17:24 +08:00
var secret []byte
2019-02-06 13:26:35 +08:00
var nats_conn *nats.Conn
func main() {
2019-02-18 23:43:35 +08:00
// 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")
2019-02-06 13:26:35 +08:00
2019-02-10 22:17:24 +08:00
secret = []byte(s)
2019-02-06 13:26:35 +08:00
//NATS
n, err := nats.Connect(natsHost)
if err != nil {
log.Fatal(err)
return
}
nats_conn = n
// Routes
router := httprouter.New()
2019-02-24 04:02:26 +08:00
router.PUT("/conversation/:key/start/:start", AuthMiddleware(PutBite)) // bites
router.PUT("/conversation/:key/start/:start/user", AuthMiddleware(PutBiteUser)) // bite_users
2019-02-06 13:26:35 +08:00
// Start server
log.Printf("starting server on %s", listen)
2019-02-24 04:02:26 +08:00
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)
}
2019-02-06 13:26:35 +08:00
}
// 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) {
2019-02-24 04:02:26 +08:00
client := r.Context().Value("user").(RawClient)
c := Client {
Key: client.UserId,
Client: client.ClientId,
2019-02-10 22:17:24 +08:00
}
2019-02-06 13:26:35 +08:00
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) {
2019-02-06 13:26:35 +08:00
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,
2019-02-24 04:02:26 +08:00
Client: &c,
2019-02-06 13:26:35 +08:00
}
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) {
2019-02-24 04:02:26 +08:00
client := r.Context().Value("user").(RawClient)
c := Client {
Key: client.UserId,
Client: client.ClientId,
2019-02-10 22:17:24 +08:00
}
2019-02-06 13:26:35 +08:00
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) {
2019-02-06 13:26:35 +08:00
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,
2019-02-24 04:02:26 +08:00
Client: &c,
2019-02-06 13:26:35 +08:00
}
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)
}