mirror of
https://github.com/sony/sonyflake.git
synced 2026-01-08 20:33:06 +00:00
* feat(v2): make bit assignment for time/sequence/machine customizable via Settings; update all logic and tests * gofmt
52 lines
941 B
Go
52 lines
941 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/sony/sonyflake/v2"
|
|
"github.com/sony/sonyflake/v2/awsutil"
|
|
)
|
|
|
|
var sf *sonyflake.Sonyflake
|
|
|
|
func init() {
|
|
var st sonyflake.Settings
|
|
st.MachineID = awsutil.AmazonEC2MachineID
|
|
|
|
var err error
|
|
sf, err = sonyflake.New(st)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
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(sf.Decompose(id))
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header()["Content-Type"] = []string{"application/json; charset=utf-8"}
|
|
_, err = w.Write(body)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/", handler)
|
|
err := http.ListenAndServe(":8080", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|