-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (43 loc) · 1.05 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
package main
import (
"fmt"
"log"
"encoding/json"
"net/http"
"github.com/namsral/flag"
"github.com/sony/sonyflake"
)
func nextId(sf *sonyflake.Sonyflake) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
id, err := sf.NextID()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
body, err := json.Marshal(sonyflake.Decompose(id))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header()["Content-Type"] = []string{"application/json; charset=utf-8"}
w.Write(body)
}
}
func main() {
var sf *sonyflake.Sonyflake
var st sonyflake.Settings
sf = sonyflake.NewSonyflake(st)
if sf == nil {
panic("Sonyflake not created")
}
var (
port = flag.Int("port", 80, "Listen port for server")
)
flag.Parse()
http.HandleFunc("/", nextId(sf))
// start up http server
log.Printf("Ready and listening on port %d\n", *port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
panic(err)
}
}