aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: b7ae65c6ac0b389830616c3eaacc77783f4f786f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main

import (
	"html/template"
	"log"
	"net/http"
	"os"
	"sync/atomic"

	"embed"
)

/*
#include <stdint.h>
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)
}