5
0
Fork 0
backend-publish/main.go

165 lines
3.8 KiB
Go
Raw Normal View History

2019-02-06 13:26:35 +08:00
package main;
import (
"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"
2019-02-10 22:17:24 +08:00
"github.com/dgrijalva/jwt-go"
"github.com/aiden0z/go-jwt-middleware"
2019-02-06 13:26:35 +08:00
)
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
2019-02-10 22:17:24 +08:00
// JWT Middleware
jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options {
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return secret, nil
},
SigningMethod: jwt.SigningMethodHS256,
})
2019-02-06 13:26:35 +08:00
// Routes
router := httprouter.New()
router.PUT("/conversation/:key/start/:start", PutBite) // bites
router.PUT("/conversation/:key/start/:start/user", PutBiteUser) // bite_users
// Start server
log.Printf("starting server on %s", listen)
2019-02-10 22:17:24 +08:00
log.Fatal(http.ListenAndServe(listen, jwtMiddleware.Handler(router)))
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-10 22:17:24 +08:00
user := r.Context().Value("user")
userClaims := user.(*jwt.Token).Claims.(jwt.MapClaims)
client := Client {
Key: userClaims["id"].(string),
Client: userClaims["client"].(string),
}
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-10 22:17:24 +08:00
Client: &client,
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-10 22:17:24 +08:00
user := r.Context().Value("user")
userClaims := user.(*jwt.Token).Claims.(jwt.MapClaims)
client := Client {
Key: userClaims["id"].(string),
Client: userClaims["client"].(string),
}
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-10 22:17:24 +08:00
Client: &client,
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)
}