From 2599717d094d9ac9eb4f9d9fd11717d1ff617ae9 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Mon, 2 Sep 2013 09:36:03 -0400 Subject: [PATCH] Combine server into single 'git-style' executable --- {server => asinkd}/admin_rpc.go | 2 +- {server => asinkd}/database.go | 2 +- {server => asinkd}/exit.go | 2 +- {server/server => asinkd}/longpolling.go | 0 server/admin/admin.go => asinkd/main.go | 15 ++++++---- {server/admin => asinkd}/rpc.go | 0 server/admin/users.go => asinkd/rpc_users.go | 29 ++++++++++---------- {server/server => asinkd}/server.go | 29 ++++++++++---------- {server => asinkd}/users.go | 2 +- 9 files changed, 43 insertions(+), 38 deletions(-) rename {server => asinkd}/admin_rpc.go (98%) rename {server => asinkd}/database.go (99%) rename {server => asinkd}/exit.go (98%) rename {server/server => asinkd}/longpolling.go (100%) rename server/admin/admin.go => asinkd/main.go (79%) rename {server/admin => asinkd}/rpc.go (100%) rename server/admin/users.go => asinkd/rpc_users.go (83%) rename {server/server => asinkd}/server.go (90%) rename {server => asinkd}/users.go (97%) diff --git a/server/admin_rpc.go b/asinkd/admin_rpc.go similarity index 98% rename from server/admin_rpc.go rename to asinkd/admin_rpc.go index 26d275b..303f833 100644 --- a/server/admin_rpc.go +++ b/asinkd/admin_rpc.go @@ -1,4 +1,4 @@ -package server +package main import ( "net" diff --git a/server/database.go b/asinkd/database.go similarity index 99% rename from server/database.go rename to asinkd/database.go index 2169a65..08b3443 100644 --- a/server/database.go +++ b/asinkd/database.go @@ -1,4 +1,4 @@ -package server +package main import ( "asink" diff --git a/server/exit.go b/asinkd/exit.go similarity index 98% rename from server/exit.go rename to asinkd/exit.go index fad888b..263e7fe 100644 --- a/server/exit.go +++ b/asinkd/exit.go @@ -1,4 +1,4 @@ -package server +package main import ( "os" diff --git a/server/server/longpolling.go b/asinkd/longpolling.go similarity index 100% rename from server/server/longpolling.go rename to asinkd/longpolling.go diff --git a/server/admin/admin.go b/asinkd/main.go similarity index 79% rename from server/admin/admin.go rename to asinkd/main.go index daf4063..0744753 100644 --- a/server/admin/admin.go +++ b/asinkd/main.go @@ -5,24 +5,29 @@ import ( "os" ) -type AdminCommand struct { +type Command struct { cmd string fn func(args []string) explanation string } -var commands []AdminCommand = []AdminCommand{ - AdminCommand{ +var commands []Command = []Command{ + Command{ + cmd: "start", + fn: StartServer, + explanation: "Start the server daemon", + }, + Command{ cmd: "useradd", fn: UserAdd, explanation: "Add a user", }, - AdminCommand{ + Command{ cmd: "userdel", fn: UserDel, explanation: "Remove a user", }, - AdminCommand{ + Command{ cmd: "usermod", fn: UserMod, explanation: "Modify a user", diff --git a/server/admin/rpc.go b/asinkd/rpc.go similarity index 100% rename from server/admin/rpc.go rename to asinkd/rpc.go diff --git a/server/admin/users.go b/asinkd/rpc_users.go similarity index 83% rename from server/admin/users.go rename to asinkd/rpc_users.go index c0a3817..1504068 100644 --- a/server/admin/users.go +++ b/asinkd/rpc_users.go @@ -1,7 +1,6 @@ package main import ( - "asink/server" "code.google.com/p/gopass" "flag" "fmt" @@ -56,20 +55,20 @@ func UserAdd(args []string) { os.Exit(1) } - user := new(server.User) + user := new(User) if *admin { - user.Role = server.ADMIN + user.Role = ADMIN } else { - user.Role = server.NORMAL + user.Role = NORMAL } user.Username = flags.Arg(0) - user.PWHash = server.HashPassword(passwordOne) + user.PWHash = HashPassword(passwordOne) i := 99 err = RPCCall("UserModifier.AddUser", user, &i) 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()) return } @@ -83,13 +82,13 @@ func UserDel(args []string) { os.Exit(1) } - user := new(server.User) + user := new(User) user.Username = args[0] i := 99 err := RPCCall("UserModifier.RemoveUser", user, &i) 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()) return } @@ -98,9 +97,9 @@ func UserDel(args []string) { } func UserMod(args []string) { - rpcargs := new(server.UserModifierArgs) - rpcargs.Current = new(server.User) - rpcargs.Updated = new(server.User) + rpcargs := new(UserModifierArgs) + rpcargs.Current = new(User) + rpcargs.Updated = new(User) admin := newBoolIsSetFlag(false) @@ -137,15 +136,15 @@ func UserMod(args []string) { fmt.Println("Error: Passwords do not match. Please try again.") 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 rpcargs.UpdateRole = admin.IsSet if admin.Value { - rpcargs.Updated.Role = server.ADMIN + rpcargs.Updated.Role = ADMIN } else { - rpcargs.Updated.Role = server.NORMAL + rpcargs.Updated.Role = NORMAL } if !rpcargs.UpdateRole && !rpcargs.UpdateLogin && !rpcargs.UpdatePassword { @@ -156,7 +155,7 @@ func UserMod(args []string) { i := 99 err := RPCCall("UserModifier.ModifyUser", rpcargs, &i) 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()) return } diff --git a/server/server/server.go b/asinkd/server.go similarity index 90% rename from server/server/server.go rename to asinkd/server.go index 901a5d9..f10bb3f 100644 --- a/server/server/server.go +++ b/asinkd/server.go @@ -2,7 +2,6 @@ package main import ( "asink" - "asink/server" "encoding/base64" "encoding/json" "flag" @@ -18,28 +17,30 @@ import ( //global variables var eventsRegexp *regexp.Regexp var port int = 8080 -var adb *server.AsinkDB +var adb *AsinkDB func init() { 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]+)$") - adb, err = server.GetAndInitDB() + adb, err = GetAndInitDB() if err != nil { panic(err) } } -func main() { - flag.Parse() +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)") + + flags.Parse(args) rpcTornDown := make(chan int) - go server.StartRPC(rpcTornDown, adb) + go StartRPC(rpcTornDown, adb) http.HandleFunc("/", rootHandler) http.HandleFunc("/events", eventHandler) @@ -54,7 +55,7 @@ func main() { go http.Serve(l, nil) //TODO handle errors from http.Serve? - server.WaitOnExit() + WaitOnExit() <-rpcTornDown } @@ -62,7 +63,7 @@ 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, user *server.User, nextEvent uint64) { +func getEvents(w http.ResponseWriter, r *http.Request, user *User, nextEvent uint64) { var events []*asink.Event var error_message string = "" 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 error_message string = "" 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"] if !ok { return nil diff --git a/server/users.go b/asinkd/users.go similarity index 97% rename from server/users.go rename to asinkd/users.go index 66be0c5..8febe8c 100644 --- a/server/users.go +++ b/asinkd/users.go @@ -1,4 +1,4 @@ -package server +package main import ( "crypto/sha256"