Skip to content

Commit

Permalink
feat: implement collection and payment
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Oct 22, 2023
1 parent cc38eb2 commit 3bb5c76
Show file tree
Hide file tree
Showing 36 changed files with 1,732 additions and 235 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: Swatinem/rust-cache@v2
- name: Build the Docker image
run: |
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker buildx create --use
docker login --username ${{ secrets.CR_USERNAME }} --password ${{ secrets.CR_PASSWORD }} ${{ secrets.CR_REGISTRY }}
IMAGE_TAG="${{ secrets.CR_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}"
TAGS="-t ${IMAGE_TAG}"
docker buildx build --platform='linux/amd64,linux/arm64' $TAGS --push .
- uses: actions/checkout@v3
- uses: Swatinem/rust-cache@v2
- name: Build the Docker image
run: |
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker buildx create --use
docker login --username ${{ secrets.CR_USERNAME }} --password ${{ secrets.CR_PASSWORD }} ${{ secrets.CR_REGISTRY }}
IMAGE_TAG="${{ secrets.CR_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}"
TAGS="-t ${IMAGE_TAG}"
docker buildx build --platform='linux/amd64,linux/arm64' $TAGS --push .
28 changes: 14 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ jobs:
go-version: ['1.21.x']

steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 1
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 1

- name: Print Go version
run: go version
- name: Print Go version
run: go version

- name: Get dependencies
run: go get -v -t -d ./...
- name: Get dependencies
run: go get -v -t -d ./...

- name: Run tests
run: go test -v -failfast -tags=test -timeout="3m" -race ./...
- name: Run tests
run: make test
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ run-dev:
@CONFIG_FILE_PATH=${PWD}/config.toml APP_ENV=dev go run main.go

test:
@CONFIG_FILE_PATH=${PWD}/config/default.toml APP_ENV=test go test ./...
@EXEC_DIR_PATH=${PWD} CONFIG_FILE_PATH=${PWD}/config/default.toml APP_ENV=test go test -v -failfast -tags=test -timeout="3m" -race ./...

lint:
@hash golint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
Expand Down
49 changes: 49 additions & 0 deletions cmd/keys/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"encoding/base64"
"flag"
"os"

"github.com/fxamacker/cbor/v2"
"github.com/ldclabs/cose/iana"
"github.com/ldclabs/cose/key"
"github.com/ldclabs/cose/key/aesgcm"
"github.com/ldclabs/cose/key/hmac"
)

var kind = flag.String("kind", "state", "generate key for kind")
var out = flag.String("out", "./keys/out.key", "write key to a file")

func main() {
flag.Parse()

var err error
var k key.Key
var data []byte

switch *kind {
case "hmac":
k, err = hmac.GenerateKey(iana.AlgorithmHMAC_256_64)
case "aesgcm":
k, err = aesgcm.GenerateKey(iana.AlgorithmA256GCM)
default:
panic("unsupported kind")
}

if err == nil {
// data, err = k.MarshalCBOR()
data, err = cbor.Marshal(cbor.Tag{
Number: 55799, // self described CBOR Tag
Content: k,
})
}

if err == nil {
err = os.WriteFile(*out, []byte(base64.RawURLEncoding.EncodeToString(data)), 0644)
}

if err != nil {
panic(err)
}
}
48 changes: 48 additions & 0 deletions cmd/tiktoken/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"flag"
"fmt"
"os"
"unicode/utf8"

"github.com/pkoukk/tiktoken-go"
tiktoken_loader "github.com/pkoukk/tiktoken-go-loader"
)

var help = flag.Bool("help", false, "show help info")
var version = flag.Bool("version", false, "show version info")

