Skip to content

Commit 32abb2a

Browse files
committed
add docker-compose.yml
1 parent f7b222b commit 32abb2a

File tree

5 files changed

+117
-58
lines changed

5 files changed

+117
-58
lines changed

Dockerfile

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
FROM golang:alpine AS builder
1+
FROM golang AS builder
22

3-
# Add source code
4-
ADD ./ /go/src/github.com/dhax/go-base/
3+
WORKDIR /src
4+
# Download dependencies
5+
COPY go.mod go.sum /
6+
RUN go mod download
57

6-
RUN cd /go/src/github.com/dhax/go-base && \
7-
go build && \
8-
mv ./go-base /usr/bin/go-base
8+
# Add source code
9+
COPY . .
10+
RUN CGO_ENABLED=0 go build -o main .
911

1012
# Multi-Stage production build
11-
FROM alpine
12-
13-
RUN apk add --update ca-certificates
13+
FROM alpine AS production
14+
RUN apk --no-cache add ca-certificates
1415

16+
WORKDIR /app
1517
# Retrieve the binary from the previous stage
16-
COPY --from=builder /usr/bin/go-base /usr/local/bin/go-base
17-
18+
COPY --from=builder /src/main .
19+
# Copy static template files
20+
COPY templates templates
21+
# Copy frontend
22+
COPY public public
23+
# Expose port
24+
EXPOSE 3000
1825
# Set the binary as the entrypoint of the container
19-
CMD ["go-base", "serve"]
26+
CMD ["./main", "serve"]

README.md

+54-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Go Restful API Boilerplate
22

3-
[![GoDoc Badge]][GoDoc] [![GoReportCard Badge]][GoReportCard]
3+
[![GoDoc Badge]][godoc] [![GoReportCard Badge]][goreportcard]
44

55
Easily extendible RESTful API boilerplate aiming to follow idiomatic go and best practice.
66

@@ -9,89 +9,99 @@ The goal of this boiler is to have a solid and structured foundation to build up
99
Any feedback and pull requests are welcome and highly appreciated. Feel free to open issues just for comments and discussions.
1010

1111
## Features
12+
1213
The following feature set is a minimal selection of typical Web API requirements:
1314

