Skip to content

Commit

Permalink
refactor: 重构项目结构,分离example到单独mod
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Feb 12, 2025
1 parent a6701eb commit 145eb30
Show file tree
Hide file tree
Showing 177 changed files with 1,170 additions and 635 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ jobs:
go-version: '1.23'

- name: Build
run: |
mkdir -p assets/dash/dashboard/apps/web-ele/dist
touch assets/dash/dashboard/apps/web-ele/dist/index.html
make init
make build
run: cd example && make init &&make build

- name: Test
run: go test -v ./...
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,4 @@ fabric.properties
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,goland,go

/swagger
/build
/var
/api
/config.json
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,goland,go
112 changes: 3 additions & 109 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,111 +1,5 @@
MODULE := $(shell go list -m)
MODULE_NAME := $(lastword $(subst /, ,$(MODULE)))
BUILD := $(shell git rev-parse --short HEAD)@$(shell date +%s)
CURRENT_OS := $(shell uname | tr "[A-Z]" "[a-z]")
CURRENT_ARCH := $(shell uname -m | sed 's/x86_64/amd64/' | sed 's/aarch64/arm64/')

DOCKER_IMAGE ?= ghcr.io/tbxark/$(MODULE_NAME)
DOCKER_FILE := cmd/app/Dockerfile

LD_FLAGS := "-X $(MODULE)/internal/config.BuildVersion=$(BUILD)"
GO_BUILD := CGO_ENABLED=0 go build -trimpath -ldflags $(LD_FLAGS) -tags=jsoniter

.PHONY: init
init: ## Init all dependencies
go mod download
go get entgo.io/ent/cmd/ent@latest
go get github.com/google/wire/cmd/wire@latest
go install github.com/swaggo/swag/cmd/swag@latest
go install github.com/bufbuild/buf/cmd/buf@latest
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install github.com/favadi/protoc-go-inject-tag@latest
$(MAKE) install
$(MAKE) gen-ent
$(MAKE) gen-docs
$(MAKE) gen-wire
buf dep update
go mod tidy

.PHONY: install
install: ## Install all dependencies
# cd contrib/protoc-gen-sphere && go mod download && go install .
# cd contrib/protoc-gen-route && go mod download && go install .
# cd contrib/ent-gen-proto && go mod download && go install .
go install github.com/TBXark/sphere/contrib/protoc-gen-sphere@latest
go install github.com/TBXark/sphere/contrib/protoc-gen-route@latest
go install github.com/TBXark/sphere/contrib/ent-gen-proto@latest

.PHONY: gen-proto
gen-proto: ## Generate proto files and run protoc plugins
ent-gen-proto -path=./internal/pkg/database/schema
buf generate
protoc-go-inject-tag -input="./api/*/*/*.pb.go" -remove_tag_comment

.PHONY: gen-ent
gen-ent: ## Generate ent code
go generate ./internal/pkg/database/generate.go

.PHONY: gen-docs
gen-docs: gen-proto ## Generate swagger docs
go generate docs.go

.PHONY: gen-wire
gen-wire: ## Generate wire code
go generate ./cmd/...

.PHONY: gen-conf
gen-conf: ## Generate example config
go run ./cmd/cli/config gen

.PHONY: generate
generate: ## Run all generate command
go generate ./...

.PHONY: dash
dash: ## Build dash
sh ./assets/dash/build.sh

.PHONY: build
build: ## Build binary
$(GO_BUILD) -o ./build/$(CURRENT_OS)_$(CURRENT_ARCH)/ ./...

.PHONY: build-linux-amd64
build-linux-amd64: ## Build linux amd64 binary
GOOS=linux GOARCH=amd64 $(GO_BUILD) -o ./build/linux_amd64/ ./...

.PHONY: build-linux-arm64
build-linux-arm64: ## Build linux arm64 binary
GOOS=linux GOARCH=arm64 $(GO_BUILD) -o ./build/linux_arm64/ ./...

.PHONY: build-all
build-all: build-linux-amd64 build-linux-arm64 ## Build all arch binary

.PHONY: build-docker
build-docker: ## Build docker image
docker buildx build --platform=linux/amd64,linux/arm64 -t $(DOCKER_IMAGE) . -f $(DOCKER_FILE) --push --provenance=false

.PHONY: delpoy
deploy: ## Deploy binary
ansible-playbook -i devops/hosts/inventory.ini devops/delpoy-binary.yaml

.PHONY: lint
lint: ## Run linter
golangci-lint run
buf lint

.PHONY: fmt
fmt: ## Run formatter
go fmt ./...
buf format

.PHONY: clean
clean: ## Clean build files
rm -rf ./build
rm -rf ./swagger
rm -rf ./api
rm -rf ./internal/pkg/database/ent

