Skip to content

Commit

Permalink
feat: 🚀 just started 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshadhin committed Dec 12, 2022
0 parents commit 49d2849
Show file tree
Hide file tree
Showing 66 changed files with 7,746 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Files
.dockerignore
.editorconfig
.gitignore
.gitattributes
config.yml
Dockerfile
LICENSE
**/*.md
**/*_test.go
*.out

# Folders
.idea/
.vscode/
.git/
.github/
bin/
vendor/
_deploy/
_doc/
data
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{go.mod, go.sum, *.go}]
indent_style = tab
indent_size = 4

[{Makefile, Dockerfile, *.yml, *.yaml}]
indent_style = tab
indent_size = 2
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go.sum merge=union
_doc/* linguist-vendored
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build
on:
push:
branches: [ 'master' ]
paths-ignore: [ '_deploy/**', '_doc/**' ]
jobs:
Build:
runs-on: ubuntu-latest
env:
GO111MODULE: on
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Get dependencies
run: make dl-deps
- name: Build binary
run: make build
- name: Check binary
run: make version
28 changes: 28 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Unit Test
on:
push:
branches: [ 'master' ]
paths-ignore: [ '_deploy/**', '_doc/**' ]
jobs:
Test:
runs-on: ubuntu-latest
env:
GO111MODULE: on
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Get dependencies
run: make dl-deps
- name: Generate coverage report
run: make test-unit
- name: Upload coverage report
uses: codecov/codecov-action@v3
with:
files: ./coverage.txt
flags: unittests
name: codecov-umbrella
fail_ci_if_error: true
verbose: true
41 changes: 41 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Integration Test
on:
push:
branches: [ 'master' ]
paths-ignore: [ '_deploy/**', '_doc/**' ]
jobs:
Test:
runs-on: ubuntu-latest
env:
GO111MODULE: on
DB_DATABASE: owntracks_test
DB_MYSQL_USER: root
DB_MYSQL_PASSWORD: root
DB_PGSQL_USER: dev
DB_PGSQL_PASSWORD: dev
steps:
- uses: actions/checkout@v3
- name: Set up MySQL
run: |
sudo systemctl start mysql.service
mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }};' -u${{ env.DB_MYSQL_USER }} -p${{ env.DB_MYSQL_PASSWORD }}
sed -i 's/toor/root/g' it/mysql/config.yml # override config
- name: Set up PostgreSQL
run: |
sudo systemctl start postgresql.service
pg_isready
sudo -u postgres psql --command="CREATE USER ${{ env.DB_PGSQL_USER }} PASSWORD '${{ env.DB_PGSQL_PASSWORD }}'" --command="\du"
sudo -u postgres createdb --owner=${{ env.DB_PGSQL_USER }} ${{ env.DB_DATABASE }}
PGPASSWORD=${{ env.DB_PGSQL_PASSWORD }} psql --username=${{ env.DB_PGSQL_USER }} --host=localhost --list ${{ env.DB_DATABASE }}
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Get dependencies
run: make dl-deps
- name: Run integration test [sqlite]
run: make test-integration
- name: Run integration test [mysql]
run: make test-integration-mysql
- name: Run integration test [pgsql]
run: make test-integration-pgsql
19 changes: 19 additions & 0 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Security
on:
push:
branches: [ 'master' ]
paths-ignore: [ '_deploy/**', '_doc/**' ]
jobs:
Gosec:
runs-on: ubuntu-latest
env:
GO111MODULE: on
steps:
- uses: actions/checkout@v3
- name: Run go generate
run: go generate ./cmd
- name: Run Gosec Security Scanner
run: | # https://github.com/securego/gosec/issues/469
export PATH=$PATH:$(go env GOPATH)/bin
go install github.com/securego/gosec/v2/cmd/gosec@latest
gosec ./...
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# IDE files
.vscode
.idea

# Mac OS X files
.DS_Store

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Output of the go coverage tool
*.out
coverage.txt

# Dependencies
vendor

# Misc
bin/
_doc/swagger.json
_doc/swagger.yaml
_doc/docs.go
config.yml
!it/config.yml
!it/**/config.yml
data
it/*.db
cmd/migrations
41 changes: 41 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
linters:
disable-all: true
enable:
- bodyclose
- depguard
- dogsled
- dupl
- errcheck
- exportloopref
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- noctx
- nolintlint
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
- path: _test\.go
linters:
- gomnd
49 changes: 49 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
ARG GO_VERSION=1.19

FROM golang:${GO_VERSION}-alpine AS builder

# Install dependencies
RUN apk add --no-cache git gcc musl-dev && rm -rf /var/cache/apk/*

# Working directory outside $GOPATH
WORKDIR /build

# Copy go module files and download dependencies
COPY go.* ./
RUN go mod download

# Add source files
ADD . .

# Build the Go app
ARG BUILD_VERSION=0.0.1
ARG BUILD_TIME=00000000-000000
RUN go generate ./cmd && GOOS=linux GOARCH=amd64 go build -ldflags "-w -s -X ot-recorder/app.Version=$BUILD_VERSION -X ot-recorder/app.BuildTime=$BUILD_TIME" -o ot-recorder .

# Minimal image for running the application
FROM alpine as final

LABEL org.opencontainers.image.source="https://github.com/hrshadhin/ot-recoder" \
org.opencontainers.image.url="https://github.com/hrshadhin/ot-recoder" \
org.opencontainers.image.title="A note taking applications"

# Install/Create dependent tools,location,directory
RUN apk add --no-cache curl tini && \
rm -rf /var/cache/apk/* && \
mkdir /persist && chown -R 1000:1000 /persist

# Import the compiled executable from the first stage.
COPY --from=builder /build/ot-recorder /app/ot-recorder

## Open port
EXPOSE 8000

## Perform any further action as an unprivileged location.
USER 1000
WORKDIR /app

HEALTHCHECK --interval=1m --timeout=1s --retries=3 --start-period=2s CMD ["curl", "--fail", "http://localhost:8000/health"]

## Run the compiled binary.
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/app/ot-recorder","--config","/app/config.yml", "serve"]
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 H.R. Shadhin <[email protected]> (https://hrshadhin.me)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
82 changes: 82 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env bash

BUILD_VERSION := $(shell git describe --always --tags)
BUILD_TIME=$(shell date '+%Y%m%d-%H%M%S')
DOCKER_IMAGE_NAME="hrshadhin/ot-recoder"
BINARY_NAME=ot-recoder
BIN_OUT_DIR=bin

export PATH=$(shell go env GOPATH)/bin:$(shell echo $$PATH)

.PHONY: all

all: dl-deps build test-unit ## Build binary (with unit tests)

help: ## Display this help screen
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

lint: build ## Run lint checks
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
$(shell go env GOPATH)/bin/golangci-lint run

fmt: ## Refactor go files with gofmt and goimports
go install golang.org/x/tools/cmd/goimports@latest
find . -name '*.go' | while read -r file; do goimports -w "$$file"; done

test-unit: ## Run unit tests
go test -v -coverprofile=coverage.txt -covermode=atomic -cover ./app/...

test-integration: ## Run sqlite integration tests
go test -v -tags=integration ./it -count=1

test-integration-mysql: ## Run mysql integration tests
go test -v -tags=integration ./it/mysql -count=1

test-integration-pgsql: ## Run pgsql integration tests
go test -v -tags=integration ./it/pgsql -count=1

clean: ## Cleans output directory
$(shell rm -rf $(BIN_OUT_DIR)/*)
$(shell rm -rf cmd/migrations)
$(shell rm -rf ./*.db ./it/*.db coverage.txt _doc/docs.go _doc/swagger.json _doc/swagger.yaml)

dl-deps: ## Get dependencies
go mod vendor

build: clean ## Build binary
go generate ./cmd
go build -v -ldflags="-w -s -X ot-recorder/app.Version=${BUILD_VERSION} -X ot-recorder/app.BuildTime=${BUILD_TIME}" -o $(BIN_OUT_DIR)/$(BINARY_NAME)

version: ## Check binary version
./$(BIN_OUT_DIR)/$(BINARY_NAME) --version

serve: build ## Run http server
./$(BIN_OUT_DIR)/$(BINARY_NAME) serve

doc: ## Creates swagger documentation as html file
go install github.com/swaggo/swag/cmd/[email protected]
$(shell go env GOPATH)/bin/swag init -g _doc/api.go -o _doc
$(shell which redoc-cli) build --options.disableSearch -o _doc/swagger.html _doc/swagger.json

migrate-up: ## Run migration
./$(BIN_OUT_DIR)/$(BINARY_NAME) migrate up

migrate-down: ## Revert migration
./$(BIN_OUT_DIR)/$(BINARY_NAME) migrate down

docker-build: ## Build docker image
docker build --build-arg BUILD_VERSION=${BUILD_VERSION} --build-arg BUILD_TIME=${BUILD_TIME} --tag ${DOCKER_IMAGE_NAME} .

docker-push: ## Push docker image
docker push

docker-run: ## Run docker image with sqlite
mkdir -p data
sudo chown -R 1000:1000 data
docker run --name ot-recoder --rm -it -p 8000:8000 \
-v $$(pwd)/config.yml:/app/config.yml \
-v $$(pwd)/data:/persist \
$(DOCKER_IMAGE_NAME):latest

docker-migrate: ## Run migrations inside dorker
docker exec ot-recoder /app/ot-recoder migrate up
Loading

0 comments on commit 49d2849

Please sign in to comment.