mirror of
https://github.com/sony/sonyflake.git
synced 2026-01-10 13:17:20 +00:00
Upgrade Sonyflake to v2 (#66)
* Add v2 * Introduce staticcheck * Introduce golangci-lint * Add error checks * Add error checks * Lint v2 code * Improve CI trigger * Use io.ReadAll * Use int64 * Remove NewSonyflake * Fix errors * v2: Change MachineID, sequence, and AmazonEC2MachineID to int; update all usage and tests for type consistency * docs: update Settings struct in README to use int for MachineID and CheckMachineID (v2) * docs(v2): clarify Settings, StartTime, MachineID, and CheckMachineID comments and update README links and explanations * docs(v2/mock): improve comments and docstrings for mock implementations * docs(types): unify and clarify package and type docstrings for types.go in v1 and v2 * test(v2): refactor and modernize tests, improve error assertions, and update mocks for v2 * test(v2): normalize whitespace in pseudoSleep calls for consistency * feat(v2): add configurable TimeUnit and refactor time handling for So… (#67) * feat(v2): add configurable TimeUnit and refactor time handling for Sonyflake v2 * test(v2): add ToTime tests, clarify TimeUnit behavior, and update docs for v2 * gofmt
This commit is contained in:
6
v2/example/Dockerfile
Normal file
6
v2/example/Dockerfile
Normal file
@@ -0,0 +1,6 @@
|
||||
FROM ubuntu:24.04
|
||||
|
||||
ADD ./sonyflake_server /
|
||||
ENTRYPOINT ["/sonyflake_server"]
|
||||
|
||||
EXPOSE 8080
|
||||
8
v2/example/Dockerrun.aws.json
Normal file
8
v2/example/Dockerrun.aws.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"AWSEBDockerrunVersion": "1",
|
||||
"Ports": [
|
||||
{
|
||||
"ContainerPort": "8080"
|
||||
}
|
||||
]
|
||||
}
|
||||
21
v2/example/README.md
Normal file
21
v2/example/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
Example
|
||||
=======
|
||||
|
||||
This example runs Sonyflake on AWS Elastic Beanstalk.
|
||||
|
||||
Setup
|
||||
-----
|
||||
|
||||
1. Build the cross compiler for linux/amd64 if using other platforms.
|
||||
|
||||
```
|
||||
cd $GOROOT/src && GOOS=linux GOARCH=amd64 ./make.bash
|
||||
```
|
||||
|
||||
2. Build sonyflake_server in the example directory.
|
||||
|
||||
```
|
||||
./linux64_build.sh
|
||||
```
|
||||
|
||||
3. Upload the example directory to AWS Elastic Beanstalk.
|
||||
2
v2/example/linux64_build.sh
Executable file
2
v2/example/linux64_build.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
GOOS=linux GOARCH=amd64 go build sonyflake_server.go
|
||||
51
v2/example/sonyflake_server.go
Normal file
51
v2/example/sonyflake_server.go
Normal file
@@ -0,0 +1,51 @@
|
||||
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(sonyflake.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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user