You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
919 B
42 lines
919 B
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 :80...")
|
|
err := http.ListenAndServe(":80", 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()
|
|
}
|