Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
小滋润 committed Nov 17, 2024
1 parent 0f7b159 commit 971b953
Show file tree
Hide file tree
Showing 34 changed files with 1,080 additions and 676 deletions.
54 changes: 54 additions & 0 deletions .github/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 服务初始化的配置文件 执行代码详见 ./initialize/initialize.go
# This is the service initialization configuration file execution code see ./initialize/initialize.go

# can set `debug` or `production`
# `debug` mode can print mysql and redis logs and `/public/swagger/index.html` api
Mode: debug

System:
Addr: 8080
RouterPrefix: ""
JwtKey: ""
# Some apis use symmetric signature keys that can be configured to improve security
ClientSignKey: ""

Redis:
Addr: "localhost:6379"
Password: ""
Db: 0
LockDb: 1

Mysql:
Path: "localhost"
Port: "3306"
Config: "parseTime=True&loc=Local"
DbName: "leap_ledger"
Username: "root"
Password: ""

Nats:
ServerUrl: localhost:4222
# This is the topic that the consumer server needs to subscribe to, such as createTourist, statisticUpdate, transactionSync.
# subjects see ./global/nats/nats.go
Subjects: [all]

Captcha:
KeyLong: 6
ImgWidth: 180
ImgHeight: 50
OpenCaptcha: 0
OpenCaptchaTimeout: 3600
EmailCaptcha: 0
EmailCaptchaTimeOut: 3600

ThirdParty:
# WeCom used to send domain email
WeCom:
CorpId: ""
CorpSecret: ""
# Ai is used to obtain Chinese similarity, which is used to match transaction types
# https://huggingface.co/google-bert/bert-base-chinese
Ai:
Host: "" # leap-ledger-ai-server
Port: "" # 5000
MinSimilarity: 0.85
4 changes: 3 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ jobs:
uses: ./.github/workflows/common-test-and-update-docs.yml
with:
ref: ${{ github.ref }}

secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

push-image:
needs: test-and-update-docs
uses: ./.github/workflows/common-push-image.yml
Expand Down
76 changes: 58 additions & 18 deletions .github/workflows/common-test-and-update-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ on:
ref:
required: true
type: string
secrets:
CODECOV_TOKEN:
required: true
permissions:
contents: read

jobs:
test-and-update-docs:
Expand All @@ -15,43 +20,78 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Build image
run: |
docker build -t xiaozirun/leap-ledger:latest -f docker/Dockerfile .
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.23.0

- name: Set up Docker Compose
run: |
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
- name: Start server
- name: Copy config.yaml
run: cp -f .github/config.yaml config.yaml

- name: Start mysql
run: |
docker-compose -f docker-compose.yaml up -d leap-ledger-mysql leap-ledger-nats leap-ledger-redis
- name: Wait for MySQL to be ready
run: |
docker-compose -f docker-compose.yaml up -d leap-ledger-mysql && docker-compose -f docker-compose.yaml up -d
# Set the maximum retry count and interval (e.g., 30 retries, 5 seconds each)
max_retries=30
retries=0
until docker exec leap-ledger-mysql mysqladmin -u root -p"$MYSQL_ROOT_PASSWORD" ping --silent; do
if [ $retries -ge $max_retries ]; then
echo "MySQL failed to start after $max_retries retries!"
exit 1
fi
retries=$((retries + 1))
echo "Waiting for MySQL to be ready... Retry $retries/$max_retries"
sleep 5
done
- name: Run tests and make report
- name: Go mod
run: |
docker exec leap-ledger-server sh -c "go install github.com/jstemmer/go-junit-report@latest"
docker exec leap-ledger-server sh -c "go test -v 2>&1 ./... -coverprofile=docs/coverage.out | go-junit-report > docs/test-report.xml"
docker exec leap-ledger-server sh -c "go tool cover -html=docs/coverage.out -o docs/coverage.html"
continue-on-error: false
sudo chmod -R 666 ./docker
go mod download
go mod tidy
- name: Exec test
run: |
go install github.com/jstemmer/go-junit-report/v2@latest
go test -timeout 5m -v 2>&1 ./... -coverprofile=docs/coverage.out | tee docs/test-process.log
go-junit-report < docs/test-process.log > docs/test-report.xml
- name: Upload test-report.xml
uses: actions/upload-artifact@v4
with:
name: test-report.xml
path: docs/test-report.xml

