commit 00f50e1422a184f94385c2733f51afd58d1d9b80 Author: 王性驊 Date: Wed Jan 15 19:33:04 2025 +0800 feat: init project diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..817ec0b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.idea/ +go.sum +gen_result/ +etc/reaction.yaml +etc/reaction.dev.yaml +client/ +.DS_Store \ No newline at end of file diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..36f79f3 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,134 @@ +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 + - typecheck + - unused + - asasalint + - asciicheck + - bidichk + - bodyclose + - contextcheck + - wastedassign + - whitespace + - 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 + - dupword + - durationcheck + - errchkjson + - errname + - errorlint + # - execinquery + - exhaustive + - exportloopref + - forbidigo + - forcetypeassert + - gochecknoinits + - gocognit + - goconst + - gocritic + - gocyclo + - goheader + - gomoddirectives + - goprintffuncname + - gosec + - grouper + - importas + - interfacebloat + - 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/logic + + 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 \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..99bf12d --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ +# go-zero 生成風格 +GO_ZERO_STYLE=go_zero +GO ?= go +GOFMT ?= gofmt "-s" +GOFILES := $(shell find . -name "*.go") +LDFLAGS := -s -w +VERSION="v1.0.4" +DOCKER_REPO="igs170911/reaction" +GIT_COMMIT ?= $(shell git rev-parse --short HEAD) + +.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 + goctl rpc protoc ./generate/protobuf/reaction.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: 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 VERSION=$(VERSION) \ + --build-arg GIT_COMMIT=$(GIT_COMMIT) \ + --secret id=ssh_key,src=./build/id_ed25519 \ + --progress=plain . + rm -rf Dockerfile + @echo "Generate core-api files successfully" + + +.PHONY: mock-gen +mock-gen: # 建立 mock 資料 + mockgen -source=./pkg/domain/repository/order.go -destination=./pkg/mock/repository/order.go -package=mock + + @echo "Generate mock files successfully" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..309eebd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,71 @@ +version: "3.9" + +services: + app: + image: igs170911/reaction:v1.0.4 + container_name: app-service + ports: + - "8080:8080" # 替換為您的應用服務的公開端口 + depends_on: + - mongo + - etcd + - redis + environment: + MONGO_URI: mongodb://mongo:27017/appdb + ETCD_ENDPOINT: http://etcd:2379 + REDIS_HOST: redis + REDIS_PORT: 6379 + networks: + - app-network + + mongo: + image: mongo:8.0 + container_name: mongo + restart: always + ports: + - "27017:27017" + environment: + MONGO_INITDB_ROOT_USERNAME: root + MONGO_INITDB_ROOT_PASSWORD: example + networks: + - app-network + volumes: + - mongo-data:/data/db + + etcd: + image: quay.io/coreos/etcd:v3.5.5 + container_name: etcd + restart: always + command: > + /usr/local/bin/etcd + --data-dir=/etcd-data + --name=etcd + --listen-client-urls=http://0.0.0.0:2379 + --advertise-client-urls=http://etcd:2379 + ports: + - "2379:2379" + - "2380:2380" + networks: + - app-network + volumes: + - etcd-data:/etcd-data + + redis: + image: redis:7.0 + container_name: redis + restart: always + ports: + - "6379:6379" + networks: + - app-network + volumes: + - redis-data:/data + +networks: + app-network: + driver: bridge + +volumes: + mongo-data: + etcd-data: + redis-data: \ No newline at end of file diff --git a/generate/protobuf/reaction.proto b/generate/protobuf/reaction.proto new file mode 100644 index 0000000..c424085 --- /dev/null +++ b/generate/protobuf/reaction.proto @@ -0,0 +1,19 @@ +syntax = "proto3"; + +package reaction; +option go_package="./reaction"; + +// OKResp +message OKResp {} +// NoneReq +message NoneReq {} + +message Pager { + int64 total =1; + int64 size=2; + int64 index=3; +} + +service Reaction { + rpc Empty(NoneReq) returns(OKResp); +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4b7cc12 --- /dev/null +++ b/go.mod @@ -0,0 +1,89 @@ +module code.30cm.net/digimon/app-cloudep-reaction-service + +go 1.23.4 + +require ( + github.com/zeromicro/go-zero v1.7.6 + google.golang.org/grpc v1.69.4 + google.golang.org/protobuf v1.36.2 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cenkalti/backoff/v4 v4.3.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/coreos/go-semver v0.3.1 // indirect + github.com/coreos/go-systemd/v22 v22.5.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/emicklei/go-restful/v3 v3.11.0 // indirect + github.com/fatih/color v1.18.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-openapi/jsonpointer v0.19.6 // indirect + github.com/go-openapi/jsonreference v0.20.2 // indirect + github.com/go-openapi/swag v0.22.4 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/mock v1.6.0 // indirect + github.com/golang/protobuf v1.5.4 // indirect + github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.6.0 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/compress v1.17.9 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/openzipkin/zipkin-go v0.4.3 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/prometheus/client_golang v1.20.5 // indirect + github.com/prometheus/client_model v0.6.1 // indirect + github.com/prometheus/common v0.55.0 // indirect + github.com/prometheus/procfs v0.15.1 // indirect + github.com/redis/go-redis/v9 v9.7.0 // indirect + github.com/spaolacci/murmur3 v1.1.0 // indirect + go.etcd.io/etcd/api/v3 v3.5.15 // indirect + go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect + go.etcd.io/etcd/client/v3 v3.5.15 // indirect + go.opentelemetry.io/otel v1.31.0 // indirect + go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/zipkin v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.31.0 // indirect + go.opentelemetry.io/otel/sdk v1.31.0 // indirect + go.opentelemetry.io/otel/trace v1.31.0 // indirect + go.opentelemetry.io/proto/otlp v1.3.1 // indirect + go.uber.org/atomic v1.10.0 // indirect + go.uber.org/automaxprocs v1.6.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + go.uber.org/zap v1.24.0 // indirect + golang.org/x/net v0.33.0 // indirect + golang.org/x/oauth2 v0.23.0 // indirect + golang.org/x/sys v0.28.0 // indirect + golang.org/x/term v0.27.0 // indirect + golang.org/x/text v0.21.0 // indirect + golang.org/x/time v0.8.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20241015192408-796eee8c2d53 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + k8s.io/api v0.29.3 // indirect + k8s.io/apimachinery v0.29.4 // indirect + k8s.io/client-go v0.29.3 // indirect + k8s.io/klog/v2 v2.110.1 // indirect + k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect + k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect + sigs.k8s.io/yaml v1.3.0 // indirect +) diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100755 index 0000000..c1f85b9 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,7 @@ +package config + +import "github.com/zeromicro/go-zero/zrpc" + +type Config struct { + zrpc.RpcServerConf +} diff --git a/internal/logic/reaction/empty_logic.go b/internal/logic/reaction/empty_logic.go new file mode 100644 index 0000000..93d6aa2 --- /dev/null +++ b/internal/logic/reaction/empty_logic.go @@ -0,0 +1,30 @@ +package reactionlogic + +import ( + "context" + + "code.30cm.net/digimon/app-cloudep-reaction-service/gen_result/pb/reaction" + "code.30cm.net/digimon/app-cloudep-reaction-service/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type EmptyLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewEmptyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EmptyLogic { + return &EmptyLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +func (l *EmptyLogic) Empty(in *reaction.NoneReq) (*reaction.OKResp, error) { + // todo: add your logic here and delete this line + + return &reaction.OKResp{}, nil +} diff --git a/internal/server/reaction/reaction_server.go b/internal/server/reaction/reaction_server.go new file mode 100644 index 0000000..b44cc72 --- /dev/null +++ b/internal/server/reaction/reaction_server.go @@ -0,0 +1,29 @@ +// Code generated by goctl. DO NOT EDIT. +// goctl 1.7.3 +// Source: reaction.proto + +package server + +import ( + "context" + + "code.30cm.net/digimon/app-cloudep-reaction-service/gen_result/pb/reaction" + reactionlogic "code.30cm.net/digimon/app-cloudep-reaction-service/internal/logic/reaction" + "code.30cm.net/digimon/app-cloudep-reaction-service/internal/svc" +) + +type ReactionServer struct { + svcCtx *svc.ServiceContext + reaction.UnimplementedReactionServer +} + +func NewReactionServer(svcCtx *svc.ServiceContext) *ReactionServer { + return &ReactionServer{ + svcCtx: svcCtx, + } +} + +func (s *ReactionServer) Empty(ctx context.Context, in *reaction.NoneReq) (*reaction.OKResp, error) { + l := reactionlogic.NewEmptyLogic(ctx, s.svcCtx) + return l.Empty(in) +} diff --git a/internal/svc/service_context.go b/internal/svc/service_context.go new file mode 100644 index 0000000..4ac8507 --- /dev/null +++ b/internal/svc/service_context.go @@ -0,0 +1,13 @@ +package svc + +import "code.30cm.net/digimon/app-cloudep-reaction-service/internal/config" + +type ServiceContext struct { + Config config.Config +} + +func NewServiceContext(c config.Config) *ServiceContext { + return &ServiceContext{ + Config: c, + } +} diff --git a/reaction.go b/reaction.go new file mode 100644 index 0000000..0c4cccc --- /dev/null +++ b/reaction.go @@ -0,0 +1,40 @@ +package main + +import ( + "flag" + + "github.com/zeromicro/go-zero/core/logx" + + "code.30cm.net/digimon/app-cloudep-reaction-service/gen_result/pb/reaction" + "code.30cm.net/digimon/app-cloudep-reaction-service/internal/config" + reactionServer "code.30cm.net/digimon/app-cloudep-reaction-service/internal/server/reaction" + "code.30cm.net/digimon/app-cloudep-reaction-service/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/core/service" + "github.com/zeromicro/go-zero/zrpc" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" +) + +var configFile = flag.String("f", "etc/reaction.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + ctx := svc.NewServiceContext(c) + + s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { + reaction.RegisterReactionServer(grpcServer, reactionServer.NewReactionServer(ctx)) + + if c.Mode == service.DevMode || c.Mode == service.TestMode { + reflection.Register(grpcServer) + } + }) + defer s.Stop() + + logx.Infof("Starting rpc server at %s...\n", c.ListenOn) + s.Start() +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..640c19a --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +# 按讚或不讚(互動系統) \ No newline at end of file