1415
- Configuration using [viper](https://github.com/spf13/viper)
1516
- CLI features using [cobra](https://github.com/spf13/cobra)
1617
- PostgreSQL support including migrations using [go-pg](https://github.com/go-pg/pg)
1718
- Structured logging with [Logrus](https://github.com/sirupsen/logrus)
1819
- Routing with [chi router](https://github.com/go-chi/chi) and middleware
19-
- JWT Authentication using [jwt-go](https://github.com/dgrijalva/jwt-go) with example passwordless email authentication
20+
- JWT Authentication using [golang-jwt](https://github.com/golang-jwt/jwt/v4) with example passwordless email authentication
2021
- Request data validation using [ozzo-validation](https://github.com/go-ozzo/ozzo-validation)
2122
- HTML emails with [gomail](https://github.com/go-gomail/gomail)
2223

2324
## Start Application
25+
2426
- Clone this repository
2527
- Create a postgres database and set environment variables for your database accordingly if not using same as default
26-
- Run the application to see available commands: ```go run main.go```
27-
- First initialize the database running all migrations found in ./database/migrate at once with command *migrate*: ```go run main.go migrate```
28-
- Run the application with command *serve*: ```go run main.go serve```
28+
- Run the application to see available commands: `go run main.go`
29+
- First initialize the database running all migrations found in ./database/migrate at once with command _migrate_: `go run main.go migrate`
30+
- Run the application with command _serve_: `go run main.go serve`
31+
32+
Or just use the provided docker-compose file. After first start attach to the server container and run `./main migrate` to populate the database.
2933

3034
## API Routes
3135

3236
### Authentication
37+
3338
For passwordless login following routes are available:
3439

35-
Path | Method | Required JSON | Header | Description
36-
---|---|---|---|---
37-
/auth/login | POST | email | | the email you want to login with (see below)
38-
/auth/token | POST | token | | the token you received via email (or printed to stdout if smtp not set)
39-
/auth/refresh | POST | | Authorization: "Bearer refresh_token" | refresh JWTs
40-
/auth/logout | POST | | Authorizaiton: "Bearer refresh_token" | logout from this device
40+
| Path | Method | Required JSON | Header | Description |
41+
| ------------- | ------ | ------------- | ------------------------------------- | ----------------------------------------------------------------------- |
42+
| /auth/login | POST | email | | the email you want to login with (see below) |
43+
| /auth/token | POST | token | | the token you received via email (or printed to stdout if smtp not set) |
44+
| /auth/refresh | POST | | Authorization: "Bearer refresh_token" | refresh JWTs |
45+
| /auth/logout | POST | | Authorizaiton: "Bearer refresh_token" | logout from this device |
4146

4247
### Example API
43-
Besides /auth/* the API provides two main routes /api/* and /admin/*, as an example to separate application and administration context. The latter requires to be logged in as administrator by providing the respective JWT in Authorization Header.
4448

45-
Check [routes.md](routes.md) for a generated overview of the provided API routes.
49+
Besides /auth/_ the API provides two main routes /api/_ and /admin/\*, as an example to separate application and administration context. The latter requires to be logged in as administrator by providing the respective JWT in Authorization Header.
4650

51+
Check [routes.md](routes.md) for a generated overview of the provided API routes.
4752

4853
### Client API Access and CORS
49-
The server is configured to serve a Progressive Web App (PWA) client from *./public* folder (this repo only serves an example index.html, see below for a demo PWA client to put here). In this case enabling CORS is not required, because the client is served from the same host as the api.
5054

51-
If you want to access the api from a client that is serverd from a different host, including e.g. a development live reloading server with below demo client, you must enable CORS on the server first by setting environment variable *ENABLE_CORS=true* on the server to allow api connections from clients serverd by other hosts.
55+
The server is configured to serve a Progressive Web App (PWA) client from _./public_ folder (this repo only serves an example index.html, see below for a demo PWA client to put here). In this case enabling CORS is not required, because the client is served from the same host as the api.
56+
57+
If you want to access the api from a client that is serverd from a different host, including e.g. a development live reloading server with below demo client, you must enable CORS on the server first by setting environment variable _ENABLE_CORS=true_ on the server to allow api connections from clients serverd by other hosts.
5258

5359
#### Demo client application
54-
For demonstration of the login and account management features this API serves a demo [Vue.js](https://vuejs.org) PWA. The client's source code can be found [here](https://github.com/dhax/go-base-vue). Build and put it into the api's *./public* folder, or use the live development server (requires CORS enabled).
5560

56-
Outgoing emails containing the login token will be print to stdout if no valid email smtp settings are provided by environment variables (see table below). If *EMAIL_SMTP_HOST* is set but the host can not be reached the application will exit immediately at start.
61+
For demonstration of the login and account management features this API serves a demo [Vue.js](https://vuejs.org) PWA. The client's source code can be found [here](https://github.com/dhax/go-base-vue). Build and put it into the api's _./public_ folder, or use the live development server (requires CORS enabled).
62+
63+
Outgoing emails containing the login token will be print to stdout if no valid email smtp settings are provided by environment variables (see table below). If _EMAIL_SMTP_HOST_ is set but the host can not be reached the application will exit immediately at start.
5764

5865
Use one of the following bootstrapped users for login:
66+
5967
- [email protected] (has access to admin panel)
6068
6169

6270
A deployed version can also be found on [Heroku](https://govue.herokuapp.com)
6371

6472
### Environment Variables
73+
6574
By default viper will look at $HOME/.go-base.yaml for a config file. Setting your config as Environment Variables is recommended as by 12-Factor App.
6675

67-
Name | Type | Default | Description
68-
---|---|---|---
69-
PORT | string | localhost:3000 | http address (accepts also port number only for heroku compability)
70-
LOG_LEVEL | string | debug | log level
71-
LOG_TEXTLOGGING | bool | false | defaults to json logging
72-
DB_NETWORK | string | tcp | database 'tcp' or 'unix' connection
73-
DB_ADDR | string | localhost:5432 | database tcp address or unix socket
74-
DB_USER | string | postgres | database user name
75-
DB_PASSWORD | string | postgres | database user password
76-
DB_DATABASE | string | gobase | database shema name
77-
AUTH_LOGIN_URL | string | http://localhost:3000/login | client login url as sent in login token email
78-
AUTH_LOGIN_TOKEN_LENGTH | int | 8 | length of login token
79-
AUTH_LOGIN_TOKEN_EXPIRY | time.Duration | 11m | login token expiry
80-
AUTH_JWT_SECRET | string | random | jwt sign and verify key - value "random" creates random 32 char secret at startup (and automatically invalidates existing tokens on app restarts, so during dev you might want to set a fixed value here)
81-
AUTH_JWT_EXPIRY | time.Duration | 15m | jwt access token expiry
82-
AUTH_JWT_REFRESH_EXPIRY | time.Duration | 1h | jwt refresh token expiry
83-
EMAIL_SMTP_HOST | string || email smtp host (if set and connection can't be established then app exits)
84-
EMAIL_SMTP_PORT | int || email smtp port
85-
EMAIL_SMTP_USER | string || email smtp username
86-
EMAIL_SMTP_PASSWORD | string || email smtp password
87-
EMAIL_FROM_ADDRESS | string || from address used in sending emails
88-
EMAIL_FROM_NAME | string || from name used in sending emails
89-
ENABLE_CORS | bool | false | enable CORS requests
76+
| Name | Type | Default | Description |
77+
| ----------------------- | ------------- | --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
78+
| PORT | string | localhost:3000 | http address (accepts also port number only for heroku compability) |
79+
| LOG_LEVEL | string | debug | log level |
80+
| LOG_TEXTLOGGING | bool | false | defaults to json logging |
81+
| DB_NETWORK | string | tcp | database 'tcp' or 'unix' connection |
82+
| DB_ADDR | string | localhost:5432 | database tcp address or unix socket |
83+
| DB_USER | string | postgres | database user name |
84+
| DB_PASSWORD | string | postgres | database user password |
85+
| DB_DATABASE | string | postgres | database shema name |
86+
| AUTH_LOGIN_URL | string | http://localhost:3000/login | client login url as sent in login token email |
87+
| AUTH_LOGIN_TOKEN_LENGTH | int | 8 | length of login token |
88+
| AUTH_LOGIN_TOKEN_EXPIRY | time.Duration | 11m | login token expiry |
89+
| AUTH_JWT_SECRET | string | random | jwt sign and verify key - value "random" creates random 32 char secret at startup (and automatically invalidates existing tokens on app restarts, so during dev you might want to set a fixed value here) |
90+
| AUTH_JWT_EXPIRY | time.Duration | 15m | jwt access token expiry |
91+
| AUTH_JWT_REFRESH_EXPIRY | time.Duration | 1h | jwt refresh token expiry |
92+
| EMAIL_SMTP_HOST | string | | email smtp host (if set and connection can't be established then app exits) |
93+
| EMAIL_SMTP_PORT | int | | email smtp port |
94+
| EMAIL_SMTP_USER | string | | email smtp username |
95+
| EMAIL_SMTP_PASSWORD | string | | email smtp password |
96+
| EMAIL_FROM_ADDRESS | string | | from address used in sending emails |
97+
| EMAIL_FROM_NAME | string | | from name used in sending emails |
98+
| ENABLE_CORS | bool | false | enable CORS requests |
9099

91100
### Testing
101+
92102
Package auth/pwdless contains example api tests using a mocked database.
93103

94-
[GoDoc]: https://godoc.org/github.com/dhax/go-base
95-
[GoDoc Badge]: https://godoc.org/github.com/dhax/go-base?status.svg
96-
[GoReportCard]: https://goreportcard.com/report/github.com/dhax/go-base
97-
[GoReportCard Badge]: https://goreportcard.com/badge/github.com/dhax/go-base
104+
[godoc]: https://godoc.org/github.com/dhax/go-base
105+
[godoc badge]: https://godoc.org/github.com/dhax/go-base?status.svg
106+
[goreportcard]: https://goreportcard.com/report/github.com/dhax/go-base
107+
[goreportcard badge]: https://goreportcard.com/badge/github.com/dhax/go-base

cmd/serve.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func init() {
2626
RootCmd.AddCommand(serveCmd)
2727

2828
// Here you will define your flags and configuration settings.
29-
viper.SetDefault("port", "localhost:3000")
29+
viper.SetDefault("port", "3000")
3030
viper.SetDefault("log_level", "debug")
3131

3232
viper.SetDefault("auth_login_url", "http://localhost:3000/login")

database/postgres.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func DBConn() (*pg.DB, error) {
1515
viper.SetDefault("db_addr", "localhost:5432")
1616
viper.SetDefault("db_user", "postgres")
1717
viper.SetDefault("db_password", "postgres")
18-
viper.SetDefault("db_database", "gobase")
18+
viper.SetDefault("db_database", "postgres")
1919

2020
db := pg.Connect(&pg.Options{
2121
Network: viper.GetString("db_network"),

docker-compose.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: "3.8"
2+
3+
volumes:
4+
postgres:
5+
6+
services:
7+
server:
8+
build:
9+
context: .
10+
depends_on:
11+
- postgres
12+
ports:
13+
- 3000:3000
14+
environment:
15+
LOG_LEVEL: debug
16+
LOG_TEXTLOGGING: "true"
17+
#PORT: 3000
18+
DB_ADDR: postgres:5432
19+
#DB_USER: postgres
20+
#DB_PASSWORD: postgres
21+
#DB_DATABASE: postgres
22+
#AUTH_JWT_EXPIRY: 1h
23+
#AUTH_JWT_REFRESH_EXPIRY: 72h
24+
#AUTH_JWT_SECRET: my secret
25+
#SENDGRID_API_KEY: your-sendgrid-api-key
26+
#EMAIL_FROM_ADDRESS: go-base
27+
#EMAIL_FROM_NAME: Go Base
28+
#EMAIL_SMTP_HOST:
29+
#EMAIL_SMTP_PORT: 465
30+
#EMAIL_SMTP_USER:
31+
#EMAIL_SMTP_PASSWORD:
32+
ENABLE_CORS: "true"
33+
34+
postgres:
35+
image: postgres:13
36+
restart: unless-stopped
37+
ports:
38+
- 5432:5432
39+
volumes:
40+
- postgres:/var/lib/postgresql/data
41+
environment:
42+
POSTGRES_PASSWORD: postgres

0 commit comments

Comments
 (0)