CheqBooq/server/routes.go

63 lines
1.3 KiB
Go
Executable File

package server
import (
"fmt"
"local/cheqbooq/config"
"local/cheqbooq/server/account"
"local/cheqbooq/server/balance"
"local/cheqbooq/server/transaction"
"local/router"
"net/http"
)
func (s *Server) Routes() error {
routes := map[string]http.HandlerFunc{
"api/v1/balance": s.balance1,
"api/v1/transaction": s.transaction1,
"api/v1/account": s.account1,
fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard): s.public,
}
for path, handler := range routes {
if err := s.Add(path, handler); err != nil {
return err
}
}
return nil
}
func (s *Server) balance1(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
balance.Read(w, r)
default:
http.NotFound(w, r)
}
}
func (s *Server) transaction1(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
transaction.Read(w, r)
case http.MethodPatch:
transaction.Write(w, r)
default:
http.NotFound(w, r)
}
}
func (s *Server) account1(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
account.Read(w, r)
case http.MethodPatch:
account.Write(w, r)
default:
http.NotFound(w, r)
}
}
func (s *Server) public(w http.ResponseWriter, r *http.Request) {
d := http.FileServer(http.Dir(config.Public))
d.ServeHTTP(w, r)
}