- name: Upload coverage.html
uses: actions/upload-artifact@v4
- name: Upload results to Codecov
uses: codecov/codecov-action@v4
with:
name: coverage-report
path: docs/coverage.html
files: docs/coverage.out
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Execute updateDocs.sh in container
run: docker exec leap-ledger-server sh "./docs/updateDocs.sh"
- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
files: docs/test-report.xml
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Update docs
run: |
sudo chmod -R 777 ./
go install github.com/swaggo/swag/cmd/swag@latest
swag init -p pascalcase
- name: Commit and push changes
# https://github.com/actions/checkout/discussions/479
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
Expand All @@ -61,7 +101,7 @@ jobs:
else
git commit -m "update docs" || echo "No changes to commit."
git push origin ${{ inputs.ref }}
fi
continue-on-error: true

- name: Docker Compose Down
run: |
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/develop-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
uses: ./.github/workflows/common-test-and-update-docs.yml
with:
ref: ${{ github.ref }}
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

push-image:
needs: test-and-update-docs
Expand Down
2 changes: 1 addition & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ docker-compose up -d
```
Access http://localhost:8080/public/health to verify the service.

For custom configurations, refer to: [./config.yaml](./config.yaml)
If you don't want to rely on Docker, you can modify the request addresses of mysql, nats, and redis in the [./config.yaml](./config.yaml) file and run it locally

For client packaging details, visit: https://github.com/ZiRunHua/LeapLedger-App

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ docker-compose up -d
```
访问http://localhost:8080/public/health 验证服务

自定义配置详见:[./config.yaml](./config.yaml)
如果不想依赖Docker,可以修改[./config.yaml](./config.yaml)文件中的mysql、nats和redis的请求地址等信息后直接在本地运行

客户端打包详见:https://github.com/ZiRunHua/LeapLedger-App

Expand Down
8 changes: 4 additions & 4 deletions api/v1/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"context"
"errors"
"golang.org/x/sync/errgroup"
"time"

