added http handler and routes for adding users

This commit is contained in:
majormjr 2016-05-06 10:49:01 -04:00
parent 350441feca
commit 59396f2b52
6 changed files with 90 additions and 15 deletions

2
.gitignore vendored
View File

@ -1,6 +1,6 @@
node_modules/
bundle.js
factorio-server-manager
auth.level-db*
auth.leveldb*
conf.json
*.exe

19
auth.go
View File

@ -13,9 +13,10 @@ type AuthHTTP struct {
}
type User struct {
Username string
Password string
Role string
Username string `json:"username"`
Password string `json:"password"`
Role string `json:"role"`
Email string `json:"email"`
}
func initAuth() *AuthHTTP {
@ -61,6 +62,8 @@ func (auth *AuthHTTP) createInitialUser(username, password, role, email string)
return err
}
log.Printf("Created initial user: %s", user.Username)
return nil
}
@ -74,3 +77,13 @@ func (auth *AuthHTTP) listUsers() ([]httpauth.UserData, error) {
log.Printf("listing users: %+v", users)
return users, nil
}
func (auth *AuthHTTP) addUser(username, password, email, role string) error {
user := httpauth.UserData{Username: username, Hash: []byte(password), Email: email, Role: role}
err := Auth.backend.SaveUser(user)
if err != nil {
log.Printf("Error creating user %v: %s", user, err)
}
log.Printf("Added user: %v", user)
return nil
}

View File

@ -441,8 +441,6 @@ func StartServer(w http.ResponseWriter, r *http.Request) {
case "POST":
log.Printf("Starting Factorio server.")
// TODO get form parameters for starting server
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error in starting factorio server handler body: %s", err)
@ -550,7 +548,7 @@ func CheckServer(w http.ResponseWriter, r *http.Request) {
status := map[string]string{}
status["status"] = "stopped"
resp.Data = status
log.Printf("Server not running, creating status response: %v", status)
log.Printf("Server not running, creating status response: %v", resp)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error encoding config file JSON reponse: ", err)
}
@ -679,3 +677,63 @@ func ListUsers(w http.ResponseWriter, r *http.Request) {
log.Printf("Error getting user status: %s", err)
}
}
func AddUser(w http.ResponseWriter, r *http.Request) {
resp := JSONResponse{
Success: false,
}
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
switch r.Method {
case "GET":
log.Printf("GET not supported for add user handler")
resp.Data = "Unsupported method"
resp.Success = false
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error adding user: %s", err)
}
case "POST":
user := User{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error in reading add user POST: %s", err)
resp.Data = fmt.Sprintf("Error in adding user: %s", err)
resp.Success = false
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error adding user: %s", err)
}
return
}
log.Printf("Adding user: %v", string(body))
err = json.Unmarshal(body, &user)
if err != nil {
log.Printf("Error unmarshaling user add JSON: %s", err)
resp.Data = fmt.Sprintf("Error in adding user: %s", err)
resp.Success = false
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error adding user: %s", err)
}
return
}
err = Auth.addUser(user.Username, user.Password, user.Email, user.Role)
if err != nil {
log.Printf("Error in adding user: %s", err)
resp.Data = fmt.Sprintf("Error in adding user: %s", err)
resp.Success = false
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error adding user: %s", err)
}
return
}
resp.Success = true
resp.Data = fmt.Sprintf("User: %s successfully added.", user.Username)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error in returning added user response: %s", err)
}
}
}

View File

@ -89,7 +89,7 @@ func main() {
}
router := NewRouter()
Auth.listUsers()
fmt.Printf("Starting server on: %s:%s", config.ServerIP, config.ServerPort)
log.Fatal(http.ListenAndServe(config.ServerIP+":"+config.ServerPort, router))
}

View File

@ -23,6 +23,10 @@ func NewRouter() *mux.Router {
Methods("GET").
Name("Login").
Handler(http.StripPrefix("/login", http.FileServer(http.Dir("./app/"))))
r.Path("/login").
Methods("POST").
Name("LoginPOST").
HandlerFunc(LoginUser)
// API subrouter
// Serves all JSON REST handlers prefixed with /api
@ -31,7 +35,7 @@ func NewRouter() *mux.Router {
s.Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
Handler(CheckSession(route.HandlerFunc))
}
// Serves the frontend application from the app directory
@ -165,11 +169,6 @@ var apiRoutes = Routes{
"GET",
"/server/status",
CheckServer,
}, {
"LoginUser",
"POST",
"/login",
LoginUser,
}, {
"LogoutUser",
"GET",
@ -185,5 +184,10 @@ var apiRoutes = Routes{
"GET",
"/user/list",
ListUsers,
}, {
"AddUser",
"POST",
"/user/add",
AddUser,
},
}

View File

@ -20,7 +20,7 @@ class LoginContent extends React.Component {
$.ajax({
type: "POST",
url: "/api/login",
url: "/login",
dataType: "json",
data: JSON.stringify(user),
success: (resp) => {