Select Git revision

meskio authored
api.go 2.82 KiB
package api
import (
"log"
"0xacab.org/meskio/cicer/api/db"
"github.com/gorilla/mux"
)
type api struct {
db *db.DB
signKey []byte
mail *Mail
}
func Init(dbPath string, signKey string, mail *Mail, r *mux.Router) error {
database, err := db.Init(dbPath)
if err != nil {
return err
}
a := api{database, []byte(signKey), mail}
go a.refundOrders()
go a.cleanPaswordResets()
token, err := a.newToken(0, "admin", false)
log.Print(token)
r.HandleFunc("/signin", a.SignIn).Methods("POST")
r.HandleFunc("/token", a.GetToken).Methods("GET")
r.HandleFunc("/reset", a.SendPasswordReset).Methods("POST")
r.HandleFunc("/reset/{token}", a.ValidatePasswordReset).Methods("GET")
r.HandleFunc("/reset/{token}", a.PasswordReset).Methods("PUT")
r.HandleFunc("/member", a.authAdmin(a.ListMembers)).Methods("GET")
r.HandleFunc("/member", a.authAdmin(a.AddMember)).Methods("POST")
r.HandleFunc("/member/batch", a.authAdmin(a.AddBatchMember)).Methods("POST")
r.HandleFunc("/member/me", a.authNum(a.getMemberNum)).Methods("GET")
r.HandleFunc("/member/me", a.authNum(a.UpdateMemberMe)).Methods("PUT")
r.HandleFunc("/member/{num:[0-9]+}", a.authAdmin(a.GetMember)).Methods("GET")
r.HandleFunc("/member/{num:[0-9]+}", a.authAdmin(a.UpdateMember)).Methods("PUT")
r.HandleFunc("/member/{num:[0-9]+}", a.authAdmin(a.DeleteMember)).Methods("DELETE")
r.HandleFunc("/member/{num:[0-9]+}/transactions", a.authAdmin(a.GetMemberTransactions)).Methods("GET")
r.HandleFunc("/member/{num:[0-9]+}/purchase", a.authAdminNum(a.AddMemberPurchase)).Methods("POST")
r.HandleFunc("/product", a.auth(a.ListProducts)).Methods("GET")
r.HandleFunc("/product", a.authAdmin(a.AddProduct)).Methods("POST")
r.HandleFunc("/product/{code:[0-9]+}", a.auth(a.GetProduct)).Methods("GET")
r.HandleFunc("/product/{code:[0-9]+}", a.authAdmin(a.UpdateProduct)).Methods("PUT")
r.HandleFunc("/product/{code:[0-9]+}", a.authAdmin(a.DeleteProduct)).Methods("DELETE")
r.HandleFunc("/transaction", a.authAdmin(a.ListTransactions)).Methods("GET")
r.HandleFunc("/transaction/{id:[0-9]+}", a.authNumRole(a.GetTransaction)).Methods("GET")
r.HandleFunc("/transaction/mine", a.authNum(a.getTransactionsByMember)).Methods("GET")
r.HandleFunc("/purchase", a.authNum(a.AddPurchase)).Methods("POST")
r.HandleFunc("/topup", a.authAdminNum(a.AddTopup)).Methods("POST")
r.HandleFunc("/order", a.auth(a.ListOrders)).Methods("GET")
r.HandleFunc("/order", a.authNum(a.AddOrder)).Methods("POST")
r.HandleFunc("/order/{id:[0-9]+}", a.authNum(a.GetOrder)).Methods("GET")
r.HandleFunc("/order/{id:[0-9]+}", a.authNumRole(a.DeleteOrder)).Methods("DELETE")
r.HandleFunc("/order/active", a.auth(a.ListActiveOrders)).Methods("GET")
r.HandleFunc("/order/picks", a.authNum(a.ListOrderPicks)).Methods("GET")
r.HandleFunc("/order/{id:[0-9]+}/purchase", a.authNum(a.AddOrderPurchase)).Methods("POST")
return nil
}