2013-02-20 23:43:01 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"asink"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2013-03-09 22:47:49 -05:00
|
|
|
//global variables
|
2013-02-20 23:43:01 -05:00
|
|
|
var eventsRegexp *regexp.Regexp
|
|
|
|
var port int = 8080
|
2013-07-19 22:15:53 -04:00
|
|
|
var adb *AsinkDB
|
2013-02-22 00:06:10 -05:00
|
|
|
|
2013-02-20 23:43:01 -05:00
|
|
|
func init() {
|
2013-02-22 00:06:10 -05:00
|
|
|
var err error
|
2013-02-20 23:43:01 -05:00
|
|
|
const port_usage = "Port on which to serve HTTP API"
|
|
|
|
|
|
|
|
flag.IntVar(&port, "port", 8080, port_usage)
|
|
|
|
flag.IntVar(&port, "p", 8080, port_usage+" (shorthand)")
|
|
|
|
|
|
|
|
eventsRegexp = regexp.MustCompile("^/events/([0-9]+)$")
|
2013-02-22 00:06:10 -05:00
|
|
|
|
2013-07-19 22:15:53 -04:00
|
|
|
adb, err = GetAndInitDB()
|
2013-02-22 00:06:10 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
http.HandleFunc("/", rootHandler)
|
|
|
|
http.HandleFunc("/events", eventHandler)
|
|
|
|
http.HandleFunc("/events/", eventHandler)
|
|
|
|
|
|
|
|
//TODO replace with http://golang.org/pkg/net/http/#ListenAndServeTLS
|
|
|
|
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "You're probably looking for /events/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEvents(w http.ResponseWriter, r *http.Request, nextEvent uint64) {
|
2013-02-22 00:06:10 -05:00
|
|
|
var events []*asink.Event
|
|
|
|
var error_message string = ""
|
|
|
|
defer func() {
|
|
|
|
var apiresponse asink.APIResponse
|
|
|
|
if error_message != "" {
|
|
|
|
apiresponse = asink.APIResponse{
|
|
|
|
Status: asink.ERROR,
|
|
|
|
Explanation: error_message,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
apiresponse = asink.APIResponse{
|
|
|
|
Status: asink.SUCCESS,
|
|
|
|
Events: events,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(apiresponse)
|
|
|
|
if err != nil {
|
|
|
|
error_message = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(b)
|
|
|
|
}()
|
|
|
|
|
2013-07-19 22:15:53 -04:00
|
|
|
events, err := adb.DatabaseRetrieveEvents(nextEvent, 50)
|
2013-02-22 00:06:10 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
error_message = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-03-09 22:47:49 -05:00
|
|
|
//long-poll if events is empty
|
|
|
|
if len(events) == 0 {
|
|
|
|
c := make(chan *asink.Event)
|
|
|
|
addPoller("aclindsa", &c) //TODO support more than one user
|
|
|
|
e, ok := <-c
|
|
|
|
if ok {
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func putEvents(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var events asink.EventList
|
|
|
|
var error_message string = ""
|
|
|
|
defer func() {
|
|
|
|
var apiresponse asink.APIResponse
|
2013-02-22 00:06:10 -05:00
|
|
|
if error_message != "" {
|
2013-02-20 23:43:01 -05:00
|
|
|
apiresponse = asink.APIResponse{
|
2013-02-22 00:06:10 -05:00
|
|
|
Status: asink.ERROR,
|
2013-02-20 23:43:01 -05:00
|
|
|
Explanation: error_message,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
apiresponse = asink.APIResponse{
|
2013-02-22 00:06:10 -05:00
|
|
|
Status: asink.SUCCESS,
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(apiresponse)
|
|
|
|
if err != nil {
|
2013-02-22 00:06:10 -05:00
|
|
|
error_message = err.Error()
|
|
|
|
return
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
w.Write(b)
|
|
|
|
}()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
error_message = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &events)
|
|
|
|
if err != nil {
|
|
|
|
error_message = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, event := range events.Events {
|
2013-07-19 22:15:53 -04:00
|
|
|
err = adb.DatabaseAddEvent(event)
|
2013-03-17 23:04:23 -04:00
|
|
|
if err != nil {
|
2013-03-18 07:17:44 -04:00
|
|
|
//TODO should probably do this in a way that the caller knows how many of these have failed and doesn't re-try sending ones that succeeded
|
|
|
|
//i.e. add this to the return codes or something
|
|
|
|
//OR put all the DatabaseAddEvent's inside a SQL transaction, and rollback on any failure
|
2013-03-17 23:04:23 -04:00
|
|
|
error_message = err.Error()
|
|
|
|
return
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
2013-03-09 22:47:49 -05:00
|
|
|
|
|
|
|
broadcastToPollers("aclindsa", events.Events[0]) //TODO support more than one user
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func eventHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
//if GET, return any events later than (and including) the event id passed in
|
|
|
|
if sm := eventsRegexp.FindStringSubmatch(r.RequestURI); sm != nil {
|
|
|
|
i, err := strconv.ParseUint(sm[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
//TODO display error message here instead
|
|
|
|
fmt.Printf("ERROR parsing " + sm[1] + "\n")
|
|
|
|
getEvents(w, r, 0)
|
|
|
|
} else {
|
|
|
|
getEvents(w, r, i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
getEvents(w, r, 0)
|
|
|
|
}
|
|
|
|
} else if r.Method == "POST" {
|
|
|
|
putEvents(w, r)
|
|
|
|
} else {
|
2013-03-18 07:17:44 -04:00
|
|
|
apiresponse := asink.APIResponse{
|
|
|
|
Status: asink.ERROR,
|
|
|
|
Explanation: "Invalid HTTP method - only GET and POST are supported on this endpoint.",
|
|
|
|
}
|
|
|
|
b, _ := json.Marshal(apiresponse)
|
|
|
|
w.Write(b)
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
}
|