mirror of
https://github.com/safing/portbase
synced 2025-09-11 07:44:43 +00:00
Finish first API version
This commit is contained in:
parent
d7e0602548
commit
31c09512a0
6 changed files with 175 additions and 22 deletions
|
@ -4,26 +4,44 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/Safing/portbase/log"
|
||||
)
|
||||
|
||||
var (
|
||||
additionalRoutes map[string]func(arg1 http.ResponseWriter, arg2 *http.Request)
|
||||
additionalRoutes map[string]http.Handler
|
||||
)
|
||||
|
||||
func RegisterAdditionalRoute(path string, handleFunc func(arg1 http.ResponseWriter, arg2 *http.Request)) {
|
||||
func RegisterAdditionalRoute(path string, handler http.Handler) {
|
||||
if additionalRoutes == nil {
|
||||
additionalRoutes = make(map[string]func(arg1 http.ResponseWriter, arg2 *http.Request))
|
||||
additionalRoutes = make(map[string]http.Handler)
|
||||
}
|
||||
additionalRoutes[path] = handleFunc
|
||||
additionalRoutes[path] = handler
|
||||
}
|
||||
|
||||
func logger(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ew := NewEnrichedResponseWriter(w)
|
||||
next.ServeHTTP(ew, r)
|
||||
log.Infof("api request: %s %d %s", r.RemoteAddr, ew.Status, r.RequestURI)
|
||||
})
|
||||
}
|
||||
|
||||
func Serve() {
|
||||
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/api/database/v1", startDatabaseAPI)
|
||||
// router.HandleFunc("/api/database/v1", startDatabaseAPI)
|
||||
|
||||
for path, handleFunc := range additionalRoutes {
|
||||
router.HandleFunc(path, handleFunc)
|
||||
for path, handler := range additionalRoutes {
|
||||
router.Handle(path, handler)
|
||||
}
|
||||
|
||||
router.Use(logger)
|
||||
|
||||
http.Handle("/", router)
|
||||
http.HandleFunc("/api/database/v1", startDatabaseAPI)
|
||||
|
||||
address := "127.0.0.1:18"
|
||||
log.Infof("api: starting to listen on %s", address)
|
||||
log.Errorf("api: failed to listen on %s: %s", address, http.ListenAndServe(address, nil))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue