Skip to content

Instantly share code, notes, and snippets.

@naxmefy
Last active October 2, 2020 10:38
Show Gist options
  • Save naxmefy/9e4b24a20bba9d485b7d19a89c9c64f4 to your computer and use it in GitHub Desktop.
Save naxmefy/9e4b24a20bba9d485b7d19a89c9c64f4 to your computer and use it in GitHub Desktop.
Makefile for Golang Project with NPM Client, Air for backend development, Docker build, Heroku Deployment
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format
[build]
exclude_dir = ["assets", "tmp", "vendor", "client/node_modules", "client"]
.github/
.idea/
.vscode/
client/node_modules/
client/build/
tmp/
FROM node:12-alpine
WORKDIR /usr/app/src
COPY client .
RUN yarn
RUN yarn build
FROM golang:1.14
# TODO update ORG, and REPO to fit your requirements
WORKDIR /go/src/github.com/{ORG}/{REPO}/
COPY . .
COPY --from=0 /usr/app/src/build client/build
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-w" -a -installsuffix cgo -o app .
RUN go get -u github.com/GeertJohan/go.rice/rice
RUN rice append -i . --exec app
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
# TODO update ORG, and REPO to fit your requirements
COPY --from=1 /go/src/github.com/{ORG}/{REPO}/app .
EXPOSE 8080
CMD ["./app"]
build:
docker:
web: Dockerfile
worker:
dockerfile: Dockerfile
package main
import (
"log"
"net/http"
rice "github.com/GeertJohan/go.rice"
"github.com/labstack/echo/v4"
"github.com/{ORG}/{REPO}/pkg/api"
"github.com/{ORG}/{REPO}/pkg/config"
"github.com/{ORG}/{REPO}/pkg/util/database"
)
func main() {
c := config.New()
db := database.Init(c.DatabaseSourceName())
defer func() {
err := db.Close()
if err != nil {
log.Fatal(err)
}
}()
e := echo.New()
e.HideBanner = true
a := api.New(c)
a.SetupRoutes(e, "/api")
appBox, err := rice.FindBox("client/build")
if err != nil {
log.Fatal(err)
}
assetHandler := http.FileServer(appBox.HTTPBox())
e.GET("/*", echo.WrapHandler(assetHandler))
e.GET("/static/*", echo.WrapHandler(assetHandler))
e.Logger.Fatal(e.Start(c.ServerAddress()))
}
.PHONY: default
default: default-echo build;
.PHONY: default-echo
default-echo:
@echo Run Default targets: build
.PHONY: install
install: client-install server-install
.PHONY: build
build: client-build server-build
.PHONY: test
test: client-test server-test
###### DOCKER ######
# TODO update ORG, REPO, and IMAGE to fit your requirements
DOCKER_IMAGE_NAME=docker.pkg.github.com/{ORG}/{REPO}/{IMAGE}
DOCKER_IMAGE_TAG=next
.PHONY: docker-build
docker-build:
@docker build -t $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) .
.PHONY: docker-run
docker-run:
@echo docker run $(ARGS) $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) .
###### DEV ######
.PHONY: dev
dev:
@go get -u github.com/cosmtrek/air
@air -c .air.toml
###### SERVER ######
.PHONY: server-install
server-install:
@echo Install go backend dependencies.
@go get
@echo Install finished.
.PHONY: server-build
server-build: server-install
@echo Build go backend.
@CGO_ENABLED=0 go build -ldflags "-w" -a -o tmp/main main.go
@go get -u github.com/GeertJohan/go.rice/rice
@rice append -i . --exec tmp/main
@echo Build finished.
.PHONY: server-test
server-test: server-install
@echo Test go backend.
@go test -cover ./...
@echo Test finished.
###### CLIENT ######
.PHONY: client-install
client-install:
@echo Install npm client dependencies.
@(cd client && yarn)
@echo Install finished.
.PHONY: client-build
client-build: client install
@echo Build npm client.
@(cd client && yarn build)
@echo Build finished.
.PHONY: client-test
client-test: client-install
@echo Test npm client.
@(cd client && CI=true yarn test)
@echo Test finished.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment