diff --git a/README.md b/README.md index 2228ec0..fc6cffe 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/cmd/web-api/Dockerfile b/cmd/web-api/Dockerfile index 36ff03d..09a1a09 100644 --- a/cmd/web-api/Dockerfile +++ b/cmd/web-api/Dockerfile @@ -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 diff --git a/cmd/web-app/Dockerfile b/cmd/web-app/Dockerfile index 66ebe65..696f41b 100644 --- a/cmd/web-app/Dockerfile +++ b/cmd/web-app/Dockerfile @@ -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 diff --git a/cmd/web-app/templates/content/account-update.gohtml b/cmd/web-app/templates/content/account-update.gohtml index 87daf92..5f8b176 100644 --- a/cmd/web-app/templates/content/account-update.gohtml +++ b/cmd/web-app/templates/content/account-update.gohtml @@ -3,7 +3,6 @@ {{end}} {{define "content"}} -