func main() {
flag.Parse()
if *help || *version {
fmt.Println("tiktoken example.txt")
os.Exit(0)
}

args := flag.Args()
if len(args) == 0 {
fmt.Println("tiktoken example.txt")
os.Exit(0)
}
file, err := os.ReadFile(args[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
text := string(file)
if !utf8.ValidString(text) {
fmt.Println("invalid utf8 text")
os.Exit(1)
}

tiktoken.SetBpeLoader(tiktoken_loader.NewOfflineLoader())
tk, err := tiktoken.GetEncoding("cl100k_base")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Printf("%s %d tokens\n", args[0], len(tk.Encode(text, nil, nil)))
os.Exit(0)
}
4 changes: 4 additions & 0 deletions config/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ addr = ":8080"
# The maximum number of seconds to wait for graceful shutdown.
graceful_shutdown = 10

[keys]
hmac = "./keys/hmac.key"
aesgcm = "./keys/aesgcm.key"

[redis]
prefix = "YWAPI:"
node = "127.0.0.1:6379"
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ require (
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible
github.com/bsm/redislock v0.9.4
github.com/fxamacker/cbor/v2 v2.5.0
github.com/gabriel-vasile/mimetype v1.4.2
github.com/gabriel-vasile/mimetype v1.4.3
github.com/go-playground/validator/v10 v10.15.5
github.com/google/uuid v1.3.1
github.com/jaevor/go-nanoid v1.3.0
github.com/klauspost/compress v1.17.0
github.com/klauspost/compress v1.17.1
github.com/ldclabs/cose v1.1.2
github.com/pkoukk/tiktoken-go v0.1.6
github.com/pkoukk/tiktoken-go-loader v0.0.1
github.com/redis/go-redis/v9 v9.2.1
github.com/rs/xid v1.5.0
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d
github.com/stretchr/testify v1.8.4
github.com/teambition/gear v1.27.3
go.uber.org/dig v1.17.0
go.uber.org/dig v1.17.1
golang.org/x/text v0.13.0
)

Expand Down
24 changes: 10 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE=
github.com/fxamacker/cbor/v2 v2.5.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-http-utils/cookie v1.3.1 h1:GCdTeqVV5vDcjP7LrgYpH8pbt3dOYKS+Wrs7Jo3/k/w=
github.com/go-http-utils/cookie v1.3.1/go.mod h1:ATl4rfG3bEemjiVa+8WIfgNcBUWdYBTasfXKjJ3Avt8=
github.com/go-http-utils/negotiator v1.0.0 h1:Qp1zofD6Nw7KXApXa3pAjehP06Js0ILguEBCnHhZeVA=
Expand All @@ -39,10 +39,14 @@ github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg=
github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g=
github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/ldclabs/cose v1.1.2 h1:kq8IkpCiTM2jcynmPbEUH4dPQ4tM8+qQewKMvuC/ljo=
github.com/ldclabs/cose v1.1.2/go.mod h1:M52HratClumnAkI1icUIUljX4fWfZL7kF80hh6ijGrQ=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/pion/dtls/v2 v2.2.7 h1:cSUBsETxepsCSFSxC3mc/aDo14qQLMSL+O6IjG28yV8=
github.com/pion/dtls/v2 v2.2.7/go.mod h1:8WiMkebSHFD0T+dIU+UeBaoV7kDhOW5oDCzZ7WZ/F9s=
github.com/pkoukk/tiktoken-go v0.1.6 h1:JF0TlJzhTbrI30wCvFuiw6FzP2+/bR+FIxUdgEAcUsw=
github.com/pkoukk/tiktoken-go v0.1.6/go.mod h1:9NiV+i9mJKGj1rYOT+njbv+ZwA/zJxYdewGl6qVatpg=
github.com/pkoukk/tiktoken-go-loader v0.0.1 h1:aOB2gRFzZTCCPi3YsOQXJO771P/5876JAsdebMyazig=
Expand Down Expand Up @@ -70,20 +74,12 @@ github.com/teambition/trie-mux v1.5.2 h1:ALTagFwKZXkn1vfSRlODlmoZg+NMeWAm4dyBPQI
github.com/teambition/trie-mux v1.5.2/go.mod h1:0Woh4KOHSN9bkJ66eWmLs8ltrEKw+fnZbFaHFfbMrtc=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
go.uber.org/dig v1.17.0 h1:5Chju+tUvcC+N7N6EV08BJz41UZuO3BmHcN4A287ZLI=
go.uber.org/dig v1.17.0/go.mod h1:rTxpf7l5I0eBTlE6/9RL+lDybC7WFwY2QH55ZSjy1mU=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
go.uber.org/dig v1.17.1 h1:Tga8Lz8PcYNsWsyHMZ1Vm0OQOUaJNDyvPImgbAu9YSc=
go.uber.org/dig v1.17.1/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos=
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
Expand Down
1 change: 1 addition & 0 deletions keys/aesgcm.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2dn3pAEEAlRkHwywt4RHc4QPkRFC1Tzcuov7CgMDIFggtG5vgaB8sZJqQ1HDpQ78QBQLYO7HHUnXutsovGCAArI
1 change: 1 addition & 0 deletions keys/hmac.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2dn3pAEEAlScZRWIAJlBA18cw7mHo3bB03xW9gMEIFggBQ13neTam4zLiMMLn2Hk6J5jT3TQlS5eHegh4AO_Qh0
Loading

0 comments on commit 3bb5c76

Please sign in to comment.