Combine server into single 'git-style' executable

This commit is contained in:
Aaron Lindsay 2013-09-02 09:36:03 -04:00
parent a6c296e0fd
commit 2599717d09
9 changed files with 43 additions and 38 deletions

View File

@ -1,4 +1,4 @@
package server package main
import ( import (
"net" "net"

View File

@ -1,4 +1,4 @@
package server package main
import ( import (
"asink" "asink"

View File

@ -1,4 +1,4 @@
package server package main
import ( import (
"os" "os"

View File

@ -5,24 +5,29 @@ import (
"os" "os"
) )
type AdminCommand struct { type Command struct {
cmd string cmd string
fn func(args []string) fn func(args []string)
explanation string explanation string
} }
var commands []AdminCommand = []AdminCommand{ var commands []Command = []Command{
AdminCommand{ Command{
cmd: "start",
fn: StartServer,
explanation: "Start the server daemon",
},
Command{
cmd: "useradd", cmd: "useradd",
fn: UserAdd, fn: UserAdd,
explanation: "Add a user", explanation: "Add a user",
}, },
AdminCommand{ Command{
cmd: "userdel", cmd: "userdel",
fn: UserDel, fn: UserDel,
explanation: "Remove a user", explanation: "Remove a user",
}, },
AdminCommand{ Command{
cmd: "usermod", cmd: "usermod",
fn: UserMod, fn: UserMod,
explanation: "Modify a user", explanation: "Modify a user",

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"asink/server"
"code.google.com/p/gopass" "code.google.com/p/gopass"
"flag" "flag"
"fmt" "fmt"
@ -56,20 +55,20 @@ func UserAdd(args []string) {
os.Exit(1) os.Exit(1)
} }
user := new(server.User) user := new(User)
if *admin { if *admin {
user.Role = server.ADMIN user.Role = ADMIN
} else { } else {
user.Role = server.NORMAL user.Role = NORMAL
} }
user.Username = flags.Arg(0) user.Username = flags.Arg(0)
user.PWHash = server.HashPassword(passwordOne) user.PWHash = HashPassword(passwordOne)
i := 99 i := 99
err = RPCCall("UserModifier.AddUser", user, &i) err = RPCCall("UserModifier.AddUser", user, &i)
if err != nil { if err != nil {
if _, ok := err.(rpc.ServerError); ok && err.Error() == server.DuplicateUsernameErr.Error() { if _, ok := err.(rpc.ServerError); ok && err.Error() == DuplicateUsernameErr.Error() {
fmt.Println("Error: " + err.Error()) fmt.Println("Error: " + err.Error())
return return
} }
@ -83,13 +82,13 @@ func UserDel(args []string) {
os.Exit(1) os.Exit(1)
} }
user := new(server.User) user := new(User)
user.Username = args[0] user.Username = args[0]
i := 99 i := 99
err := RPCCall("UserModifier.RemoveUser", user, &i) err := RPCCall("UserModifier.RemoveUser", user, &i)
if err != nil { if err != nil {
if _, ok := err.(rpc.ServerError); ok && err.Error() == server.NoUserErr.Error() { if _, ok := err.(rpc.ServerError); ok && err.Error() == NoUserErr.Error() {
fmt.Println("Error: " + err.Error()) fmt.Println("Error: " + err.Error())
return return
} }
@ -98,9 +97,9 @@ func UserDel(args []string) {
} }
func UserMod(args []string) { func UserMod(args []string) {
rpcargs := new(server.UserModifierArgs) rpcargs := new(UserModifierArgs)
rpcargs.Current = new(server.User) rpcargs.Current = new(User)
rpcargs.Updated = new(server.User) rpcargs.Updated = new(User)
admin := newBoolIsSetFlag(false) admin := newBoolIsSetFlag(false)
@ -137,15 +136,15 @@ func UserMod(args []string) {
fmt.Println("Error: Passwords do not match. Please try again.") fmt.Println("Error: Passwords do not match. Please try again.")
os.Exit(1) os.Exit(1)
} }
rpcargs.Updated.PWHash = server.HashPassword(passwordOne) rpcargs.Updated.PWHash = HashPassword(passwordOne)
} }
//set the UpdateRole flag based on whether it was present on the command-line //set the UpdateRole flag based on whether it was present on the command-line
rpcargs.UpdateRole = admin.IsSet rpcargs.UpdateRole = admin.IsSet
if admin.Value { if admin.Value {
rpcargs.Updated.Role = server.ADMIN rpcargs.Updated.Role = ADMIN
} else { } else {
rpcargs.Updated.Role = server.NORMAL rpcargs.Updated.Role = NORMAL
} }
if !rpcargs.UpdateRole && !rpcargs.UpdateLogin && !rpcargs.UpdatePassword { if !rpcargs.UpdateRole && !rpcargs.UpdateLogin && !rpcargs.UpdatePassword {
@ -156,7 +155,7 @@ func UserMod(args []string) {
i := 99 i := 99
err := RPCCall("UserModifier.ModifyUser", rpcargs, &i) err := RPCCall("UserModifier.ModifyUser", rpcargs, &i)
if err != nil { if err != nil {
if _, ok := err.(rpc.ServerError); ok && err.Error() == server.NoUserErr.Error() { if _, ok := err.(rpc.ServerError); ok && err.Error() == NoUserErr.Error() {
fmt.Println("Error: " + err.Error()) fmt.Println("Error: " + err.Error())
return return
} }

View File

@ -2,7 +2,6 @@ package main
import ( import (
"asink" "asink"
"asink/server"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"flag" "flag"
@ -18,28 +17,30 @@ import (
//global variables //global variables
var eventsRegexp *regexp.Regexp var eventsRegexp *regexp.Regexp
var port int = 8080 var port int = 8080
var adb *server.AsinkDB var adb *AsinkDB
func init() { func init() {
var err error var err error
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]+)$") eventsRegexp = regexp.MustCompile("^/events/([0-9]+)$")
adb, err = server.GetAndInitDB() adb, err = GetAndInitDB()
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
func main() { func StartServer(args []string) {
flag.Parse() 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)")
flags.Parse(args)
rpcTornDown := make(chan int) rpcTornDown := make(chan int)
go server.StartRPC(rpcTornDown, adb) go StartRPC(rpcTornDown, adb)
http.HandleFunc("/", rootHandler) http.HandleFunc("/", rootHandler)
http.HandleFunc("/events", eventHandler) http.HandleFunc("/events", eventHandler)
@ -54,7 +55,7 @@ func main() {
go http.Serve(l, nil) go http.Serve(l, nil)
//TODO handle errors from http.Serve? //TODO handle errors from http.Serve?
server.WaitOnExit() WaitOnExit()
<-rpcTornDown <-rpcTornDown
} }
@ -62,7 +63,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "You're probably looking for /events/") fmt.Fprintf(w, "You're probably looking for /events/")
} }
func getEvents(w http.ResponseWriter, r *http.Request, user *server.User, nextEvent uint64) { func getEvents(w http.ResponseWriter, r *http.Request, user *User, nextEvent uint64) {
var events []*asink.Event var events []*asink.Event
var error_message string = "" var error_message string = ""
defer func() { defer func() {
@ -103,7 +104,7 @@ func getEvents(w http.ResponseWriter, r *http.Request, user *server.User, nextEv
} }
} }
func putEvents(w http.ResponseWriter, r *http.Request, user *server.User) { func putEvents(w http.ResponseWriter, r *http.Request, user *User) {
var events asink.EventList var events asink.EventList
var error_message string = "" var error_message string = ""
defer func() { defer func() {
@ -192,7 +193,7 @@ func eventHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
func AuthenticateUser(r *http.Request) (user *server.User) { func AuthenticateUser(r *http.Request) (user *User) {
h, ok := r.Header["Authorization"] h, ok := r.Header["Authorization"]
if !ok { if !ok {
return nil return nil

View File

@ -1,4 +1,4 @@
package server package main
import ( import (
"crypto/sha256" "crypto/sha256"