Files
sonyflake/v2/example/sonyflake_server.go
Yoshiyuki Mineo 7ee8f154df feat(v2): make bit assignment for time/sequence/machine customizable … (#68)
* feat(v2): make bit assignment for time/sequence/machine customizable via Settings; update all logic and tests

* gofmt
2025-05-05 14:16:21 +09:00

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)
}
}