parent
2d3d6bea05
commit
ec230678ed
@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//NewHTTPHandler creates a new http handler
|
||||
func NewHTTPHandler(bm BlocklistManager) http.Handler {
|
||||
return newAPIHandler(nil, bm)
|
||||
}
|
||||
|
||||
func newAPIHandler(inner http.Handler, bm BlocklistManager) http.Handler {
|
||||
l := log.New(os.Stdout, "[HTTP API] ", log.LUTC|log.Lshortfile)
|
||||
apiHandler := http.NewServeMux()
|
||||
|
||||
/*
|
||||
1. update block lists
|
||||
*/
|
||||
apiHandler.HandleFunc("/api/gopherhole/blocklists/reload", func(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed, must be a POST", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
if err := bm.Reload(req.Context()); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
l.Println("reloaded blocklists successfully")
|
||||
})
|
||||
|
||||
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if strings.HasPrefix(req.Host, "/api/gopherhole") {
|
||||
apiHandler.ServeHTTP(res, req)
|
||||
return
|
||||
}
|
||||
|
||||
handleDefaultRequests(res, req)
|
||||
})
|
||||
}
|
||||
|
||||
func handleDefaultRequests(res http.ResponseWriter, req *http.Request) {
|
||||
log.Printf("got request for: %s", req.URL.Hostname())
|
||||
}
|
Loading…
Reference in new issue