From 968922fffd53234da20634c52a77f6c04708e6ab Mon Sep 17 00:00:00 2001 From: "daniel.w" Date: Sat, 5 Oct 2024 14:55:28 +0800 Subject: [PATCH] init project --- .gitignore | 7 + .golangci.yaml | 148 ++++++++++++++++++ Makefile | 67 ++++++++ generate/database/mongodb/readme.md | 1 + .../20240828100000_create_schema.down.sql | 1 + .../20240828100000_create_schema.up.sql | 1 + generate/database/readme.md | 39 +++++ generate/protobuf/order.proto | 23 +++ readme.md | 1 + 9 files changed, 288 insertions(+) create mode 100644 .gitignore create mode 100644 .golangci.yaml create mode 100644 Makefile create mode 100644 generate/database/mongodb/readme.md create mode 100644 generate/database/mysql/create/20240828100000_create_schema.down.sql create mode 100644 generate/database/mysql/create/20240828100000_create_schema.up.sql create mode 100644 generate/database/readme.md create mode 100644 generate/protobuf/order.proto create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..480bfb9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.idea/ +go.sum +account/ +gen_result/ +etc/feed.yaml +client/ +.DS_Store diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..c9726ca --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,148 @@ +run: + timeout: 3m + # Exit code when at least one issue was found. + # Default: 1 + issues-exit-code: 2 + # Include test files or not. + # Default: true + tests: false + +# Reference URL: https://golangci-lint.run/usage/linters/ +linters: + # Disable everything by default so upgrades to not include new - default + # enabled- linters. + disable-all: true + # Specifically enable linters we want to use. + enable: + # - depguard + - errcheck + # - godot + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - misspell + - revive + # - staticcheck + - typecheck + - unused + # - wsl + - asasalint + - asciicheck + - bidichk + - bodyclose + # - containedctx + - contextcheck + # - cyclop + # - varnamelen + # - gci + - wastedassign + - whitespace + # - wrapcheck + - thelper + - tparallel + - unconvert + - unparam + - usestdlibvars + - tenv + - testableexamples + - stylecheck + - sqlclosecheck + - nosprintfhostport + - paralleltest + - prealloc + - predeclared + - promlinter + - reassign + - rowserrcheck + - nakedret + - nestif + - nilerr + - nilnil + - nlreturn + - noctx + - nolintlint + - nonamedreturns + - decorder + - dogsled + # - dupl + - dupword + - durationcheck + - errchkjson + - errname + - errorlint + # - execinquery + - exhaustive + - exportloopref + - forbidigo + - forcetypeassert + # - gochecknoglobals + - gochecknoinits + - gocognit + - goconst + - gocritic + - gocyclo + # - godox + # - goerr113 + # - gofumpt + - goheader + - gomoddirectives + # - gomodguard always failed + - goprintffuncname + - gosec + - grouper + - importas + - interfacebloat + # - ireturn + - lll + - loggercheck + - maintidx + - makezero + +issues: + exclude-rules: + - path: _test\.go + linters: + - funlen + - goconst + - interfacer + - dupl + - lll + - goerr113 + - errcheck + - gocritic + - cyclop + - wrapcheck + - gocognit + - contextcheck + + exclude-dirs: + - internal/model + + exclude-files: + - .*_test.go + + + +linters-settings: + gci: + sections: + - standard # Standard section: captures all standard packages. + - default # Default section: contains all imports that could not be matched to another section type. + gocognit: + # Minimal code complexity to report. + # Default: 30 (but we recommend 10-20) + min-complexity: 40 + nestif: + # Minimal complexity of if statements to report. + # Default: 5 + min-complexity: 10 + lll: + # Max line length, lines longer will be reported. + # '\t' is counted as 1 character by default, and can be changed with the tab-width option. + # Default: 120. + line-length: 200 + # Tab width in spaces. + # Default: 1 + tab-width: 1 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..beb3d7a --- /dev/null +++ b/Makefile @@ -0,0 +1,67 @@ +GO_CTL_NAME=goctl + +# go-zero 生成風格 +GO_ZERO_STYLE=go_zero + +GO ?= go +GOFMT ?= gofmt "-s" +GOFILES := $(shell find . -name "*.go") +LDFLAGS := -s -w +VERSION="v1.0.1" +DOCKER_REPO="igs170911/feed" + +.PHONY: test +test: # 進行測試 + go test -v --cover ./... + +.PHONY: fmt +fmt: # 格式優化 + $(GOFMT) -w $(GOFILES) + goimports -w ./ + golangci-lint run + +.PHONY: gen-rpc +gen-rpc: # 建立 rpc code + $(GO_CTL_NAME) rpc protoc ./generate/protobuf/order.proto -m --style=$(GO_ZERO_STYLE) --go_out=./gen_result/pb --go-grpc_out=./gen_result/pb --zrpc_out=. + go mod tidy + @echo "Generate core-api files successfully" + +.PHONY: gen-clean +gen-clean: # 建立 rpc code + rm -rf ./client + rm -rf ./etc + rm -rf ./gen_result + rm -rf ./internal + rm -rf go.mod + rm -rf go.sum + rm -rf order.go + @echo "Generate core-api files successfully" + +.PHONY: run-docker +run-docker: # 建立 rpc code + docker run --platform=linux/arm64/v8 -p 8080:8080 $(DOCKER_REPO):$(VERSION) + +.PHONY: build-docker +build-docker: + cp ./build/Dockerfile Dockerfile + docker buildx build -t $(DOCKER_REPO):$(VERSION) --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/ed_25519)" . + rm -rf Dockerfile + @echo "Generate core-api files successfully" + +gen-mongo-model: # 建立 rpc 資料庫 + # 只產生 Model 剩下的要自己撰寫,連欄位名稱也是 + goctl model mongo -t post --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) + goctl model mongo -t comment --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) + goctl model mongo -t tags --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) + goctl model mongo -t post_likes --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) + goctl model mongo -t comment_likes --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) + @echo "Generate mongo model files successfully" + +.PHONY: mock-gen +mock-gen: # 建立 mock 資料 + mockgen -source=./internal/model/mongo/post_model_gen.go -destination=./internal/mock/model/post_model_gen.go -package=mock + @echo "Generate mock files successfully" + +.PHONY: migrate-database +migrate-database: + migrate -source file://generate/database/migrations/mongodb -database 'mongodb://127.0.0.1:27017/digimon_tweeting' up diff --git a/generate/database/mongodb/readme.md b/generate/database/mongodb/readme.md new file mode 100644 index 0000000..5cbb27e --- /dev/null +++ b/generate/database/mongodb/readme.md @@ -0,0 +1 @@ +如果有需要可以把 mongo 放這邊 \ No newline at end of file diff --git a/generate/database/mysql/create/20240828100000_create_schema.down.sql b/generate/database/mysql/create/20240828100000_create_schema.down.sql new file mode 100644 index 0000000..3da4282 --- /dev/null +++ b/generate/database/mysql/create/20240828100000_create_schema.down.sql @@ -0,0 +1 @@ +DROP DATABASE IF EXISTS `digimon_order`; \ No newline at end of file diff --git a/generate/database/mysql/create/20240828100000_create_schema.up.sql b/generate/database/mysql/create/20240828100000_create_schema.up.sql new file mode 100644 index 0000000..ccfd4ec --- /dev/null +++ b/generate/database/mysql/create/20240828100000_create_schema.up.sql @@ -0,0 +1 @@ +CREATE DATABASE IF NOT EXISTS `digimon_order`; \ No newline at end of file diff --git a/generate/database/readme.md b/generate/database/readme.md new file mode 100644 index 0000000..4db6e02 --- /dev/null +++ b/generate/database/readme.md @@ -0,0 +1,39 @@ +# migrate +數據庫遷移工具 + +[golang-migrate](https://github.com/golang-migrate/migrate) + +## 安裝 make +```shell +brew install Makefile +``` + +## 安裝 golang-migrate +```shell +brew install golang-migrate +``` + +## 執行刪除 mysql schema + +```shell +migrate -source file://database/migrations/mysql -database 'mysql://account:password@tcp(127.0.0.1:3306)/esc_c2c' down +``` + +## 執行安裝 mysql schema + +```shell +migrate -source file://database/migrations/mysql -database 'mysql://account:password@tcp(127.0.0.1:3306)/esc_c2c' up +``` + +## 執行刪除 mongo schema + +```shell +migrate -source file://database/migrations/mongodb -database 'mongodb://127.0.0.1:27017/esc_c2c' down +``` + +## 執行安裝 mongo schema + +```shell +migrate -source file://database/migrations/mongodb -database 'mongodb://127.0.0.1:27017/esc_c2c' up +``` + diff --git a/generate/protobuf/order.proto b/generate/protobuf/order.proto new file mode 100644 index 0000000..ee764f6 --- /dev/null +++ b/generate/protobuf/order.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; +package tweeting; +option go_package = "./tweeting"; + +// ========== 基本回應 =========== +message OKResp {} + +// 空的請求 +message NoneReq {} + +// 分頁信息 +message Pager +{ + int64 total = 1; // 總數量 + int64 size = 2; // 每頁數量 + int64 index = 3; // 當前頁碼 +} + +// ========== 貼文區 =========== + + +// OrderService 訂單服務(業務邏輯在外面組合) +service OrderService{} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..7c1da10 --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +### 訂單系統 \ No newline at end of file