Skip to content

Commit

Permalink
initial code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tinx committed Aug 29, 2024
0 parents commit 109c033
Show file tree
Hide file tree
Showing 56 changed files with 4,154 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "gomod" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
reviewers:
- "tinx"
71 changes: 71 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Create and publish Docker image

on:
push:
branches:
- 'main'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
build-and-push-docker-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Print debugging information
run: >
echo registry="$REGISTRY" &&
echo image-name="$IMAGE_NAME" &&
echo image-tags="$IMAGE_TAGS" &&
echo full-repo-url="$FULL_REPO_URL" &&
echo branch-or-tag-name="$BRANCH_OR_TAG_NAME" &&
echo commit-hash="$COMMIT_HASH" &&
echo registry-user="$REGISTRY_USER"
shell: bash
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
IMAGE_TAGS: latest
FULL_REPO_URL: https://github.com/${{ github.repository }}
BRANCH_OR_TAG_NAME: ${{ github.ref_name }}
COMMIT_HASH: ${{ github.sha }}
REGISTRY_USER: ${{ github.actor }}

- name: Checkout repository
run: 'git clone -b "$BRANCH_OR_TAG_NAME" --depth 1 "$REPO_URL_WITH_AUTH" app'
shell: bash
env:
REPO_URL_WITH_AUTH: https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
BRANCH_OR_TAG_NAME: ${{ github.ref_name }}

- name: Log in to the Container registry
run: 'echo "$REGISTRY_PASS" | docker login "$REGISTRY" -u "$REGISTRY_USER" --password-stdin'
shell: bash
env:
REGISTRY: ghcr.io
REGISTRY_USER: ${{ github.actor }}
REGISTRY_PASS: ${{ secrets.GITHUB_TOKEN }}

- name: Docker build and push image
run: >
cd app &&
TAG_ARGS=$(echo -n "$IMAGE_TAGS" | sed -r "s_([^ :/]+)_ --tag $REGISTRY/${IMAGE_NAME,,}:\1 _g") &&
docker build
--label org.opencontainers.image.url="$FULL_REPO_URL"
--label org.opencontainers.image.revision="$COMMIT_HASH"
$TAG_ARGS
--pull
-f Dockerfile
. &&
docker push -a "$REGISTRY/${IMAGE_NAME,,}"
shell: bash
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
IMAGE_TAGS: latest
FULL_REPO_URL: https://github.com/${{ github.repository }}
COMMIT_HASH: ${{ github.sha }}
37 changes: 37 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Go

on:
push:
branches: [ main ]
paths-ignore:
- '**.md'
pull_request:
branches: [ main ]
paths-ignore:
- '**.md'

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
path: .

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '^1.22.2'

- name: Build
run: go build -v ./...
working-directory: .

- name: Test
run: go test -v ./...
working-directory: .

- name: Vet
run: go vet ./...
working-directory: .
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea/**
.vscode/**
api/generator/openapi-generator-cli-*.jar
*.exe
/local-config.yaml
main
artshow-artbattle
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1 as build

COPY . /app
WORKDIR /app

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" cmd/main.go

RUN chmod 755 main

FROM scratch

COPY --from=build /app/main /main
COPY --from=build /etc/ssl/certs /etc/ssl/certs

# run as an unprivileged unnamed user that has no write permissions anywhere binary
USER 8877

ENTRYPOINT ["/main"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Eurofurence e.V.

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.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# artshow-artbattle

<img src="https://github.com/eurofurence/artshow-artbattle/actions/workflows/go.yml/badge.svg" alt="test status"/>

## Overview

Purpose of the service.

## Installation

This service uses go modules to provide dependency management, see `go.mod`.

If you place this repository outside your gopath, `go build cmd/main.go` and
`go test ./...` will download all required dependencies by default.

## Generate models

In a shell or git bash in the project root, run `./api/generator/generate.sh`.

Models are checked in for convenience and change tracking.

_Note: the generator needs a current Java runtime environment._

## Running on development system

Copy the configuration template from `docs/local-config.template.yaml` to `./local-config.yaml`
and edit as needed.

Then run `go run cmd/main.go`.

## Test Coverage

In order to collect full test coverage, set go tool arguments to `-coverpkg=./internal/...`,
or manually run
```
go test -coverpkg=./internal/... ./...
```

## Architecture

Components are grouped by stereotypes:
* controller = anything that's being interacted with from the outside
* service = business logic
* repository = anything that interacts with the outside
38 changes: 38 additions & 0 deletions api/generator/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#! /bin/bash

set -e

if [ -d "api" ]; then
cd api
fi

if [ -d "generator" ]; then
cd generator
fi

GENERATOR_VERSION="7.6.0"
GENERATOR="openapi-generator-cli-$GENERATOR_VERSION.jar"

if [ ! -f "$GENERATOR" ]; then
curl https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/$GENERATOR_VERSION/$GENERATOR > $GENERATOR
fi

API_MODEL_PACKAGE_NAME="apimodel"

rm -rf tmp

mkdir -p tmp

java -jar "$GENERATOR" generate \
-i "../openapi-spec.yaml" \
-o "tmp/$API_MODEL_PACKAGE_NAME" \
--package-name "$API_MODEL_PACKAGE_NAME" \
--global-property models,modelTests=false,modelDocs=false \
-g go

( cat tmp/"$API_MODEL_PACKAGE_NAME"/model_*.go | \
go run postprocess.go "$API_MODEL_PACKAGE_NAME" > "../../internal/$API_MODEL_PACKAGE_NAME/apimodel.go" || \
(rm -rf tmp && exit 1) \
); rm -rf tmp

gofmt -l -w "../../internal/$API_MODEL_PACKAGE_NAME/apimodel.go"
55 changes: 55 additions & 0 deletions api/generator/postprocess.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
)

var regexTypeBegins = regexp.MustCompile(`^type .* {$`)

func main() {
fmt.Printf(`// Code generated by generate.sh using openapi-generator-cli. DO NOT EDIT.
package %s
import "time"
var _ = time.Now
`, packageNameArg())

enabled := false
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "type Nullable") {
// ignore
} else if regexTypeBegins.MatchString(line) {
fmt.Println(line)
enabled = true
} else if line == "}" && enabled {
fmt.Println(line)
fmt.Println()
enabled = false
} else if enabled {
fmt.Println(line)
}
}

if err := scanner.Err(); err != nil {
log.Println(err)
os.Exit(2)
}
}

func packageNameArg() string {
if len(os.Args) != 2 {
log.Println("required first argument missing: package name")
os.Exit(1)
}
return os.Args[1]
}
Loading

0 comments on commit 109c033

Please sign in to comment.