"github.com/ZiRunHua/LeapLedger/api/request"
Expand All @@ -16,7 +17,6 @@ import (
userModel "github.com/ZiRunHua/LeapLedger/model/user"
"github.com/ZiRunHua/LeapLedger/util/timeTool"
"github.com/gin-gonic/gin"
"github.com/songzhibin97/gkit/egroup"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -570,7 +570,7 @@ func (a *AccountApi) GetUserInfo(ctx *gin.Context) {
return
}
accountUser, account, nowTime := contextFunc.GetAccountUser(ctx), contextFunc.GetAccount(ctx), contextFunc.GetNowTime(ctx)
group := egroup.WithContext(ctx)
var group errgroup.Group
var todayTotal, monthTotal *global.IEStatisticWithTime
var recentTrans *response.TransactionDetailList
for _, infoType := range requestData.Types {
Expand Down Expand Up @@ -990,9 +990,9 @@ func (a *AccountApi) GetInfo(ctx *gin.Context) {
return nil
}
// process and response
var group *egroup.Group
var group *errgroup.Group
if len(types) > 1 {
group = egroup.WithContext(ctx)
group = &errgroup.Group{}
}
for i := range types {
t := types[i]
Expand Down
5 changes: 2 additions & 3 deletions api/v1/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"golang.org/x/sync/errgroup"
"time"

"github.com/ZiRunHua/LeapLedger/api/request"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"github.com/pkg/errors"
"github.com/songzhibin97/gkit/egroup"
)

type UserApi struct {
Expand Down Expand Up @@ -458,8 +458,7 @@ func (u *UserApi) Home(ctx *gin.Context) {
if false == pass {
return
}

group := egroup.WithContext(ctx)
var group errgroup.Group
nowTime, timeLocation := account.GetNowTime(), account.GetTimeLocation()
year, month, day := nowTime.Date()
var todayData, yesterdayData, weekData, monthData, yearData response.TransactionStatistic
Expand Down
7 changes: 4 additions & 3 deletions docs/beforeDocsMake/renameModel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package main

import (
"bufio"
"github.com/ZiRunHua/LeapLedger/global/constant"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/ZiRunHua/LeapLedger/global/constant"
)

func main() {
handleDir(constant.WORK_PATH + "/api/request/")
handleDir(constant.WORK_PATH + "/api/response/")
handleDir(filepath.Clean(constant.RootDir + "/api/request/"))
handleDir(filepath.Clean(constant.RootDir + "/api/response/"))
}
func handleDir(path string) {
_ = filepath.Walk(
Expand Down
50 changes: 41 additions & 9 deletions global/constant/constant.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package constant

type ServerMode string

var Debug, Production ServerMode = "debug", "production"
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

const WORK_PATH = "/go/LeapLedger"
const RUNTIME_DATA_PATH = WORK_PATH + "/runtime/data"
type ServerMode string

const LOG_PATH = WORK_PATH + "/log"
const DATA_PATH = WORK_PATH + "/data"
const Debug, Production ServerMode = "debug", "production"

var ExampleAccountJsonPath = DATA_PATH + "/template/account/example.json"
var (
RootDir = getRootDir()
LogPath = filepath.Join(RootDir, "log")
DataPath = filepath.Join(RootDir, "data")
ExampleAccountJsonPath = filepath.Clean(DataPath + "/template/account/example.json")
)

// IncomeExpense 收支类型
type IncomeExpense string //@name IncomeExpense `example:"expense" enums:"income,expense" swaggertype:"string"`
type IncomeExpense string // @name IncomeExpense `example:"expense" enums:"income,expense" swaggertype:"string"`

const (
Income IncomeExpense = "income"
Expand Down Expand Up @@ -82,3 +89,28 @@ const (
// nats

type Subject string

func getRootDir() string {
// `os.Getwd()` is avoided here because, during tests, the working directory is set to the test file’s directory.
// This command retrieves the module's root directory instead.
// Source of `go list` usage: https://stackoverflow.com/a/75943840/23658318
rootDir, err := exec.Command("go", "list", "-m", "-f", "{{.Dir}}").Output()
if err == nil {
return strings.TrimSpace(string(rootDir))
}
// If `go list` fails, it may indicate the absence of a Go environment.
// In such cases, this suggests we are not in a test environment, so fall back to `os.Getwd()` to set `RootDir`.
workDir, err := os.Getwd()
if err != nil {
panic(err)
}
// Validate that the directory exists
_, err = os.Stat(workDir)
if err != nil {
if os.IsNotExist(err) {
panic(fmt.Sprintf("Path:%s does not exists", workDir))
}
panic(err)
}
return workDir
}
11 changes: 6 additions & 5 deletions global/cron/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ package cron

import (
"context"
"path/filepath"

"github.com/ZiRunHua/LeapLedger/global"
"github.com/ZiRunHua/LeapLedger/global/constant"
"github.com/ZiRunHua/LeapLedger/global/nats"
"github.com/ZiRunHua/LeapLedger/initialize"
"github.com/ZiRunHua/LeapLedger/util/log"
"go.uber.org/zap"
)

const logPath = constant.LOG_PATH + "/cron.log"

var (
logger *zap.Logger
logPath = filepath.Join(constant.LogPath, "cron.log")
logger *zap.Logger

Scheduler = initialize.Scheduler
)

func init() {
var err error
logger, err = log.GetNewZapLogger(logPath)
logger, err = global.Config.Logger.New(logPath)
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit 971b953

Please sign in to comment.