Skip to content

Commit efc12bc

Browse files
committed
initial import
0 parents  commit efc12bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3061
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: qiangxue
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1.
16+
2.
17+
3.
18+
4.
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Environment (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
29+
**Additional context**
30+
Add any other context about the problem here.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: qiangxue
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/workflows/build.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: build
2+
on: [push, pull_request]
3+
jobs:
4+
5+
build:
6+
name: Build
7+
runs-on: ubuntu-latest
8+
9+
services:
10+
postgres:
11+
image: postgres:10.8
12+
env:
13+
POSTGRES_USER: postgres
14+
POSTGRES_PASSWORD: postgres
15+
POSTGRES_DB: go_restful
16+
ports:
17+
- 5432/tcp
18+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
19+
20+
steps:
21+
22+
- name: Set up Go 1.13
23+
uses: actions/setup-go@v1
24+
with:
25+
go-version: 1.13
26+
id: go
27+
28+
- name: Set up path
29+
run: |
30+
echo "::set-env name=GOPATH::$(go env GOPATH)"
31+
echo "::add-path::$(go env GOPATH)/bin"
32+
shell: bash
33+
34+
- name: Check out code into the Go module directory
35+
uses: actions/checkout@v1
36+
37+
- name: Get dependencies
38+
run: |
39+
go mod download
40+
go mod verify
41+
go get golang.org/x/tools/cmd/cover
42+
go get github.com/mattn/goveralls
43+
go get golang.org/x/lint/golint
44+
45+
- name: Run go lint
46+
run: make lint
47+
48+
- name: Build
49+
run: make build
50+
51+
- name: Test
52+
env:
53+
APP_DSN: postgres://127.0.0.1:${{ job.services.postgres.ports[5432] }}/go_restful?sslmode=disable&user=postgres&password=postgres
54+
run: |
55+
make migrate
56+
make test-cover
57+
58+
- name: Upload coverage to Codecov
59+
uses: codecov/codecov-action@v1
60+
with:
61+
token: ${{ secrets.CODECOV_TOKEN }}
62+
file: ./coverage-all.out

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Test coverage output
15+
coverage*.*
16+
17+
# postgres data volume used by postgres server container for testing purpose
18+
testdata/postgres
19+
20+
# server binary
21+
./server
22+
23+
# PID file generated to support live reload
24+
.pid

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019-2020 Qiang Xue
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
MODULE = $(shell go list -m)
2+
VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2> /dev/null || echo "1.0.0")
3+
PACKAGES := $(shell go list ./... | grep -v /vendor/)
4+
LDFLAGS := -ldflags "-X main.Version=${VERSION}"
5+
6+
CONFIG_FILE ?= ./config/local.yml
7+
APP_DSN ?= $(shell sed -n 's/^dsn:[[:space:]]*"\(.*\)"/\1/p' $(CONFIG_FILE))
8+
MIGRATE := docker run -v $(shell pwd)/migrations:/migrations --network host migrate/migrate -path=/migrations/ -database "$(APP_DSN)"
9+
10+
PID_FILE := './.pid'
11+
FSWATCH_FILE := './fswatch.cfg'
12+
13+
.PHONY: default
14+
default: help
15+
16+
# generate help info from comments: thanks to https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
17+
.PHONY: help
18+
help: ## help information about make commands
19+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
20+
21+
.PHONY: test
22+
test: ## run unit tests
23+
@echo "mode: count" > coverage-all.out
24+
@$(foreach pkg,$(PACKAGES), \
25+
go test -p=1 -cover -covermode=count -coverprofile=coverage.out ${pkg}; \
26+
tail -n +2 coverage.out >> coverage-all.out;)
27+
28+
.PHONY: test-cover
29+
test-cover: test ## run unit tests and show test coverage information
30+
go tool cover -html=coverage-all.out
31+
32+
.PHONY: run
33+
run: ## run the API server
34+
go run ${LDFLAGS} cmd/server/main.go & echo $$! > $(PID_FILE)
35+
36+
.PHONY: run-stop
37+
run-stop: ## stop the API server
38+
@pkill -P `cat $(PID_FILE)` || true
39+
40+
.PHONY: run-restart
41+
run-restart: ## restart the API server
42+
@make run-stop
43+
@printf '%*s\n' "80" '' | tr ' ' -
44+
@echo "Source file changed. Restarting server..."
45+
@make run
46+
@printf '%*s\n' "80" '' | tr ' ' -
47+
48+
run-live: run ## run the API server with live reload support (requires fswatch)
49+
@fswatch -x -o --event Created --event Updated --event Renamed -r internal pkg cmd config | xargs -n1 -I {} make run-restart
50+
51+
.PHONY: build
52+
build: ## build the API server binary
53+
CGO_ENABLED=0 go build ${LDFLAGS} -a -o server $(MODULE)/cmd/server
54+
55+
.PHONY: build-docker
56+
build-docker: ## build the API server as a docker image
57+
docker build -f cmd/server/Dockerfile -t server .
58+
59+
.PHONY: clean
60+
clean: ## remove temporary files
61+
rm -rf server coverage.out coverage-all.out
62+
63+
.PHONY: version
64+
version: ## display the version of the API server
65+
@echo $(VERSION)
66+
67+
.PHONY: db-start
68+
db-start: ## start the database server
69+
@mkdir -p testdata/postgres
70+
docker run --rm --name postgres -v $(shell pwd)/testdata:/testdata \
71+
-v $(shell pwd)/testdata/postgres:/var/lib/postgresql/data \
72+
-e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=go_restful -d -p 5432:5432 postgres
73+
74+
.PHONY: db-stop
75+
db-stop: ## stop the database server
76+
docker stop postgres
77+
78+
.PHONY: testdata
79+
testdata: ## populate the database with test data
80+
make migrate-reset
81+
@echo "Populating test data..."
82+
@docker exec -it postgres psql "$(APP_DSN)" -f /testdata/testdata.sql
83+
84+
.PHONY: lint
85+
lint: ## run golint on all Go package
86+
@golint $(PACKAGES)
87+
88+
.PHONY: fmt
89+
fmt: ## run "go fmt" on all Go packages
90+
@go fmt $(PACKAGES)
91+
92+
.PHONY: migrate
93+
migrate: ## run all new database migrations
94+
@echo "Running all new database migrations..."
95+
@$(MIGRATE) up
96+
97+
.PHONY: migrate-down
98+
migrate-down: ## revert database to the last migration step
99+
@echo "Reverting database to the last migration step..."
100+
@$(MIGRATE) down 1
101+
102+
.PHONY: migrate-new
103+
migrate-new: ## create a new database migration
104+
@read -p "Enter the name of the new migration: " name; \
105+
$(MIGRATE) create -ext sql -dir /migrations/ $${name// /_}
106+
107+
.PHONY: migrate-reset
108+
migrate-reset: ## reset database and re-run all migrations
109+
@echo "Resetting database..."
110+
@$(MIGRATE) drop
111+
@echo "Running all database migrations..."
112+
@$(MIGRATE) up

0 commit comments

Comments
 (0)