1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-06-02 23:27:51 +02:00

issue#19 recompile service on file change

Updated web-app and web-api to auto build the service when running with
docker-compose.
This commit is contained in:
Lee Brown 2019-08-12 12:30:35 -08:00
parent e4094d87c8
commit 2e43f95748
8 changed files with 66 additions and 36 deletions

View File

@ -269,38 +269,25 @@ builds locally, update `docker-compose.yaml` to define a volume.
### Re-starting a specific Go service for development
When writing code in an iterative fashion, it is nice to be able to restart a specific service so it will run updated
Go code. This decreases the overhead of stopping all services with `docker-compose down` and then re-starting all the
services again with 'docker-compose up'.
When writing code in an iterative fashion, it is nice to have your change automatically rebuilt. This project uses
[github.com/gravityblast/fresh](https://github.com/gravityblast/fresh) to recompile your services that will include most
changes.
Fresh is a command line tool that builds and (re)starts your web application everytime you save a Go or template file.
The (Fresh configuration file](https://gitlab.com/geeks-accelerator/oss/saas-starter-kit/blob/master/fresh-auto-reload.conf)
is located in the project root. By default the following folders are watched by Fresh:
- handlers
- static
- templates
Any changes to [internal/*](https://gitlab.com/geeks-accelerator/oss/saas-starter-kit/tree/master/internal) or
additional project dependencies added to [go.mod](https://gitlab.com/geeks-accelerator/oss/saas-starter-kit/blob/master/go.mod)
will require the service to be rebuilt.
To restart a specific service, first use `docker ps` to see the list of services running.
```bash
$ docker ps
CONTAINER ID IMAGE COMMAND NAMES
35043164fd0d example-project/web-api:latest "/gosrv" saas-starter-kit_web-api_1
d34c8fc27f3b example-project/web-app:latest "/gosrv" saas-starter-kit_web-app_1
fd844456243e postgres:11-alpine "docker-entrypoint.s…" saas-starter-kit_postgres_1
dda16bfbb8b5 redis:latest "redis-server --appe…" saas-starter-kit_redis_1
```
Then use `docker-compose stop` for a specific service. In the command including the name of service in `docker-compose.yaml` file for the service
to shut down. In the example command, we will shut down the web-api service so we can start it again.
```bash
$ docker-compose stop web-app
```
If you are not in the directory for the service you want to restart then navigate to it. We will go to the directory for the
web-api.
```bash
$ cd cmd/web-api/
```
Then you can start the service again by running main.go
```bash
$ go run main.go
docker-compose up --build -d web-app
```

View File

@ -29,8 +29,9 @@ ENV GO111MODULE="on"
COPY go.mod .
COPY go.sum .
RUN go mod download
RUN go get github.com/pilu/fresh
FROM build_base_golang AS builder
FROM build_base_golang AS dev
ARG service
ARG commit_ref=-
@ -40,20 +41,24 @@ ARG swagInit
COPY internal ./internal
# Copy cmd specific packages.
COPY cmd/${service} ./cmd/web-api
COPY cmd/${service} ./cmd/${service}
COPY cmd/${service}/templates /templates
#COPY cmd/${service}/static /static
# Copy the global templates.
ADD resources/templates/shared /templates/shared
ADD fresh-auto-reload.conf /runner.conf
WORKDIR ./cmd/${service}
ENTRYPOINT ["fresh", "-c", "/runner.conf"]
FROM dev AS builder
# Update the API documentation.
# Disabled for the moment as it takes forever to run, rely on manual execution.
RUN if [ "$swagInit" != "" ]; then swag init ; fi
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.build=${commit_ref}" -a -installsuffix nocgo -o /gosrv .
FROM alpine:3.9

View File

@ -13,8 +13,9 @@ ENV GO111MODULE="on"
COPY go.mod .
COPY go.sum .
RUN go mod download
RUN go get github.com/pilu/fresh
FROM build_base_golang AS builder
FROM build_base_golang AS dev
ARG service
ARG commit_ref=-
@ -23,15 +24,20 @@ ARG commit_ref=-
COPY internal ./internal
# Copy cmd specific packages.
COPY cmd/${service} ./cmd/web-app
COPY cmd/${service} ./cmd/${service}
COPY cmd/${service}/templates /templates
COPY cmd/${service}/static /static
# Copy the global templates.
ADD resources/templates/shared /templates/shared
ADD fresh-auto-reload.conf /runner.conf
WORKDIR ./cmd/${service}
ENTRYPOINT ["fresh", "-c", "/runner.conf"]
FROM dev AS builder
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.build=${commit_ref}" -a -installsuffix nocgo -o /gosrv .
FROM alpine:3.9

View File

@ -3,7 +3,6 @@
{{end}}
{{define "content"}}
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="/account">Account</a></li>

View File

@ -2,7 +2,7 @@
# docker-compose up
# docker-compose stop
# docker-compose down
version: '3'
version: '3.7'
networks:
main:
@ -63,9 +63,12 @@ services:
image: example-project/web-app:latest
build:
context: .
target: dev
dockerfile: cmd/web-app/Dockerfile
args:
service: 'web-app'
volumes:
- ./:/go/src/gitlab.com/geeks-accelerator/oss/saas-starter-kit
ports:
- 3000:3000 # WEB APP
- 4000:4000 # DEBUG API
@ -99,9 +102,12 @@ services:
image: example-project/web-api:latest
build:
context: .
target: dev
dockerfile: cmd/web-api/Dockerfile
args:
service: 'web-api'
volumes:
- ./:/go/src/gitlab.com/geeks-accelerator/oss/saas-starter-kit
ports:
- 3001:3001 # WEB API
- 4001:4001 # DEBUG API

14
fresh-auto-reload.conf Normal file
View File

@ -0,0 +1,14 @@
root: .
tmp_path: ./tmp
build_name: runner-build
build_log: runner-build-errors.log
valid_ext: .go, .tpl, .tmpl, .html, .gohtml
no_rebuild_ext: .tpl, .tmpl, .html
ignored: assets, tmp
build_delay: 600
colors: 1
log_color_main: cyan
log_color_build: yellow
log_color_runner: green
log_color_watcher: magenta
log_color_app:

4
go.mod
View File

@ -24,6 +24,7 @@ require (
github.com/gorilla/schema v1.1.0
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.0
github.com/howeyc/fsnotify v0.9.0 // indirect
github.com/huandu/go-sqlbuilder v1.4.1
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365
github.com/ikeikeikeike/go-sitemap-generator/v2 v2.0.2
@ -34,6 +35,7 @@ require (
github.com/leodido/go-urn v1.1.0 // indirect
github.com/lib/pq v1.2.0
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-sqlite3 v1.11.0 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/onsi/ginkgo v1.8.0 // indirect
@ -41,6 +43,8 @@ require (
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/pborman/uuid v1.2.0
github.com/philhofer/fwd v1.0.0 // indirect
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect
github.com/pilu/fresh v0.0.0-20170301142741-9c0092493eff // indirect
github.com/pkg/errors v0.8.1
github.com/sergi/go-diff v1.0.0
github.com/sethgrid/pester v0.0.0-20190127155807-68a33a018ad0

9
go.sum
View File

@ -90,6 +90,8 @@ github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyC
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ=
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/go-sqlbuilder v1.4.1 h1:DYGFGLbOUXhtQ2kwO1uyDIPJbsztmVWdPPDyxi0EJGw=
@ -125,7 +127,10 @@ github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
@ -149,6 +154,10 @@ github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ=
github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a h1:Tg4E4cXPZSZyd3H1tJlYo6ZreXV0ZJvE/lorNqyw1AU=
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a/go.mod h1:9Or9aIl95Kp43zONcHd5tLZGKXb9iLx0pZjau0uJ5zg=
github.com/pilu/fresh v0.0.0-20170301142741-9c0092493eff h1:/FQrxtJUVqC79XhN/OHwWzuSe051qehQCzZ3LIhdo5c=
github.com/pilu/fresh v0.0.0-20170301142741-9c0092493eff/go.mod h1:2LLTtftTZSdAPR/iVyennXZDLZOYzyDn+T0qEKJ8eSw=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=