From 942ffab12618635f4e8a0033287f478909463101 Mon Sep 17 00:00:00 2001 From: "daniel.w" Date: Sat, 24 Aug 2024 15:04:30 +0800 Subject: [PATCH] add base filt --- .gitignore | 6 + .golangci.yaml | 140 ++++++++++++ Makefile | 81 +++++++ build/Dockerfile | 47 ++++ chart/readme.md | 1 + .../20240824000000_create_schema.down.sql | 1 + .../20240824000000_create_schema.up.sql | 0 generate/database/readme.md | 39 ++++ generate/protobuf/service.proto | 210 ++++++++++++++++++ 9 files changed, 525 insertions(+) create mode 100644 .gitignore create mode 100644 .golangci.yaml create mode 100644 Makefile create mode 100644 build/Dockerfile create mode 100644 chart/readme.md create mode 100644 generate/database/mysql/create/20240824000000_create_schema.down.sql create mode 100644 generate/database/mysql/create/20240824000000_create_schema.up.sql create mode 100644 generate/database/readme.md create mode 100644 generate/protobuf/service.proto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a56a16 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea/ +go.sum +account/ +gen_result/ +etc/service.yaml +client/ \ No newline at end of file diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..5518484 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,140 @@ +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 + +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..bb182f6 --- /dev/null +++ b/Makefile @@ -0,0 +1,81 @@ +# go-zero 生成風格 +GO_ZERO_STYLE=go_zero + +GO ?= go +GOFMT ?= gofmt "-s" +GOFILES := $(shell find . -name "*.go") +LDFLAGS := -s -w +VERSION="v1.0.3" +DOCKER_REPO="igs170911/notification" + +.PHONY: test +test: # 進行測試 + go test -v --cover ./... + +.PHONY: fmt +fmt: # 格式優化 + $(GOFMT) -w $(GOFILES) + goimports -w ./ + +.PHONY: gen-rpc +gen-rpc: # 建立 rpc code + goctl rpc protoc ./generate/protobuf/service.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 service.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" + +.PHONY: gen-my-sql-model +gen-my-sql-model: # 建立 rpc 資料庫 + goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020011_account_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i '' + goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020012_account_uid_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i '' + goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020013_user_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i '' + goctl model mysql ddl -c no -s ./generate/database/mysql/20230719061241_machine_node.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i '' + @echo "Generate mysql model files successfully" + +gen-mongo-model: # 建立 rpc 資料庫 + # 只產生 Model 剩下的要自己撰寫,連欄位名稱也是 + goctl model mongo -t AutoId --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) + @echo "Generate mongo model files successfully" + +#goctl model mysql ddl -s ./generate/database/mysql/20240819090248_create_role_permission_table.up.sql --style go_zero -d ./internal/model -i '' (沒cache) +#goctl model mysql ddl -c no -s ./generate/database/mysql/20240816014305_create_permission_table.up.sql --style go_zero -d ./internal/model -i '' (有cache) + +.PHONY: mock-gen +mock-gen: # 建立 mock 資料 + mockgen -source=./internal/domain/usecase/uid_generate.go -destination=./internal/mock/usecase/uid_generate.go -package=mock + mockgen -source=./internal/model/account_model_gen.go -destination=./internal/mock/model/account_model_gen.go -package=mock + mockgen -source=./internal/model/account_model.go -destination=./internal/mock/model/account_model.go -package=mock + mockgen -source=./internal/model/account_to_uid_model_gen.go -destination=./internal/mock/model/account_to_uid_model_gen.go -package=mock + mockgen -source=./internal/model/account_to_uid_model.go -destination=./internal/mock/model/account_to_uidmodel.go -package=mock + mockgen -source=./internal/model/machine_node_model_gen.go -destination=./internal/mock/model/machine_node_model_gen.go -package=mock + mockgen -source=./internal/model/machine_node_model.go -destination=./internal/mock/model/machine_node_model.go -package=mock + mockgen -source=./internal/model/user_table_model_gen.go -destination=./internal/mock/model/user_table_model_gen.go -package=mock + mockgen -source=./internal/model/user_table_model.go -destination=./internal/mock/model/user_table_model.go -package=mock + mockgen -source=./internal/model/mongo/auto_id_model_gen.go -destination=./internal/mock/model/auto_id_model_gen.go -package=mock + mockgen -source=./internal/model/mongo/auto_id_model.go -destination=./internal/mock/model/auto_id_model.go -package=mock + @echo "Generate mock files successfully" + +.PHONY: migrate-database +migrate-database: + migrate -source file://generate/database/mysql -database 'mysql://root:yytt@tcp(127.0.0.1:3306)/digimon_member' up diff --git a/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000..78cd457 --- /dev/null +++ b/build/Dockerfile @@ -0,0 +1,47 @@ +########### +# BUILDER # +########### + +FROM golang:1.22.3 as builder + +ARG VERSION +ARG BUILT +ARG GIT_COMMIT +ARG SSH_PRV_KEY + +# private go packages +ENV GOPRIVATE=code.30cm.net +ENV FLAG="-s -w -X main.Version=${VERSION} -X main.Built=${BUILT} -X main.GitCommit=${GIT_COMMIT}" +WORKDIR /app +COPY . . + + +RUN apt-get update && \ + apt-get install git + +# Make the root foler for our ssh +RUN mkdir -p /root/.ssh && \ + chmod 0700 /root/.ssh && \ + ssh-keyscan git.30cm.net > /root/.ssh/known_hosts && \ + echo "$SSH_PRV_KEY" > /root/.ssh/id_rsa && \ + chmod 600 /root/.ssh/id_rsa + + + +RUN --mount=type=ssh go mod download + +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ + -ldflags "$FLAG" \ + -o service + +########## +## FINAL # +########## +# +FROM gcr.io/distroless/static-debian11 +WORKDIR /app + +COPY --from=builder /app/service /app/service +COPY --from=builder /app/etc/service.yaml /app/etc/service.yaml +EXPOSE 8080 +CMD ["/app/service"] \ No newline at end of file diff --git a/chart/readme.md b/chart/readme.md new file mode 100644 index 0000000..33c4f67 --- /dev/null +++ b/chart/readme.md @@ -0,0 +1 @@ +// TODO 未來要放 helm 的地方 \ No newline at end of file diff --git a/generate/database/mysql/create/20240824000000_create_schema.down.sql b/generate/database/mysql/create/20240824000000_create_schema.down.sql new file mode 100644 index 0000000..9c7ec94 --- /dev/null +++ b/generate/database/mysql/create/20240824000000_create_schema.down.sql @@ -0,0 +1 @@ +DROP DATABASE IF EXISTS `digimon_permission`; \ No newline at end of file diff --git a/generate/database/mysql/create/20240824000000_create_schema.up.sql b/generate/database/mysql/create/20240824000000_create_schema.up.sql new file mode 100644 index 0000000..e69de29 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/service.proto b/generate/protobuf/service.proto new file mode 100644 index 0000000..94b372b --- /dev/null +++ b/generate/protobuf/service.proto @@ -0,0 +1,210 @@ +syntax = "proto3"; + +package member; +option go_package="./member"; + +// OKResp +message OKResp {} +// NoneReq +message NoneReq {} + +// ================ enum ================ +enum VerifyType { + VERIFY_NONE = 0; // 初始(異常) + VERIFY_EMAIL = 1; + VERIFY_PHONE = 2; + VERIFY_GOOGLE = 3; // google 驗證器 + VERIFY_NOT = 4; // 尚未 +} + +enum AlarmType { + ALARM_NONE = 0; // 初始(異常) + ALARM_NOT = 1; // 未告警 + ALARM_SYSTEM = 2; // 系統告警中 +} + +enum MemberStatus { + STATUS_NONE = 0; // 初始(異常) + STATUS_VERIFY = 1; // 尚未驗證 + STATUS_COMPLETE = 2; // 帳號啟用中 + STATUS_DISABLE = 3; // 帳號停權中 + STATUS_EMAIL = 4; // 信箱以驗證 + STATUS_PHONE = 5; // 手機以驗證 + STATUS_GA = 6; // GA 已綁定 +} + +// ================ enum ================ + + +// ================ common ================ +message Pager { + int64 total =1; + int64 size=2; + int64 index=3; +} +// ================ common ================ + + +// ================ account ================ +message CreateLoginUserReq { + string login_id = 1; + int64 platform = 2; + string token = 3; +} + +message BindingUserReq { + string uid = 1; + string login_id = 2; + int64 type = 3; +} + +message BindingUserResp { + string uid = 1; + string login_id = 2; + int64 type = 3; +} + +message CreateUserInfoReq { + string uid = 1; + VerifyType verify_type = 2; + AlarmType alarm_type = 3; + MemberStatus status = 4; + string language = 5; + string currency = 6; + optional string avatar= 7; + optional string nick_name = 8; +} + +message GetAccountInfoResp { + CreateLoginUserReq data = 1; +} + +// UpdateUserInfoReq 不處理邏輯給不給改,這裡只關新增修改刪除 +message UpdateUserInfoReq { + string uid = 1; + optional string language = 2; + optional string currency = 3; + optional string nick_name = 4; + optional string avatar = 5; + optional VerifyType verify_type = 6; + optional AlarmType alarm_type = 7; + optional MemberStatus status = 8; +} + +message GetUIDByAccountReq { + string account = 1; +} + +message GetUidByAccountResp { + string uid = 1; + string account =2; +} + +message UpdateTokenReq { + string account = 1; + string token = 2; + int64 platform=3; +} + +message GenerateRefreshCodeReq { + string account = 1; + int32 code_type =2; +} + +message VerifyCode { + string verify_code = 1; +} + +message GenerateRefreshCodeResp { + VerifyCode data = 1; +} + +message VerifyRefreshCodeReq { + string account = 1; + int32 code_type =2; + string verify_code = 3; +} + +message UpdateStatusReq { + string uid = 1; + MemberStatus status = 2; +} + +message GetUserInfoReq { + string uid = 1; + optional string nick_name =2; +} + +message UserInfo { + string uid = 1; + VerifyType verify_type = 2; + AlarmType alarm_type = 3; + MemberStatus status = 4; + string language = 5; + string currency = 6; + string avatar = 7; + int64 create_time=8; + int64 update_time=9; + optional string nick_name = 10; +} + +message GetUserInfoResp { + UserInfo data = 1; +} + +message ListUserInfoReq { + optional VerifyType verify_type = 1; + optional AlarmType alarm_type = 2; + optional MemberStatus status = 3; + optional int64 create_start_time = 4; + optional int64 create_end_time = 5; + int64 page_size =6; + int64 page_index=7; +} + +message ListUserInfoResp { + repeated UserInfo data = 1; + Pager page =2; +} + +message VerifyAuthResultReq { + string token = 1; +} + +message VerifyAuthResultResp { + bool status = 1; +} + +message TwitterAccessTokenResp { + string token = 1; +} + +service Account { + // CreateUserAccount 建立帳號與密碼 -> 可登入,但可不可以做其他事情看業務流程,也可以只註冊就好 + rpc CreateUserAccount(CreateLoginUserReq) returns(OKResp); + // GetUserAccountInfo 取得帳號密碼資料 + rpc GetUserAccountInfo(GetUIDByAccountReq) returns(GetAccountInfoResp); + // UpdateUserToken 更新密碼 + rpc UpdateUserToken(UpdateTokenReq) returns(OKResp); + // GetUidByAccount 用帳號換取 UID + rpc GetUidByAccount(GetUIDByAccountReq) returns(GetUidByAccountResp); + // BindAccount 綁定帳號 -> account bind to UID + rpc BindAccount(BindingUserReq) returns(BindingUserResp); + // BindUserInfo 初次,綁定 User Info + rpc BindUserInfo(CreateUserInfoReq) returns(OKResp); + // UpdateUserInfo 更新 User Info + rpc UpdateUserInfo(UpdateUserInfoReq) returns(OKResp); + // UpdateStatus 修改狀態 + rpc UpdateStatus(UpdateStatusReq) returns(OKResp); + // GetUserInfo 取得會員資訊 + rpc GetUserInfo(GetUserInfoReq) returns(GetUserInfoResp); + // ListMember 取得會員列表 + rpc ListMember(ListUserInfoReq) returns(ListUserInfoResp); + // GenerateRefreshCode 這個帳號驗證碼(十分鐘),通用的 + rpc GenerateRefreshCode(GenerateRefreshCodeReq) returns(GenerateRefreshCodeResp); + // VerifyRefreshCode 驗證忘記密碼 token + rpc VerifyRefreshCode(VerifyRefreshCodeReq) returns(OKResp); + // VerifyGoogleAuthResult 驗證 google 登入是否有效 + rpc VerifyGoogleAuthResult(VerifyAuthResultReq)returns(VerifyAuthResultResp); +} +// ================ account ================ \ No newline at end of file