-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
130 lines (118 loc) · 4.92 KB
/
Copy pathMakefile
File metadata and controls
130 lines (118 loc) · 4.92 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
all: build test
setup:
@echo "Setting up development environment..."
@if [ ! -f .env ]; then \
echo "Creating .env from example.env..."; \
cp example.env .env; \
fi
@if ! grep -q "^DB_ENCRYPTION_KEY=[A-Fa-f0-9]\{64,\}" .env 2>/dev/null; then \
echo "Generating DB_ENCRYPTION_KEY..."; \
DB_ENCRYPTION_KEY=$$(openssl rand -hex 32); \
sed -i.bak "s|^DB_ENCRYPTION_KEY=.*|DB_ENCRYPTION_KEY=$$DB_ENCRYPTION_KEY|" .env && rm -f .env.bak; \
else \
echo "DB_ENCRYPTION_KEY already set"; \
fi
@echo "Setup complete! Run 'make migrate-up && make run' to start."
build: docs
@echo "Building binaries..."
@if [ "$$(grep -E '^DISABLE_DB_ENCRYPTION=(false|False|FALSE)' .env 2>/dev/null)" ]; then \
echo " - With SQLCipher encryption"; \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -tags sqlcipher -o bin/api cmd/api/main.go; \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -tags sqlcipher -o bin/migrate cmd/migrate/main.go; \
else \
echo " - With standard SQLite (unencrypted)"; \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o bin/api cmd/api/main.go; \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o bin/migrate cmd/migrate/main.go; \
fi
run:
@if [ "$$(grep -E '^DISABLE_DB_ENCRYPTION=(false|False|FALSE)' .env 2>/dev/null)" ]; then \
go run -tags sqlcipher cmd/api/main.go; \
else \
go run cmd/api/main.go; \
fi
test:
@echo "Testing..."
@go test ./... -v
itest:
@echo "Running integration tests..."
migrate-up:
@echo "Running migrations..."
@if [ "$$(grep -E '^DISABLE_DB_ENCRYPTION=(false|False|FALSE)' .env 2>/dev/null)" ]; then \
go run -tags sqlcipher cmd/migrate/main.go -action=up; \
else \
go run cmd/migrate/main.go -action=up; \
fi
migrate-down:
@echo "Rolling back last migration..."
@if [ "$$(grep -E '^DISABLE_DB_ENCRYPTION=(false|False|FALSE)' .env 2>/dev/null)" ]; then \
go run -tags sqlcipher cmd/migrate/main.go -action=down -steps=1; \
else \
go run cmd/migrate/main.go -action=down -steps=1; \
fi
migrate-status:
@echo "Checking migration status..."
@if [ "$$(grep -E '^DISABLE_DB_ENCRYPTION=(false|False|FALSE)' .env 2>/dev/null)" ]; then \
go run -tags sqlcipher cmd/migrate/main.go -action=status; \
else \
go run cmd/migrate/main.go -action=status; \
fi
docs:
@echo "Generating Swagger documentation..."
@which swag > /dev/null || (echo "Error: swag is not installed." && echo "Install it with: go install github.com/swaggo/swag/cmd/swag@latest" && exit 1)
@swag fmt
@swag init -g cmd/api/main.go -o docs
clean:
@echo "Cleaning..."
@rm -rf bin
setup-systemd:
@echo "Setting up systemd service..."
@if [ "$$(id -u)" -ne 0 ]; then \
echo "Error: This target must be run as root (use sudo make setup-systemd)"; \
exit 1; \
fi
@if ! id authy-api >/dev/null 2>&1; then \
echo "Creating authy-api user..."; \
useradd --system --no-create-home --shell /usr/sbin/nologin authy-api; \
else \
echo "User authy-api already exists"; \
fi
@echo "Creating application directory..."
@mkdir -p /opt/authy-api
@if [ ! -f /opt/authy-api/.env ]; then \
echo "Creating .env file from example.env..."; \
cp example.env /opt/authy-api/.env; \
echo "Setting production values..."; \
sed -i "s|^APP_MODE=.*|APP_MODE=production|" /opt/authy-api/.env; \
sed -i "s|^ALLOW_INSECURE_SERVER=.*|ALLOW_INSECURE_SERVER=true|" /opt/authy-api/.env; \
sed -i "s|^ALLOW_INSECURE_EXTERNAL=.*|ALLOW_INSECURE_EXTERNAL=true|" /opt/authy-api/.env; \
sed -i "s|^SQLITE_DB_PATH=.*|SQLITE_DB_PATH=/opt/authy-api/data/authy.db|" /opt/authy-api/.env; \
sed -i "s|^DISABLE_DB_ENCRYPTION=.*|DISABLE_DB_ENCRYPTION=false|" /opt/authy-api/.env; \
sed -i "s|^AUTO_MIGRATE=.*|AUTO_MIGRATE=false|" /opt/authy-api/.env; \
echo "Generating DB_ENCRYPTION_KEY..."; \
DB_ENCRYPTION_KEY=$$(openssl rand -hex 32); \
sed -i "s|^DB_ENCRYPTION_KEY=.*|DB_ENCRYPTION_KEY=$$DB_ENCRYPTION_KEY|" /opt/authy-api/.env; \
echo "WARNING: Review and update /opt/authy-api/.env with appropriate service URLs and credentials"; \
else \
echo ".env file already exists at /opt/authy-api/.env"; \
fi
@echo "Copying default.env..."
@cp default.env /opt/authy-api/default.env
@echo "Creating data directory..."
@mkdir -p /opt/authy-api/data
@echo "Creating cache directories..."
@mkdir -p /opt/authy-api/.cache/go-build /opt/authy-api/.cache/go-mod
@echo "Setting permissions..."
@chown -R authy-api:authy-api /opt/authy-api
@chmod 600 /opt/authy-api/.env
@if [ -f authy-api.service ]; then \
echo "Installing systemd service file..."; \
cp authy-api.service /etc/systemd/system/; \
systemctl daemon-reload; \
echo "Enabling service..."; \
systemctl enable authy-api; \
echo "Setup complete! Use 'systemctl start authy-api' to start the service."; \
else \
echo "WARNING: authy-api.service file not found. Skipping systemd service installation."; \
echo "Setup complete! Manually configure systemd service if needed."; \
fi
.PHONY: all setup build run test clean itest migrate-up migrate-down migrate-status docs setup-systemd