diff options
author | Rasmus Dahlberg <rasmus@rgdd.se> | 2023-12-17 19:10:46 +0100 |
---|---|---|
committer | Rasmus Dahlberg <rasmus@rgdd.se> | 2023-12-17 19:10:46 +0100 |
commit | 5442e71e7710897126a0034f487fab7e5013b3cc (patch) | |
tree | 5fdade10956802b27afa4fcfcfc4b39c3640e137 /pkg/server/handler.go | |
parent | 895d5fea41177e444c18f4fdc820fffa5f67d5bf (diff) |
Drafty server package to receive node requests
curl http://localhost:2009/get-status
curl -X POST --data-binary @/home/rgdd/fullchain.pem -u node_a:aaaa http://localhost:2009/add-chain
Diffstat (limited to 'pkg/server/handler.go')
-rw-r--r-- | pkg/server/handler.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/pkg/server/handler.go b/pkg/server/handler.go new file mode 100644 index 0000000..6d17af7 --- /dev/null +++ b/pkg/server/handler.go @@ -0,0 +1,32 @@ +package server + +import ( + "fmt" + "net/http" +) + +// handler implements the http.Handler interface +type handler struct { + method string + endpoint string + callback func(w http.ResponseWriter, r *http.Request) (int, string) +} + +func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.Header().Set("Cache-Control", "no-cache") + if r.Method != h.method { + http.Error(w, "Invalid HTTP method, expected "+h.method, http.StatusMethodNotAllowed) + return + } + code, text := h.callback(w, r) + if code != http.StatusOK { + http.Error(w, text, code) + return + } + fmt.Fprintf(w, fmt.Sprintf("%s\n", text)) +} + +func (h handler) register(mux *http.ServeMux) { + mux.Handle("/"+h.endpoint, h) +} |