You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

62 lines
1.6 KiB

package main
import (
"encoding/json"
"net/http"
"github.com/bvinc/go-sqlite-lite/sqlite3"
"git.sporestack.com/SporeStack/settlers"
)
const startingBalance = 0
func enable(token, depositOnlyToken, adminToken string, db *sqlite3.Conn, conf Configuration) (userErr, err error) {
// We should consider doing balance first to know if it's a DB error
// or a user error.
if userErr = settlers.ValidateToken(token); userErr != nil {
return
}
if userErr = settlers.ValidateToken(depositOnlyToken); userErr != nil {
return
}
if userErr = settlers.ValidateToken(adminToken); userErr != nil {
return
}
if adminToken != conf.AdminToken {
userErr = settlers.ErrorIncorrectAdminToken
return
}
// userErr because of constraint issues but debatable.
// If the user tries to add two tokens, it should fail and not be considered our failure.
userErr = db.Exec("INSERT INTO balances (combined_token, combined_deposit_only_token, balance) VALUES (?, ?, ?)", token, depositOnlyToken, startingBalance)
if userErr != nil {
userErr = settlers.ErrorTokenAlreadyEnabled
}
return
}
func httpEnable(w http.ResponseWriter, r *http.Request, db *sqlite3.Conn, conf Configuration) {
defer r.Body.Close()
var request settlers.EnableRequest
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
userErr := decoder.Decode(&request)
if userErr != nil {
httpHandle400(w, userErr)
return
}
userErr, err := enable(request.CombinedToken, request.CombinedDepositOnlyToken, request.AdminToken, db, conf)
if userErr != nil {
httpHandle400(w, userErr)
return
}
if err != nil {
httpHandle500(w, err)
return
}
return
}