diff options
author | hovertank3d <[email protected]> | 2025-01-18 23:27:52 +0100 |
---|---|---|
committer | hovertank3d <[email protected]> | 2025-01-18 23:28:53 +0100 |
commit | 1a5a86a5d4557200db20e20206bcdd7b9b2a7e55 (patch) | |
tree | 03aae452a7dc2d9f71cf7a83adbfd8e112ad562e /server.go | |
parent | 2aa29060c81fb52884b290603f1cf930259443b9 (diff) | |
download | lzcnt.space-1a5a86a5d4557200db20e20206bcdd7b9b2a7e55.tar.xz lzcnt.space-1a5a86a5d4557200db20e20206bcdd7b9b2a7e55.zip |
refactor and add /about
Diffstat (limited to 'server.go')
-rw-r--r-- | server.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/server.go b/server.go new file mode 100644 index 0000000..6ea8e89 --- /dev/null +++ b/server.go @@ -0,0 +1,67 @@ +package lzcnt + +import ( + "embed" + "html/template" + "net/http" + "sync/atomic" +) + +/* +#include <stdint.h> +extern unsigned long int log2lzcnt(unsigned long int); +*/ +import "C" + +var ( + //go:embed templates + templates embed.FS + + tmpl = template.New("") +) + +func init() { + tmpl = tmpl.Funcs(template.FuncMap{ + "arr": func(els ...any) []any { + return els + }, + }) + + tmpl = template.Must(tmpl.ParseFS(templates, "templates/*.html")) +} + +type Server struct { + mux *http.ServeMux + requestCounter atomic.Uint64 +} + +func New() *Server { + srv := &Server{ + mux: http.NewServeMux(), + } + + srv.mux.HandleFunc("/reset", func(w http.ResponseWriter, r *http.Request) { + srv.requestCounter.Store(0) + http.Redirect(w, r, "/", http.StatusSeeOther) + }) + + srv.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + n := srv.requestCounter.Add(1) + payload := map[string]any{ + "Page": "log2.s.html", + "Title": "Count the Number of Leading Zero Bits", + "Requests": n, + "Log2lzcnt": uint64(C.log2lzcnt(C.uint64_t(n))), + } + + tmpl.ExecuteTemplate(w, "index.html", payload) + }) + + srv.mux.HandleFunc("/about", srv.aboutPage) + + return srv +} + +func (s *Server) ListenAndServe(host string) error { + return http.ListenAndServe(host, s.mux) +} |