-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.go
44 lines (34 loc) · 837 Bytes
/
server.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
package main
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/bstick12/goflake"
"github.com/gorilla/mux"
)
var generator = goflake.GoFlakeInstanceUsingUnique("D01Z01")
func main() {
startServer()
}
func startServer() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/ids", Id)
router.Queries("count", "{count:[0-9]+}")
log.Fatal(http.ListenAndServe(":8080", router))
}
func Id(w http.ResponseWriter, r *http.Request) {
values := r.URL.Query()
countVar := values["count"]
var count = 1
if len(countVar) == 1 {
count, _ = strconv.Atoi(countVar[0])
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
ids := []string{}
for i := 0; i < count; i++ {
ids = append(ids, generator.GetBase64UUID())
}
json.NewEncoder(w).Encode(ids)
}