Combine server into single 'git-style' executable
This commit is contained in:
parent
a6c296e0fd
commit
2599717d09
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package main
|
||||
|
||||
import (
|
||||
"asink"
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
@ -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",
|
@ -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
|
||||
}
|
@ -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
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
Loading…
Reference in New Issue
Block a user