Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Aug 17, 2024
0 parents commit 272af85
Show file tree
Hide file tree
Showing 78 changed files with 5,091 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ref: https://github.com/air-verse/air/blob/master/air_example.toml

root = "."
tmp_dir = "bin/tmp"

[build]
bin = "bin/tmp/main"
cmd = "go build -o ./bin/tmp cmd/server/main.go"
delay = 0
exclude_dir = [".github", ".vscode", "bin", "deploy", "docs"]
exclude_regex = ["_test\\.go"]
exclude_unchanged = false
follow_symlink = false
include_dir = ["internal"]
kill_delay = 0
log = "air.log"
rerun = false
rerun_delay = 500
send_interrupt = true
stop_on_error = true

[color]
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = true

[misc]
clean_on_exit = true

[screen]
clear_on_rebuild = false
keep_scroll = true
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
bin/*
.air.toml
Makefile
38 changes: 38 additions & 0 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Lint, test, vet

on:
push:
pull_request:

jobs:
audit:
name: Audit
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.22

- name: Cache modules
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: |
go mod download
go install golang.org/x/lint/golint@latest
- name: Lint, test and vet
run: |
golint ./...
go test -v ./...
go vet ./...
31 changes: 31 additions & 0 deletions .github/workflows/check-build-health.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Check build health

on:
workflow_dispatch:

jobs:
build-health:
name: Check Docker container health
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Build Docker image
run: docker build --tag n8n-shortlink:ci .

- name: Run Docker container
run: |
docker run --detach --publish 3001:3001 --name n8n-shortlink-ci n8n-shortlink:ci
sleep 10 # wait for container to start up
- name: Check health endpoint
run: |
response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3001/health)
if [ $response -eq 200 ]; then
echo "Health check passed"
exit 0
else
echo "Health check failed with status code: $response"
exit 1
fi
66 changes: 66 additions & 0 deletions .github/workflows/release-on-tag-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Release on tag push

on:
push:
tags:
- 'v*.*.*'

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to the GHCR registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker image
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=semver,pattern={{version}}
type=sha
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image to GHCR
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

# - name: Create GitHub release
# uses: softprops/action-gh-release@v1
# if: startsWith(github.ref, 'refs/tags/')
# with:
# files: |
# LICENSE
# README.md
# body: |
# Docker image for this release: `ghcr.io/${{ github.repository }}:${{ github.ref_name }}`

# You can pull this image with:
# ```
# docker pull ghcr.io/${{ github.repository }}:${{ github.ref_name }}
# ```
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
bin/*
!**/.gitkeep
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"golang.Go",
"ms-vscode-remote.remote-ssh",
"ms-vscode-remote.remote-ssh-edit"
]
}
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"go.lintTool": "golint",
"go.lintOnSave": "package",
"workbench.colorCustomizations": {
"commandCenter.border": "#15202b99",
"sash.hoverBorder": "#abedd5",
"statusBar.background": "#81e4c0",
"statusBar.foreground": "#15202b",
"statusBarItem.hoverBackground": "#57dbab",
"statusBarItem.remoteBackground": "#81e4c0",
"statusBarItem.remoteForeground": "#15202b",
"titleBar.activeBackground": "#81e4c0",
"titleBar.activeForeground": "#15202b",
"titleBar.inactiveBackground": "#81e4c099",
"titleBar.inactiveForeground": "#15202b99"
},
"peacock.color": "#81e4c0"
}
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# syntax=docker/dockerfile:1

FROM golang:1.22-alpine AS builder
WORKDIR /builder-dir
COPY . ./
ENV CGO_ENABLED=1
RUN apk add --no-cache git build-base sqlite
RUN go mod download
RUN go build -o bin cmd/server/main.go

FROM alpine:latest
RUN mkdir /root/.n8n-shortlink
WORKDIR /root/n8n-shortlink
COPY --from=builder /builder-dir/bin bin
COPY --from=builder /builder-dir/internal/db/migrations internal/db/migrations
EXPOSE 3001
CMD ["./bin/main"]
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) 2024 Iván Ovejero

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.
132 changes: 132 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
.DEFAULT_GOAL := run

