Skip to content

Commit

Permalink
start rewrite app in go
Browse files Browse the repository at this point in the history
Signed-off-by: Charly Molter <[email protected]>
  • Loading branch information
lahabana committed Nov 8, 2024
1 parent 0cbab4f commit 0e3125d
Show file tree
Hide file tree
Showing 24 changed files with 1,750 additions and 1,214 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
on: # yamllint disable-line rule:truthy
pull_request:
branches:
- main
push:
branches:
- main
- next
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: curl https://mise.run | sh
- run: make check
- run: make build
- run: make test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
.idea
bin/
dist/
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
issues:
exclude-rules:
48 changes: 48 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines below are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 2

before:
hooks:
- make generate
- make check

builds:
- env:
- CGO_ENABLED=0
main: ./app
id: kuma-counter-demo
goos:
- linux
- darwin

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

dockers:
- id: demo-app
image_templates:
- kumahq/kuma-counter-demo
dockerfile: ./app/Dockerfile
5 changes: 5 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[env]
MISE_DATA_DIR='bin/'

[settings]
experimental = true # To enable installation of Go tools from source with mise.
7 changes: 7 additions & 0 deletions .tools_versions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# renovate: datasource=github-releases depName=golangci/golangci-lint
golangci-lint: 1.61.0
# removate: datasource=github-releases depName=github.com/goreleaser/goreleaser/v2
goreleaser: 2.3.2
# removate: datasource=github-releases denName=com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen
oapi-codegen: 2.4.1
16 changes: 4 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
ARG ARCH=amd64
FROM --platform=linux/${ARCH} node:alpine
FROM golang:1.19

RUN apk add dumb-init
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build ./... -o /kuma-counter-demo

COPY /app/package.json /app/package.json
RUN npm install --prefix /app
FROM --platform=linux/${ARCH} distroless

COPY /app/public /app/public
COPY /app/server.js /app/server.js

EXPOSE 5000

WORKDIR "/app"
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "./server.js"]
CMD ["/kuma-counter-demo"]
11 changes: 11 additions & 0 deletions ERRORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Errors

The list of error details that can be returned by our app.

## REDIS-FAILURE

Redis failed in some way

## DEMOAPP-FAILURE

Demo-app failed
71 changes: 71 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Some sensible Make defaults: https://tech.davis-hansson.com/p/make/
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c

PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
TOOLS_VERSIONS_FILE = $(PROJECT_DIR)/.tools_versions.yaml

export MISE_DATA_DIR = $(PROJECT_DIR)/bin/
MISE := $(shell which mise)
.PHONY: mise
mise:
@mise -V >/dev/null || (echo "mise - https://github.com/jdx/mise - not found. Please install it." && exit 1)

GOLANGCI_LINT_VERSION = $(shell yq -ojson -r '.golangci-lint' < $(TOOLS_VERSIONS_FILE))
GOLANGCI_LINT = $(PROJECT_DIR)/bin/installs/golangci-lint/$(GOLANGCI_LINT_VERSION)/bin/golangci-lint
.PHONY: golangci-lint.download
golangci-lint.download: | mise ## Download golangci-lint locally if necessary.
$(MISE) install -y -q golangci-lint@$(GOLANGCI_LINT_VERSION)

GORELEASER_VERSION = $(shell yq -ojson -r '.goreleaser' < $(TOOLS_VERSIONS_FILE))
GORELEASER = $(PROJECT_DIR)/bin/installs/goreleaser/$(GORELEASER_VERSION)/bin/goreleaser
.PHONY: goreleaser.download
goreleaser.download: | mise ## Download goreleaser locally if necessary.
$(MISE) install -y -q goreleaser@$(GORELEASER_VERSION)

OAPI_CODEGEN_VERSION = $(shell yq -ojson -r '.oapi-codegen' < $(TOOLS_VERSIONS_FILE))
OAPI_CODEGEN = $(PROJECT_DIR)/bin/installs/oapi-codegen/$(OAPI_CODEGEN_VERSION)/bin/oapi-codegen
.PHONY: oapi-codegen.download
oapi-codegen.download: | mise ## Download oapi-codegen locally if necessary.
$(MISE) install -y -q oapi-codegen@$(OAPI_CODEGEN_VERSION)

app/internal/api/gen.go: openapi.yaml | oapi-codegen.download
$(OAPI_CODEGEN) --config openapi-config.yaml $<

.PHONY: clean
clean:
@rm -rf app/internal/api/gen.go
@rm -rf dist/
@rm -rf bin/

.PHONY: all
all: check build test run

.PHONY: check
check: tidy fmt lint

.PHONY: fmt
fmt:
go fmt ./...

.PHONY: lint
lint: tidy golangci-lint

.PHONY: tidy
tidy:
go mod tidy

.PHONY: golangci-lint
golangci-lint: | golangci-lint.download
$(GOLANGCI_LINT) run --verbose --config $(PWD)/.golangci.yaml $(GOLANGCI_LINT_FLAGS)

build: | goreleaser.download
$(GORELEASER) release --snapshot --clean

.PHONY: generate
generate: app/internal/api/gen.go
go generate ./...

.PHONY: test
test:
go test $$(go list ./... | grep -v 'e2e')
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Kuma Counter Demo

[![Run in Insomnia}](https://insomnia.rest/images/run.svg)](https://insomnia.rest/run/?label=Kuma-count-demo&uri=https%3A%2F%2Fgithub.com%2Fkumahq%2Fkuma-counter-demo%2Fblob%2Fmaster%2Fopenapi.yaml)

[![][kuma-logo]][kuma-url]

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/kumahq/kuma/blob/master/LICENSE)
Expand Down Expand Up @@ -201,4 +203,8 @@ To enforce response status code you need to set header `x-set-response-status-co

```shell
curl localhost:5001/increment -XPOST -H "x-set-response-status-code: 503"
```
```

### ErrorCodes

#### Error Redis failure
4 changes: 4 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM scratch

ENTRYPOINT ["/kuma-counter-demo"]
ADD kuma-counter-demo /
Loading

0 comments on commit 0e3125d

Please sign in to comment.