-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (54 loc) · 1.18 KB
/
main.go
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
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"encoding/binary"
"encoding/hex"
"fmt"
"html/template"
"math/rand"
"net/http"
"regexp"
)
type Link struct {
Link string
Title string
}
var idRegex = regexp.MustCompile("^[abcdefABCDEF0123456789]{16}$")
var tmpl = template.Must(template.ParseFiles("page.html"))
const domain = "http://localhost:8080"
func pageHandler(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if idRegex.MatchString(id) {
bytes, err := hex.DecodeString(id)
if err != nil {
panic("failed to decode")
}
num := binary.BigEndian.Uint64(bytes)
links := make([]Link, 64)
shuffle := rand.Perm(64)
for i := uint64(0); i < 64; i += 1 {
newNum := num ^ (uint64(0x01) << i)
newId := fmt.Sprintf("%016x", newNum)
links[shuffle[i]] = Link{
Title: newId,
Link: domain + "/?id=" + newId,
}
}
err = tmpl.Execute(w, links)
if err != nil {
panic("Failed to execute template")
}
} else {
_, err := fmt.Fprintf(w, "Invalid id")
if err != nil {
panic("Failed to write body")
}
}
}
func main() {
fmt.Println("vim-go")
http.HandleFunc("/", pageHandler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}