2013-09-04 22:02:17 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2013 Aaron Lindsay <aaron@aclindsay.com>
|
|
|
|
*/
|
|
|
|
|
2013-02-20 23:43:01 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"asink"
|
2013-08-23 00:09:03 -04:00
|
|
|
"encoding/base64"
|
2013-02-20 23:43:01 -05:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2013-08-23 00:09:03 -04:00
|
|
|
"net"
|
2013-02-20 23:43:01 -05:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2013-08-23 00:09:03 -04:00
|
|
|
"strings"
|
2013-02-20 23:43:01 -05:00
|
|
|
)
|
|
|
|
|
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-09-02 23:35:48 -04:00
|
|
|
var rpcSock string
|
2013-09-02 09:36:03 -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
|
|
|
|
|
|
|
eventsRegexp = regexp.MustCompile("^/events/([0-9]+)$")
|
2013-02-22 00:06:10 -05:00
|
|
|
|
2013-09-02 09:36:03 -04:00
|
|
|
adb, err = GetAndInitDB()
|
2013-02-22 00:06:10 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-09-03 22:21:18 -04:00
|
|
|
|
|
|
|
asink.SetupCleanExitOnSignals()
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
|
2013-09-02 23:35:48 -04:00
|
|
|
const sock_usage = "Socket to use to connect to the Asink server."
|
|
|
|
const sock_default = "/var/run/asink/asinkd.sock"
|
|
|
|
|
2013-09-02 09:36:03 -04:00
|
|
|
func StartServer(args []string) {
|
|
|
|
const port_usage = "Port on which to serve HTTP API"
|
|
|
|
|
|
|
|
flags := flag.NewFlagSet("start", flag.ExitOnError)
|
|
|
|
flags.IntVar(&port, "port", 8080, port_usage)
|
|
|
|
flags.IntVar(&port, "p", 8080, port_usage+" (shorthand)")
|
2013-09-02 23:35:48 -04:00
|
|
|
flags.StringVar(&rpcSock, "sock", sock_default, sock_usage)
|
|
|
|
flags.StringVar(&rpcSock, "s", sock_default, sock_usage+" (shorthand)")
|
2013-09-02 09:36:03 -04:00
|
|
|
flags.Parse(args)
|
2013-02-20 23:43:01 -05:00
|
|
|
|
2013-08-23 00:09:03 -04:00
|
|
|
rpcTornDown := make(chan int)
|
2013-09-02 23:35:48 -04:00
|
|
|
go StartRPC(rpcSock, rpcTornDown, adb)
|
2013-08-23 00:09:03 -04:00
|
|
|
|
2013-02-20 23:43:01 -05:00
|
|
|
http.HandleFunc("/", rootHandler)
|
|
|
|
http.HandleFunc("/events", eventHandler)
|
|
|
|
http.HandleFunc("/events/", eventHandler)
|
|
|
|
|
2013-08-23 00:09:03 -04:00
|
|
|
//TODO add HTTPS, something like http://golang.org/pkg/net/http/#ListenAndServeTLS
|
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
2013-02-20 23:43:01 -05:00
|
|
|
if err != nil {
|
2013-08-23 00:09:03 -04:00
|
|
|
panic(err)
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
2013-08-23 00:09:03 -04:00
|
|
|
defer l.Close()
|
|
|
|
go http.Serve(l, nil)
|
|
|
|
//TODO handle errors from http.Serve?
|
|
|
|
|
2013-09-03 22:21:18 -04:00
|
|
|
asink.WaitOnExit()
|
2013-08-23 00:09:03 -04:00
|
|
|
<-rpcTornDown
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
|
2013-09-02 23:35:48 -04:00
|
|
|
func StopServer(args []string) {
|
|
|
|
flags := flag.NewFlagSet("stop", flag.ExitOnError)
|
|
|
|
flags.StringVar(&rpcSock, "sock", sock_default, sock_usage)
|
|
|
|
flags.StringVar(&rpcSock, "s", sock_default, sock_usage+" (shorthand)")
|
|
|
|
flags.Parse(args)
|
|
|
|
|
|
|
|
i := 99
|
|
|
|
returnCode := 0
|
2013-09-03 23:33:36 -04:00
|
|
|
err := asink.RPCCall(rpcSock, "ServerStopper.StopServer", &returnCode, &i)
|
2013-09-02 23:35:48 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 23:43:01 -05:00
|
|
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "You're probably looking for /events/")
|
|
|
|
}
|
|
|
|
|
2013-09-02 09:36:03 -04:00
|
|
|
func getEvents(w http.ResponseWriter, r *http.Request, user *User, 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-08-28 23:05:28 -04:00
|
|
|
events, err := adb.DatabaseRetrieveEvents(nextEvent, 50, user)
|
2013-02-22 00:06:10 -05:00
|
|
|
if err != nil {
|
|
|
|
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)
|
2013-08-28 23:05:28 -04:00
|
|
|
addPoller(user.Id, &c) //TODO support more than one share per user
|
2013-03-09 22:47:49 -05:00
|
|
|
e, ok := <-c
|
|
|
|
if ok {
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
|
2013-09-02 09:36:03 -04:00
|
|
|
func putEvents(w http.ResponseWriter, r *http.Request, user *User) {
|
2013-02-20 23:43:01 -05:00
|
|
|
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-08-28 23:05:28 -04:00
|
|
|
err = adb.DatabaseAddEvent(user, 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
|
|
|
|
2013-08-28 23:05:28 -04:00
|
|
|
broadcastToPollers(user.Id, 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) {
|
2013-08-23 00:09:03 -04:00
|
|
|
user := AuthenticateUser(r)
|
|
|
|
if user == nil {
|
2013-08-29 07:36:16 -04:00
|
|
|
w.Header().Set("WWW-Authenticate", "Basic realm=\"Asink Server\"")
|
2013-08-23 00:09:03 -04:00
|
|
|
apiresponse := asink.APIResponse{
|
|
|
|
Status: asink.ERROR,
|
|
|
|
Explanation: "This operation requires user authentication",
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(apiresponse)
|
|
|
|
if err != nil {
|
|
|
|
b = []byte(err.Error())
|
|
|
|
}
|
2013-08-29 07:36:16 -04:00
|
|
|
w.WriteHeader(401)
|
2013-08-23 00:09:03 -04:00
|
|
|
w.Write(b)
|
|
|
|
return
|
|
|
|
}
|
2013-02-20 23:43:01 -05:00
|
|
|
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")
|
2013-08-28 23:05:28 -04:00
|
|
|
getEvents(w, r, user, 0)
|
2013-02-20 23:43:01 -05:00
|
|
|
} else {
|
2013-08-28 23:05:28 -04:00
|
|
|
getEvents(w, r, user, i)
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-08-28 23:05:28 -04:00
|
|
|
getEvents(w, r, user, 0)
|
2013-02-20 23:43:01 -05:00
|
|
|
}
|
|
|
|
} else if r.Method == "POST" {
|
2013-08-28 23:05:28 -04:00
|
|
|
putEvents(w, r, user)
|
2013-02-20 23:43:01 -05:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
2013-08-23 00:09:03 -04:00
|
|
|
|
2013-09-02 09:36:03 -04:00
|
|
|
func AuthenticateUser(r *http.Request) (user *User) {
|
2013-08-23 00:09:03 -04:00
|
|
|
h, ok := r.Header["Authorization"]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
authparts := strings.Split(h[0], " ")
|
|
|
|
if len(authparts) != 2 || authparts[0] != "Basic" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
userpass, err := base64.StdEncoding.DecodeString(authparts[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
splituserpass := strings.Split(string(userpass), ":")
|
|
|
|
if len(splituserpass) != 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err = adb.DatabaseGetUser(splituserpass[0])
|
|
|
|
if err != nil || user == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.ValidPassword(splituserpass[1]) {
|
|
|
|
return user
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|