Skip to content

Instantly share code, notes, and snippets.

@recoilme
Created July 30, 2020 14:08
Show Gist options
  • Save recoilme/e6d20fc3c67a7c090044af61ccea196c to your computer and use it in GitHub Desktop.
Save recoilme/e6d20fc3c67a7c090044af61ccea196c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"strings"
"time"
)
// Server is a HTTP interface
type Server struct {
Db McEngine
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
switch r.Method {
case http.MethodPost:
case http.MethodGet:
switch {
case path == "":
date := time.Now().UTC().Format(http.TimeFormat)
w.Header().Set("Date", date)
w.Header().Set("Server", "b52 "+version)
w.Header().Set("Connection", "Closed")
fmt.Fprint(w, "status = \"OK\"\r\n")
case strings.HasPrefix(path, "pulse:2:"):
s.api(path, w)
return
default:
w.WriteHeader(404)
}
}
}
func (s *Server) api(key string, w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
value, err := s.Db.Get([]byte(key))
if err != nil {
fmt.Println(err)
w.WriteHeader(500)
return
}
fmt.Fprint(w, string(value))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment