-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (42 loc) · 1018 Bytes
/
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
package main
import (
"fmt"
"strconv"
//"encoding/json"
"net/http"
"get_uid/pkg/sonyflake"
"github.com/drone/routes"
)
var gl sonyflake.GlobalVal
var gs *sonyflake.GlobalVal
// Initalize the global config
// the default count of goroutine is 10
func InitId() {
gl.Poolsize = 10
gs = gl.NewGlobal(gl.Poolsize)
gs.GenId()
}
func main() {
InitId()
mux := routes.New()
mux.Get("/user/:uid", getuid)
http.Handle("/", mux)
http.ListenAndServe(":8080", nil)
}
// the restful api
// call it by curl localhost:port/user/getuid
func getuid(w http.ResponseWriter, r *http.Request) {
id, err := gs.GetId()
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"}
s := fmt.Sprintf("id:%s\n", strconv.Itoa(int(id)))
w.Write([]byte(s))
}