forked from DataDavD/greenlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
104 lines (87 loc) · 3.42 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Include env variables
include .envrc
# ==================================================================================== #
# HELPERS
# ==================================================================================== #
## help: print this help message
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^//'
.PHONY: confirm
confirm:
@echo 'Are you sure? [y/N]' && read ans && [ $${ans:-N} = y ]
# ==================================================================================== #
# DEVELOPMENT
# ==================================================================================== #
## run/api: run the cmd/api application
.PHONY: run/api
run/api:
@go run ./cmd/api -db-dsn=${GREENLIGHT_DB_DSN}
## db/psql: connect to the database using psql
.PHONY: db/sql
db/psql:
psql ${GREENLIGHT_DB_DSN}
## db/migrations/new name=$1: create a new database migration
.PHONY: db/migrations/new
db/migrations/new:
@echo 'Creating migration files for ${name}'
migrate create -seq -ext=.sql -dir=./migrations ${name}
## db/migrations/up: apply all up database migrations
.PHONY: db/migrations/up
db/migrations/up: confirm
@echo 'Running up migrations...'
migrate -path="./migrations" -database "postgres://greenlight:${DB_PW}@localhost/greenlight?sslmode=disable" up
# ==================================================================================== #
# QUALITY CONTROL
# ==================================================================================== #
## audit: tidy dependencies and format, vet, and test all code
.PHONY: audit
audit: vendor
@echo 'Formatting code...'
go fmt ./...
@echo 'Vetting code...'
go vet ./...
staticcheck ./...
@echo 'Running tests...'
go test -race -vet=off ./...
## vendor: tidy and vendor dependencies
.PHONY: vendor
vendor:
@echo 'Tidying and verifying module dependencies'
go mod tidy
go mod verify
@echo 'Vendoring dependencies'
go mod vendor
# ==================================================================================== #
# BUILD
# ==================================================================================== #
## build/api: build the cmd/api application
.PHONY: build/api
build/api:
@echo 'Building cmd/api...'
go build -ldflags '-s -w' -o ./bin/api ./cmd/api
GOOS=linux GOARCH=amd64 go build -ldflags='-s -w' -o=./bin/linux_amd64/api ./cmd/api
# ==================================================================================== #
# PRODUCTION
# ==================================================================================== #
production_host_ip = '144.126.210.226'
## production/connect: connect to the production server
.PHONY: production/connect
production/connect:
ssh greenlight@${production_host_ip}
## production/deploy/api: deploy the api to production
.PHONY: production/deploy/api
production/deploy/api:
rsync -P ./bin/linux_amd64/api greenlight@${production_host_ip}:~
rsync -rP --delete ./migrations greenlight@${production_host_ip}:~
rsync -P ./remote/production/api.service greenlight@${production_host_ip}:~
rsync -P ./remote/production/Caddyfile greenlight@${production_host_ip}:~
ssh -t greenlight@${production_host_ip} '\
migrate -path ~/migrations -database $$GREENLIGHT_DB_DSN up \
&& sudo mv ~/api.service /etc/systemd/system/ \
&& sudo systemctl enable api \
&& sudo systemctl restart api \
&& sudo mv ~/Caddyfile /etc/caddy/ \
&& sudo systemctl reload caddy \
'