2019-02-08 07:30:21 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"net/http"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
2019-02-13 23:43:55 +08:00
|
|
|
"time"
|
2019-02-08 07:30:21 +08:00
|
|
|
|
|
|
|
"github.com/dgraph-io/badger"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/nats-io/go-nats"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
var listen string
|
|
|
|
var dbPath string
|
|
|
|
var natsHost string
|
|
|
|
|
|
|
|
var db *badger.DB
|
2019-02-13 23:43:55 +08:00
|
|
|
var natsConn *nats.Conn
|
2019-02-08 07:30:21 +08:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Parse flags
|
|
|
|
flag.StringVar(&listen, "listen", ":8080", "host and port to listen on")
|
|
|
|
flag.StringVar(&dbPath, "dbpath", "/tmp/badger", "path to store data")
|
|
|
|
flag.StringVar(&natsHost, "nats", "nats://localhost:4222", "host and port of NATS")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Open badger
|
|
|
|
log.Printf("starting badger at %s", dbPath)
|
|
|
|
opts := badger.DefaultOptions
|
|
|
|
opts.Dir = dbPath
|
|
|
|
opts.ValueDir = dbPath
|
|
|
|
var err error
|
|
|
|
db, err = badger.Open(opts)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// NATS client
|
|
|
|
nc, _ := nats.Connect(natsHost);
|
2019-02-16 00:20:41 +08:00
|
|
|
nc.Subscribe("new_bite", NewBite);
|
|
|
|
nc.Subscribe("new_bite_user", NewBiteUser);
|
2019-02-10 22:18:24 +08:00
|
|
|
defer nc.Close()
|
2019-02-08 07:30:21 +08:00
|
|
|
|
2019-02-13 23:43:55 +08:00
|
|
|
natsConn = nc;
|
|
|
|
|
2019-02-08 07:30:21 +08:00
|
|
|
// Routes
|
|
|
|
router := httprouter.New()
|
|
|
|
router.GET("/conversation/:key/scan", ScanBites) // Scanning
|
|
|
|
router.GET("/conversation/:key/start/:start", GetBite) // GET bites
|
2019-02-13 23:43:55 +08:00
|
|
|
// router.GET("/conversation/:key/start/:start/user", GetBiteUser) // GET bite_users
|
2019-02-08 07:30:21 +08:00
|
|
|
|
|
|
|
// Start server
|
|
|
|
log.Printf("starting server on %s", listen)
|
|
|
|
log.Fatal(http.ListenAndServe(listen, router))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseStartString(start string) (uint64, error) {
|
|
|
|
return strconv.ParseUint(start, 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sub handlers
|
|
|
|
// m.data = Bite protobuf
|
|
|
|
func NewBite(m *nats.Msg) {
|
2019-02-13 23:43:55 +08:00
|
|
|
New("bite", m)
|
2019-02-08 07:30:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBiteUser(m *nats.Msg) {
|
2019-02-13 23:43:55 +08:00
|
|
|
New("user", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(t string, m *nats.Msg) {
|
2019-02-08 07:30:21 +08:00
|
|
|
bite := Bite{}
|
|
|
|
if err := proto.Unmarshal(m.Data, &bite); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:43:55 +08:00
|
|
|
storeRequest := Store {
|
|
|
|
Type: t,
|
|
|
|
Bite: &bite,
|
2019-02-08 07:30:21 +08:00
|
|
|
}
|
2019-02-13 23:43:55 +08:00
|
|
|
reqBytes, reqErr := proto.Marshal(&storeRequest)
|
2019-02-08 07:30:21 +08:00
|
|
|
|
2019-02-13 23:43:55 +08:00
|
|
|
if reqErr != nil {
|
|
|
|
log.Print(reqErr)
|
2019-02-08 07:30:21 +08:00
|
|
|
return
|
2019-02-13 23:43:55 +08:00
|
|
|
}
|
|
|
|
natsConn.Publish("new_store", reqBytes)
|
2019-02-08 07:30:21 +08:00
|
|
|
}
|
|
|
|
|
2019-02-10 22:18:24 +08:00
|
|
|
// Route handlers
|
2019-02-08 07:30:21 +08:00
|
|
|
func ScanBites(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2019-02-13 23:43:55 +08:00
|
|
|
from, err := ParseStartString(r.FormValue("from"))
|
2019-02-08 07:30:21 +08:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
to, err := ParseStartString(r.FormValue("to"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:43:55 +08:00
|
|
|
scanRequest := ScanRequest {
|
|
|
|
Key: p.ByName("key"),
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Type: "bite",
|
|
|
|
}
|
2019-02-08 07:30:21 +08:00
|
|
|
|
2019-02-13 23:43:55 +08:00
|
|
|
drBytes, drErr := proto.Marshal(&scanRequest);
|
|
|
|
if drErr != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2019-02-08 07:30:21 +08:00
|
|
|
|
2019-02-13 23:43:55 +08:00
|
|
|
msg, natsErr := natsConn.Request("scan_store", drBytes, 10 * 1000 * time.Millisecond) // 10s timeout
|
|
|
|
if natsErr != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2019-02-08 07:30:21 +08:00
|
|
|
|
2019-02-13 23:43:55 +08:00
|
|
|
res := Response {}
|
|
|
|
if err := proto.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2019-02-08 07:30:21 +08:00
|
|
|
|
2019-02-13 23:43:55 +08:00
|
|
|
if res.Code == 200 {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(res.Message)
|
|
|
|
} else if len(res.Message) == 0 {
|
|
|
|
http.Error(w, http.StatusText(int(res.Code)), int(res.Code))
|
|
|
|
} else {
|
|
|
|
http.Error(w, string(res.Message), int(res.Code))
|
|
|
|
}
|
2019-02-08 07:30:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBite(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2019-02-13 23:43:55 +08:00
|
|
|
Get("bite", w, r, p)
|
2019-02-08 07:30:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBiteUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2019-02-13 23:43:55 +08:00
|
|
|
Get("user", w, r, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Get(t string, w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
start, err := ParseStartString(p.ByName("start"))
|
2019-02-08 07:30:21 +08:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:43:55 +08:00
|
|
|
dataRequest := DataRequest {
|
|
|
|
Key: p.ByName("key"),
|
|
|
|
Start: start,
|
|
|
|
Type: t,
|
|
|
|
}
|
|
|
|
|
|
|
|
drBytes, drErr := proto.Marshal(&dataRequest);
|
|
|
|
if drErr != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, natsErr := natsConn.Request("request_store", drBytes, 10 * 1000 * time.Millisecond) // 10s timeout
|
|
|
|
if natsErr != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := Response {}
|
|
|
|
if err := proto.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Code == 200 {
|
|
|
|
w.Header().Add("Content-Type", "audio/wav")
|
|
|
|
w.Write(res.Message)
|
|
|
|
} else if len(res.Message) == 0 {
|
|
|
|
http.Error(w, http.StatusText(int(res.Code)), int(res.Code))
|
|
|
|
} else {
|
|
|
|
http.Error(w, string(res.Message), int(res.Code))
|
|
|
|
}
|
2019-02-08 07:30:21 +08:00
|
|
|
}
|