HOME_DIR := $(shell echo $$HOME)
DB_PATH := $(HOME_DIR)/.n8n-shortlink/n8n-shortlink.sqlite

help:
@echo "Commands:"
@sed -n 's/^##//p' $(MAKEFILE_LIST)

# ------------
# develop
# ------------

## run: Build and run binary
run:
go run cmd/server/main.go
.PHONY: run

## live: Run binary with live reload
live:
air
.PHONY: live

# ------------
# audit
# ------------

## audit: Tun `go mod tidy`, `go fmt`, `golint`, `go test`, and `go vet`
audit:
echo 'Tidying...'
go mod tidy
echo 'Formatting...'
go fmt ./...
echo 'Linting...'
golint ./...
echo 'Testing...'
go test -v ./...
echo 'Vetting...'
go vet ./...
.PHONY: audit

## test: Run tests
test:
gotestsum --format testname
.PHONY: test

## test/watch: Run tests in watch mode
test/watch:
gotestsum --format testname --watch
.PHONY: test

# ------------
# build
# ------------

CURRENT_TIME = $(shell date +"%Y-%m-%dT%H:%M:%S%z")
GIT_DESCRIPTION = $(shell git describe --always --dirty)
LINKER_FLAGS = '-s -X main.commitSha=${GIT_DESCRIPTION} -X main.buildTime=${CURRENT_TIME}'

## build: Build binary, burning in commit SHA and build time
build:
go build -ldflags=${LINKER_FLAGS} -o bin cmd/server/main.go
.PHONY:build

## build/meta: Display commit SHA and build time burned into binary
build/meta:
./bin/main -metadata-mode
.PHONY: build/meta

# ------------
# db
# ------------

## db/create: Create new database and apply up migrations
db/create:
@if [ -f ${DB_PATH} ]; then \
echo "\033[0;33mWarning:\033[0m This will overwrite the existing database.\nAre you sure? (y/n)"; \
read confirm && if [ "$$confirm" = "y" ]; then \
rm -f ${DB_PATH}; \
sqlite3 ${DB_PATH} ""; \
make db/mig/up; \
echo "\033[0;34mOK Created new empty database\033[0m"; \
else \
echo "\033[0;31mAborted database creation\033[0m"; \
fi \
else \
sqlite3 ${DB_PATH} ""; \
make db/mig/up; \
echo "\033[0;34mOK Created new empty database\033[0m"; \
fi
.PHONY: db/create

## db/connect: Connect to database
db/connect:
sqlite3 ${DB_PATH}
.PHONY: db/connect

## db/mig/new name=$1: Create new migration, e.g. `db/mig/new name=create_users_table`
db/mig/new:
migrate create -seq -ext=.sql -dir=./internal/db/migrations ${name}
echo "\033[0;34mOK Created migration files\033[0m"
.PHONY: db/mig/new

## db/mig/up: Apply up migrations
db/mig/up:
migrate -path="./internal/db/migrations" -database "sqlite3://$(DB_PATH)" up
@echo "\033[0;34mOK Applied up migrations\033[0m"
.PHONY: db/mig/up

# ------------
# docker
# ------------

## docker/build: Build Docker image `n8n-shortlink:local`
docker/build:
docker build --tag n8n-shortlink:local .
.PHONY: docker/build

## docker/run: Run Docker container off image `n8n-shortlink:local`
docker/run:
docker compose --file deploy/docker-compose.yml --profile local up
.PHONY: docker/run

## docker/stop: Stop Docker container
docker/stop:
docker stop n8n-shortlink
.PHONY: docker/run

## docker/connect: Connect to running Docker container `n8n-shortlink`
docker/connect:
docker exec -it n8n-shortlink sh
.PHONY: docker/connect
Loading

0 comments on commit 272af85

Please sign in to comment.