This repository has been archived on 2025-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
bactdb/api/handler.go

36 lines
770 B
Go

package api
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/thermokarst/bactdb/datastore"
"github.com/thermokarst/bactdb/router"
)
var (
store = datastore.NewDatastore(nil)
schemaDecoder = schema.NewDecoder()
)
func Handler() *mux.Router {
m := router.API()
m.Get(router.User).Handler(handler(serveUser))
m.Get(router.CreateUser).Handler(handler(serveCreateUser))
m.Get(router.Users).Handler(handler(serveUsers))
return m
}
type handler func(http.ResponseWriter, *http.Request) error
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := h(w, r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "error: %s", err)
log.Println(err)
}
}