From 1a5a86a5d4557200db20e20206bcdd7b9b2a7e55 Mon Sep 17 00:00:00 2001 From: hovertank3d Date: Sat, 18 Jan 2025 23:27:52 +0100 Subject: refactor and add /about --- about.go | 24 +++++++++++++++++ build.sh | 2 +- cmd/main.go | 19 +++++++++++++ log2.s | 7 +++-- main.go | 53 ------------------------------------ server.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++ templates/gen.go | 1 + templates/index.html | 18 ++++++++++--- templates/link.html | 1 + templates/log2.s.html | 11 -------- templates/reset.html | 2 +- templates/source_code.html | 1 - 12 files changed, 133 insertions(+), 73 deletions(-) create mode 100644 about.go create mode 100644 cmd/main.go delete mode 100644 main.go create mode 100644 server.go create mode 100644 templates/link.html delete mode 100644 templates/log2.s.html delete mode 100644 templates/source_code.html diff --git a/about.go b/about.go new file mode 100644 index 0000000..e005d55 --- /dev/null +++ b/about.go @@ -0,0 +1,24 @@ +// i made this site to serve some of my future demos and fuuny +// projects. currently there are only couple of interesting things i wrote: + +// * {{ template "link.html" (arr "46load" "https://github.com/hovertank3d/46load")}}, +// * {{ template "link.html" (arr "fire" "https://github.com/hovertank3d/fire")}}, +// * {{ template "link.html" (arr "bfc" "https://github.com/hovertank3d/bfc")}}, +// * {{ template "link.html" (arr "enigma16" "https://github.com/hovertank3d/enigma16")}}, + +// so, come back when i'll write something + +// also, this site uses {{ template "link.html" (arr "genhl" "https://github.com/hovertank3d/genhl")}} to go:generate syntax highlight + +package lzcnt + +import "net/http" + +func (s *Server) aboutPage(w http.ResponseWriter, r *http.Request) { + payload := map[string]any{ + "Page": "about.go.html", + "Title": "About", + } + + tmpl.ExecuteTemplate(w, "index.html", payload) +} diff --git a/build.sh b/build.sh index 42d2c23..bf7ea6c 100755 --- a/build.sh +++ b/build.sh @@ -4,4 +4,4 @@ pushd deps/genhl make popd go generate ./... -go build -o lzcnt.space \ No newline at end of file +go build -o lzcnt.space ./cmd \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..5e8b373 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "os" + + "github.com/hovertank3d/lzcnt.space" +) + +import "C" + +func main() { + host := ":8080" + if len(os.Args) == 2 { + host = os.Args[1] + } + + srv := lzcnt.New() + srv.ListenAndServe(host) +} diff --git a/log2.s b/log2.s index 910f6c7..cae631b 100644 --- a/log2.s +++ b/log2.s @@ -1,10 +1,13 @@ +# {{template "link.html" (arr "about" "/about")}} +# {{template "reset.html" .}} .section .text .global log2lzcnt .type log2lzcnt, @function -# {{template "source_code.html" .}} -# {{template "reset.html" .}} log2lzcnt: # log2lzcnt({{.Requests}}); lzcnt %rdi, %rdi movq $63, %rax sub %rdi, %rax ret # 2^{{.Log2lzcnt}} requests handled + +# copyleft 2025 iskrim46 +# {{template "link.html" (arr "source" "https://github.com/hovertank3d/lzcnt.space")}} diff --git a/main.go b/main.go deleted file mode 100644 index b7ae65c..0000000 --- a/main.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "html/template" - "log" - "net/http" - "os" - "sync/atomic" - - "embed" -) - -/* -#include -extern unsigned long int log2lzcnt(unsigned long int); -*/ -import "C" - -//go:embed templates -var templates embed.FS - -func main() { - counter := atomic.Uint64{} - host := ":8080" - if len(os.Args) == 2 { - host = os.Args[1] - } - - tmpl, err := template.ParseFS(templates, "templates/*.html") - if err != nil { - log.Fatal(err) - } - - http.HandleFunc("/reset", func(w http.ResponseWriter, r *http.Request) { - counter.Store(0) - http.Redirect(w, r, "/", http.StatusSeeOther) - }) - - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - n := counter.Add(1) - payload := struct { - Requests uint64 - Log2lzcnt uint64 - }{ - Requests: n, - Log2lzcnt: uint64(C.log2lzcnt(C.uint64_t(n))), - } - - tmpl.Execute(w, payload) - }) - - http.ListenAndServe(host, nil) -} 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 +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) +} diff --git a/templates/gen.go b/templates/gen.go index dbbaf24..0b5f5ab 100644 --- a/templates/gen.go +++ b/templates/gen.go @@ -1,3 +1,4 @@ package templates //go:generate bash -c "cat ../log2.s | ../deps/genhl/genhl asmatt > log2.s.html" +//go:generate bash -c "cat ../about.go | ../deps/genhl/genhl > about.go.html" diff --git a/templates/index.html b/templates/index.html index 85eccab..3ae5cf4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -19,16 +19,26 @@ LZCNT - -
+ +
-

LZCNT - Count the Number of Leading Zero Bits

+

LZCNT - {{.Title}}

- {{template "log2.s.html" .}} + {{ if eq .Page "log2.s.html" }} + {{template "log2.s.html" .}} + {{ end }} + {{ if eq .Page "about.go.html"}} + {{template "about.go.html" .}} + {{ end }}
diff --git a/templates/link.html b/templates/link.html new file mode 100644 index 0000000..9a43d43 --- /dev/null +++ b/templates/link.html @@ -0,0 +1 @@ +[{{index . 0}}] \ No newline at end of file diff --git a/templates/log2.s.html b/templates/log2.s.html deleted file mode 100644 index f2fb03b..0000000 --- a/templates/log2.s.html +++ /dev/null @@ -1,11 +0,0 @@ -
    .section    .text
-    .global     log2lzcnt
-    .type       log2lzcnt, @function
-# {{template "source_code.html" .}}
-# {{template "reset.html" .}}
-log2lzcnt:              # log2lzcnt({{.Requests}});
-    lzcnt   %rdi, %rdi 
-    movq    $63,  %rax
-    sub     %rdi, %rax
-    ret                 # 2^{{.Log2lzcnt}} requests handled
-
\ No newline at end of file diff --git a/templates/reset.html b/templates/reset.html index 090b273..d6bfa7e 100644 --- a/templates/reset.html +++ b/templates/reset.html @@ -1 +1 @@ -[reset counter] \ No newline at end of file +[reset counter]
\ No newline at end of file diff --git a/templates/source_code.html b/templates/source_code.html deleted file mode 100644 index 631c649..0000000 --- a/templates/source_code.html +++ /dev/null @@ -1 +0,0 @@ -[source code] \ No newline at end of file -- cgit v1.2.3