2013-02-20 23:43:01 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"asink"
|
2013-02-22 00:06:10 -05:00
|
|
|
"database/sql"
|
2013-02-20 23:43:01 -05:00
|
|
|
"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-02-22 00:06:10 -05:00
|
|
|
var db *sql.DB
|
|
|
|
|
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
|
|
|
|
|
|
|
db, err = GetAndInitDB()
|
|
|
|
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)
|
|
|
|
}()
|
|
|
|
|
|
|
|
events, err := DatabaseRetrieveEvents(db, nextEvent, 50)
|
|
|
|
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-02-22 00:06:10 -05:00
|
|
|
DatabaseAddEvent(db, event)
|
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 {
|
|
|
|
fmt.Fprintf(w, "You f-ed up.")
|
|
|
|
}
|
|
|
|
}
|