Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
zephinzer committed Dec 23, 2023
1 parent e06c29a commit 9dbb443
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 16 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.data
.github
bin
deploy
.envrc
tests
vendor
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.envrc
Makefile.properties
vendor
28 changes: 20 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
ARG GO_VERSION=1.20
FROM golang:${GO_VERSION}-alpine AS build
RUN apk add --no-cache make g++ ca-certificates
WORKDIR /go/src/app
ARG APP_NAME=app
RUN apk update --no-cache
RUN apk add --no-cache \
ca-certificates \
g++ \
git \
make
RUN go install github.com/swaggo/swag/cmd/swag@master
WORKDIR /go/src/${APP_NAME}
COPY ./go.mod ./go.sum ./
RUN go mod download -x
COPY . .
COPY ./.git ./.git
COPY ./cmd ./cmd
COPY ./internal ./internal
COPY ./Makefile .
ENV CGO_ENABLED=0
RUN go install github.com/swaggo/swag/cmd/swag@master
RUN make docs-swaggo
RUN make deps
RUN make binary
RUN sha256sum ./bin/app > ./bin/app.sha256
RUN sha256sum ./bin/${APP_NAME} > ./bin/${APP_NAME}.sha256

FROM scratch AS final
COPY --from=build /go/src/app/bin/app /app
COPY --from=build /go/src/app/bin/app.sha256 /app.sha256
ARG APP_NAME=app
ENV APP_NAME=${APP_NAME}
COPY --from=build /go/src/${APP_NAME}/bin/${APP_NAME} /entrypoint
COPY --from=build /go/src/${APP_NAME}/bin/${APP_NAME}.sha256 /${APP_NAME}.sha256
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ENTRYPOINT ["/app"]
# CMD [ /bin/bash ]
ENTRYPOINT [ "/entrypoint" ]
56 changes: 51 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
######################
# app configurations #
######################
## set this to the namespace of your app or organisation
## of your app, this is used for build artifacts, not the
## deployment
APP_NAMESPACE := app

## set this to the universal identifier of your app
APP_NAME := app

## this assumes starting the server, set it to anything you want for
## `make start` to trigger
ARGS_START := start server

#######################
# path configurations #
#######################
BIN_PATH := ./bin
CMD_PATH := ./cmd
DOCS_OUTPUT_PATH := ./internal/docs
HELM_CHARTS_PATH := ./deploy/charts
IMAGE_PATH := ${APP_NAME}/${APP_NAME}

########################
# build configurations #
########################
## this is the hostname of the image registry where your built image will
## be published to
IMAGE_REGISTRY := docker.io

## this is the rest of the path of the image
IMAGE_PATH := ${APP_NAMESPACE}/${APP_NAME}

## this is the image tag, set this to something like the version or some
## identifier of the version of your application
IMAGE_TAG := latest

######################
# ops configurations #
######################
## kubernetes configurations
K8S_NAMESPACE := default

## kafka configurations
KAFKA_ALIAS := localhost
KAFKA_CERTS_PATH := ./.data/kafka/config/certs

## swaggo url
SWAGGO_URL := github.com/swaggo/swag

# everything before this next line is configurable in the Makefile.properties
Expand All @@ -32,13 +68,14 @@ ifeq ("${GOOS}", "windows")
BINARY_EXT := ".exe"
endif

binary:
binary: docs
@echo "building binary for ${APP_NAME} for os/arch $$(go env GOOS)/$$(go env GOARCH)..."
@mkdir -p "${BIN_PATH}"
@go build \
-ldflags "\
-extldflags 'static' -s -w \
-X ${APP_NAME}/internal/constants.AppName=${APP_NAME} \
-X ${APP_NAME}/internal/constants.BuildTimestamp=$$(date --utc +'%Y-%m-%dT%H:%M:%S') \
-X ${APP_NAME}/internal/constants.Version=$$(git rev-parse --abbrev-ref HEAD)-$$(git rev-parse HEAD | head -c 6) \
" \
-o "${BIN_PATH}/${APP_NAME}${BINARY_EXT}" \
Expand All @@ -50,8 +87,12 @@ deps:
@go mod tidy
@go mod vendor

docs:
@echo "updating documentation"
@$(MAKE) docs-swaggo

docs-swaggo:
@echo "generating documentation package..."
@echo "generating swagger documentation package..."
swag init \
--generalInfo "./internal/api/api.go" \
--output "${DOCS_OUTPUT_PATH}" \
Expand All @@ -60,7 +101,7 @@ docs-swaggo:
--parseDepth 8

image:
@echo "building image ${IMAGE_URL}:${IMAGE_TAG}..."addr
@echo "building image ${IMAGE_URL}:${IMAGE_TAG}..."
docker build -t ${IMAGE_URL}:${IMAGE_TAG} .

deploy-k8s:
Expand Down Expand Up @@ -112,7 +153,12 @@ kind-load: image
@kind load docker-image ${IMAGE_URL}:${IMAGE_TAG} --name ${KIND_CLUSTER_NAME}

nats-nkey:
nk -gen user -pubout
@echo "generating nkey for use with nats..."
@nk -gen user -pubout

publish-image: image
docker tag ${IMAGE_URL}:${IMAGE_TAG} ${IMAGE_URL}:$$(git branch --show-current)-$$(git rev-parse HEAD | head -c 8)
docker push ${IMAGE_URL}:$$(git branch --show-current)-$$(git rev-parse HEAD | head -c 8)

start: docs
@go run "${CMD_PATH}/${APP_NAME}" ${ARGS_START}
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ This repository uses Makefile for storing development operations. Included opera
- `make deploy-kind` deploys the application onto a local Kubernetes cluster
- `make deploy-k8s` deploys the application onto the currently selected Kubernetes cluster
- `make deps` pulls in the dependencies into `./vendor/`
- `make docs-swaggo` generates the Swagger documentation using `swaggo`
- `make docs` generates all documentation
- `make docs-swaggo` generates just the Swagger documentation using `swaggo`
- `make image` builds the Docker image
- `make install-swaggo` installs `swaggo` at the latest version
- `make kafka-jks` generates the keys and certificates required for Kafka security
- `make kind-load` loads the image into the local Kubernetes cluster
- `make nats-nkey` creates a new nkey for use with NATS
- `make publish-image` publishes the image in it's current form using a concatenation of `${GIT_BRANCH}` and `${GIT_COMMIT_SHA}` separated by a `-` as the image tag
- `make start` starts the HTTP server using defaults
- `make start-kafka` starts a local Kafka instance via Docker Compose
- `make start-kind` starts a local Kubernetes cluster with KinD
Expand Down
7 changes: 5 additions & 2 deletions internal/constants/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package constants

import "time"

var (
AppName = "app"
Version = "0.1.0"
AppName = "app"
Version = "0.1.0"
BuildTimestamp = time.Now().UTC().Format("2006-01-02T15:04:05")
)

0 comments on commit 9dbb443

Please sign in to comment.