|
|
|
@ -0,0 +1,42 @@ |
|
|
|
package main |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"io" |
|
|
|
"log" |
|
|
|
"math/rand" |
|
|
|
"net/http" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
func main() { |
|
|
|
fs := http.FileServer(http.Dir("./static")) |
|
|
|
nextId := func(w http.ResponseWriter, _ *http.Request) { |
|
|
|
next := base56Id3() |
|
|
|
//log.Printf("Generating ID: %s", next)
|
|
|
|
io.WriteString(w, next+"\n") |
|
|
|
} |
|
|
|
http.Handle("/", fs) |
|
|
|
http.HandleFunc("/next", nextId) |
|
|
|
|
|
|
|
log.Println("Listening on :8080...") |
|
|
|
err := http.ListenAndServe(":8080", nil) |
|
|
|
if err != nil { |
|
|
|
log.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func base56Id3() string { |
|
|
|
//Base56 alphabet with "ambiguous" characters removed: [0oOIl1]
|
|
|
|
alphabet := "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ" |
|
|
|
//Initialize rand with the time - this is a problem with multiple requests in the same ns
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano())) |
|
|
|
var b bytes.Buffer |
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
n := r.Intn(len(alphabet)) |
|
|
|
char := alphabet[n] |
|
|
|
b.WriteByte(char) |
|
|
|
} |
|
|
|
|
|
|
|
return b.String() |
|
|
|
} |