.PHONY: help
help: ## Show this help message
@echo "\n\033[1mSphere build tool.\033[0m Usage: make [target]\n"
@grep -h "##" $(MAKEFILE_LIST) | grep -v grep | sed -e 's/\(.*\):.*##\(.*\)/\1:\2/' | column -t -s ':' | sed -e 's/^/ /'
cd contrib/protoc-gen-sphere && go mod download && go install .
cd contrib/protoc-gen-route && go mod download && go install .
cd contrib/ent-gen-proto && go mod download && go install .
1 change: 0 additions & 1 deletion assets/dash/.gitignore

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions contrib/protoc-gen-route/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ And then run the command, add following to your `buf.gen.yaml` file
- paths=source_relative
- options_key=bot # The key of the options
- gen_file_suffix=_bot.pb.go # The suffix of the generated file
- request_model=github.com/TBXark/sphere/pkg/telegram;Update
- response_model=github.com/TBXark/sphere/pkg/telegram;Message
- extra_data_model=github.com/TBXark/sphere/pkg/telegram;MethodExtraData
- extra_data_constructor=github.com/TBXark/sphere/pkg/telegram;NewMethodExtraData
- request_model=github.com/TBXark/sphere/telegram;Update
- response_model=github.com/TBXark/sphere/telegram;Message
- extra_data_model=github.com/TBXark/sphere/telegram;MethodExtraData
- extra_data_constructor=github.com/TBXark/sphere/telegram;NewMethodExtraData
```
12 changes: 6 additions & 6 deletions contrib/protoc-gen-sphere/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ var (

routerType = flag.String("router_type", "github.com/gin-gonic/gin;IRouter", "router type")
contextType = flag.String("context_type", "github.com/gin-gonic/gin;Context", "context type")
dataRespType = flag.String("data_resp_type", "github.com/TBXark/sphere/pkg/server/ginx;DataResponse", "data response type, must support generic")
errorRespType = flag.String("error_resp_type", "github.com/TBXark/sphere/pkg/server/ginx;ErrorResponse", "error response type")
dataRespType = flag.String("data_resp_type", "github.com/TBXark/sphere/server/ginx;DataResponse", "data response type, must support generic")
errorRespType = flag.String("error_resp_type", "github.com/TBXark/sphere/server/ginx;ErrorResponse", "error response type")

serverHandlerFunc = flag.String("server_handler_func", "github.com/TBXark/sphere/pkg/server/ginx;WithJson", "server handler func")
parseJsonFunc = flag.String("parse_json_func", "github.com/TBXark/sphere/pkg/server/ginx;ShouldBindJSON", "parse json func")
parseUriFunc = flag.String("parse_uri_func", "github.com/TBXark/sphere/pkg/server/ginx;ShouldBindUri", "parse uri func")
parseFormFunc = flag.String("parse_form_func", "github.com/TBXark/sphere/pkg/server/ginx;ShouldBindQuery", "parse form func")
serverHandlerFunc = flag.String("server_handler_func", "github.com/TBXark/sphere/server/ginx;WithJson", "server handler func")
parseJsonFunc = flag.String("parse_json_func", "github.com/TBXark/sphere/server/ginx;ShouldBindJSON", "parse json func")
parseUriFunc = flag.String("parse_uri_func", "github.com/TBXark/sphere/server/ginx;ShouldBindUri", "parse uri func")
parseFormFunc = flag.String("parse_form_func", "github.com/TBXark/sphere/server/ginx;ShouldBindQuery", "parse form func")
)

func main() {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions devops/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
api:
build:
context: ..
dockerfile: cmd/api/Dockerfile
dockerfile: ../example/cmd/api/Dockerfile
hostname: api
networks:
- app-network
Expand All @@ -18,7 +18,7 @@ services:
dash:
build:
context: ..
dockerfile: cmd/dash/Dockerfile
dockerfile: ../example/cmd/dash/Dockerfile
hostname: dash
networks:
- app-network
Expand Down
2 changes: 1 addition & 1 deletion .dockerignore → example/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea
../.idea
.vscode
build
temp
Expand Down
165 changes: 165 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode,goland,go
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode,goland,go

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

### GoLand ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### GoLand Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
.idea/**/sonarlint/

# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
.idea/**/sonarIssues.xml

# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator-enh.xml
.idea/**/markdown-navigator/

# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
.idea/$CACHE_FILE$

# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
.idea/codestream.xml

# Azure Toolkit for IntelliJ plugin
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
.idea/**/azureSettings.xml

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,goland,go

/swagger
/build
/var
/api
/config.json
File renamed without changes.
Loading

0 comments on commit 145eb30

Please sign in to comment.