This commit is contained in:
Yoshiyuki Mineo
2025-05-02 01:01:34 +09:00
parent 8d195df6f7
commit 6ff253cccd
14 changed files with 821 additions and 24 deletions

6
v2/example/Dockerfile Normal file
View File

@@ -0,0 +1,6 @@
FROM ubuntu:14.04
ADD ./sonyflake_server /
ENTRYPOINT ["/sonyflake_server"]
EXPOSE 8080

View File

@@ -0,0 +1,8 @@
{
"AWSEBDockerrunVersion": "1",
"Ports": [
{
"ContainerPort": "8080"
}
]
}

21
v2/example/README.md Normal file
View 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
View File

@@ -0,0 +1,2 @@
#!/bin/sh
GOOS=linux GOARCH=amd64 go build sonyflake_server.go

View File

@@ -0,0 +1,42 @@
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
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)
}