mirror of
https://github.com/sony/sonyflake.git
synced 2026-01-05 02:53:55 +00:00
43 lines
810 B
Go
43 lines
810 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/sony/sonyflake"
|
|
"github.com/sony/sonyflake/awsutil"
|
|
)
|
|
|
|
var sf *sonyflake.Sonyflake
|
|
|
|
func init() {
|
|
var st sonyflake.Settings
|
|
st.MachineID = awsutil.AmazonEC2MachineID
|
|
sf = sonyflake.NewSonyflake(st)
|
|
if sf == nil {
|
|
panic("sonyflake not created")
|
|
}
|
|
}
|
|
|
|
func handler(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() {
|
|
http.HandleFunc("/", handler)
|
|
http.ListenAndServe(":8080", nil)
|
|
}
|