Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/deploy-crudoshlep.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: deploy-crudoshlep

on:
workflow_dispatch:
inputs:
environment:
required: true
description: Deploy to DEV/PROD
type: choice
options: [DEV, PROD]

jobs:
deploy:
uses: ./.github/workflows/deploy.yaml
permissions:
packages: write
contents: read
attestations: write
id-token: write
secrets: inherit
with:
dockerfile_path: 'crudoshlep'
context_path: '.'
image_name: 'crudoshlep'
environment: ${{ github.event.inputs.environment }}
secret-service-hash: ${{ github.event.inputs.environment == 'PROD' && 'CRUDOSHLEP_SERVICE_HASH' || 'CRUDOSHLEP_SERVICE_HASH_DEV' }}
11 changes: 11 additions & 0 deletions crudoshlep/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DEBUG=true

# Server
HTTP_PORT=8000

# Database
POSTGRES_USER=root
POSTGRES_PASSWORD=root
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=root
3 changes: 3 additions & 0 deletions crudoshlep/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin
.vscode
.env
20 changes: 20 additions & 0 deletions crudoshlep/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Build stage
FROM golang:1.24-alpine AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -o server ./cmd/server/main.go

# Run stage
FROM alpine:latest

WORKDIR /app

COPY --from=builder /app/server .

CMD ["./server"]
66 changes: 66 additions & 0 deletions crudoshlep/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
ifneq (,$(wildcard ./.env))
include .env
export
endif

.PHONY: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Run
server-run: ## Run server
go run cmd/server/main.go

compose-up: ## Launch full service in docker-compose
docker compose up --build -d && docker compose logs -f

compose-down: ## Compose down
docker compose down

##@ Database
db-compose-up: ## Launch database+adminer from docker-compose
docker compose up database adminer --build -d && docker compose logs -f

DB_URL ?= postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):$(POSTGRES_PORT)/$(POSTGRES_DB)?sslmode=disable

db-migrate-up: migrate-install ## Migrate up
$(MIGRATE) -database $(DB_URL) -path migrations up

db-migrate-down: migrate-install ## Migrate down
$(MIGRATE) -database $(DB_URL) -path migrations down


##@ Tools
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
golangci-lint-install:
$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.63.4)

SWAG = $(shell pwd)/bin/swag
swag-install:
$(call go-get-tool,$(SWAG),github.com/swaggo/swag/cmd/swag@v1.16.3)

MIGRATE = $(shell pwd)/bin/migrate
migrate-install:
$(call go-get-tool,$(MIGRATE),-tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@v4.17.1)

lint: golangci-lint-install ## Lint (github.com/golangci/golangci-lint)
$(GOLANGCI_LINT) run

lint-fix: golangci-lint-install ## Lint fix
$(GOLANGCI_LINT) run --fix

swag: swag-install ## Generate swag documentation (github.com/swaggo/swag)
$(SWAG) init -g cmd/server/main.go -o docs --parseDependency

PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
46 changes: 46 additions & 0 deletions crudoshlep/cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"errors"
"net/http"
"os"
"os/signal"

"github.com/jackc/pgx/v5/pgxpool"
"github.com/shampsdev/dishdash-server/crudoshlep/pkg/config"
"github.com/shampsdev/dishdash-server/crudoshlep/pkg/gateways/rest"
"github.com/shampsdev/dishdash-server/crudoshlep/pkg/usecase"
log "github.com/sirupsen/logrus"
)

// @title Crudoshlep server
// @version 1.0
// @description Save analytics events
func main() {
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
log.SetLevel(log.DebugLevel)

log.Info("Hello from tglinked server!")

cfg := config.Load(".env")
cfg.Print()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

pgConfig := cfg.PGXConfig()
pool, err := pgxpool.NewWithConfig(ctx, pgConfig)
if err != nil {
log.Fatal("can't create new database pool")
}
defer pool.Close()

s := rest.NewServer(cfg, usecase.Setup(pool))
if err := s.Run(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.WithError(err).Error("error during server shutdown")
}
}
36 changes: 36 additions & 0 deletions crudoshlep/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
database:
restart: unless-stopped
image: postgres:16.7
container_name: database
volumes:
- ~/.pg/crudoshlep-db:/var/lib/postgresql/data
env_file:
- .env
ports:
- "5432:5432"
adminer:
image: adminer
container_name: adminer
restart: always
env_file:
- .env
ports:
- "1000:8080"
depends_on:
- database
server:
container_name: server
build:
context: .
dockerfile: Dockerfile
env_file:
- .env
environment:
HTTP_PORT: 8000
POSTGRES_HOST: database
depends_on:
- database
ports:
- "8000:8000"
restart: unless-stopped
95 changes: 95 additions & 0 deletions crudoshlep/docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs

import "github.com/swaggo/swag"

const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/events": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Save event, you can type any valid json in data field",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"events"
],
"summary": "Save event",
"parameters": [
{
"description": "Event data",
"name": "event",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/github_com_shampsdev_dishdash-server_crudoshlep_pkg_usecase.SaveEventInput"
}
}
],
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "Bad Request"
},
"500": {
"description": "Internal Server Error"
}
}
}
}
},
"definitions": {
"github_com_shampsdev_dishdash-server_crudoshlep_pkg_usecase.SaveEventInput": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "integer"
}
},
"name": {
"type": "string"
}
}
}
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "Crudoshlep server",
Description: "Save analytics events",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
69 changes: 69 additions & 0 deletions crudoshlep/docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"swagger": "2.0",
"info": {
"description": "Save analytics events",
"title": "Crudoshlep server",
"contact": {},
"version": "1.0"
},
"paths": {
"/events": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Save event, you can type any valid json in data field",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"events"
],
"summary": "Save event",
"parameters": [
{
"description": "Event data",
"name": "event",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/github_com_shampsdev_dishdash-server_crudoshlep_pkg_usecase.SaveEventInput"
}
}
],
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "Bad Request"
},
"500": {
"description": "Internal Server Error"
}
}
}
}
},
"definitions": {
"github_com_shampsdev_dishdash-server_crudoshlep_pkg_usecase.SaveEventInput": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "integer"
}
},
"name": {
"type": "string"
}
}
}
}
}
Loading