Skip to content
Merged
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
Binary file added .build/server/server
Binary file not shown.
6 changes: 6 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LOG_FORMAT=text

# STORAGE_TYPE=redis
# REDIS_HOST=localhost
# REDIS_USERNAME=
# REDIS_PASSWORD=
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: gomod
directory: /
schedule:
interval: daily
commit-message:
prefix: "deps"
50 changes: 50 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: CI

on:
pull_request:
branches: "**"

permissions: read-all

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v7
with:
version: v2.10.1
args: --timeout 5m

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: go.mod

- run: go mod download && go mod tidy && go mod verify
- run: git --no-pager diff --exit-code

- run: go vet ./...
- run: git --no-pager diff --exit-code

- run: go fmt ./...
- run: git --no-pager diff --exit-code

- name: Check generate produces no diff
run: |
go generate ./...
go run github.com/mockzilla/connexions/v2/cmd/gen/discover pkg
git --no-pager diff --exit-code

- name: Test
run: go test -race -count=1 ./...
105 changes: 105 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: Release

on:
push:
branches: [main, master]

env:
BINARY_NAME: ${{ github.event.repository.name }}

permissions:
contents: write

jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Generate and discover
run: |
go generate ./...
go run github.com/mockzilla/connexions/v2/cmd/gen/discover pkg

- uses: actions/upload-artifact@v4
with:
name: generated-sources
path: |
pkg/*/gen.go
pkg/*/service.go
pkg/*/setup/openapi.yml
cmd/server/services_gen.go

build:
needs: generate
runs-on: ubuntu-latest
strategy:
matrix:
include:
- goos: linux
goarch: amd64
- goos: linux
goarch: arm64
- goos: darwin
goarch: amd64
- goos: darwin
goarch: arm64
- goos: windows
goarch: amd64
- goos: windows
goarch: arm64

steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: go.mod

- uses: actions/download-artifact@v4
with:
name: generated-sources

- name: Build
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
run: |
ext=""
if [ "$GOOS" = "windows" ]; then ext=".exe"; fi
go build -o ${BINARY_NAME}-${GOOS}-${GOARCH}${ext} ./cmd/server/

- uses: actions/upload-artifact@v4
with:
name: ${{ env.BINARY_NAME }}-${{ matrix.goos }}-${{ matrix.goarch }}
path: ${{ env.BINARY_NAME }}-*

release:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
path: artifacts
pattern: ${{ env.BINARY_NAME }}-*
merge-multiple: true

- name: Delete existing latest release
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
run: gh release delete latest --yes 2>/dev/null || true

- name: Create release
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
run: |
gh release create latest artifacts/* \
--title "Latest build" \
--notes "Built from $(echo ${GITHUB_SHA} | cut -c1-7)" \
--latest
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.build
26 changes: 26 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: "2"

run:
timeout: 3m
tests: false

issues:
max-same-issues: 0

linters:
enable:
- wastedassign
- goconst
- cyclop
disable:
- unused
- revive
exclusions:
paths:
- pkg/.*/handler
- pkg/.*/types
- "generate\\.go"

formatters:
enable:
- goimports
57 changes: 57 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
SHELL = /bin/bash
VERSION ?= "latest"
build_dir := ./.build

.PHONY: clean
clean:
rm -rf ${build_dir}

.PHONY: clean-cache
clean-cache:
go clean -cache -modcache -i -r

.PHONY: build
build: clean generate discover
@echo "Go version: $(GO_VERSION)"
@go mod download
@go build $(GO_BUILD_FLAGS) -o ${build_dir}/server/server ./cmd/server/

.PHONY: mod
mod:
go mod download && go mod tidy && go mod verify

.PHONY: vet
vet:
go vet ./...

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

.PHONY: lint
lint: mod vet fmt
echo "Running golangci-lint..."
@golangci-lint run -c .golangci.yml --timeout=5m

.PHONY: test
test:
go test -race -count=1 ./...

.PHONY: service-from-static
service-from-static:
@echo "Generating new static service..."
@go run github.com/mockzilla/connexions/v2/cmd/gen/service -type static -name=$(name) -output=pkg/$(name)

.PHONY: service
service:
@echo "Generating new OpenAPI service..."
@go run github.com/mockzilla/connexions/v2/cmd/gen/service -type openapi -name=$(name) -output=pkg/$(name)

.PHONY: discover
discover:
@echo "Discovering services to generate service imports..."
@go run github.com/mockzilla/connexions/v2/cmd/gen/discover pkg

#.PHONY: generate
generate:
@go generate ./...
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Connexions Codegen Template

Generate a Go mock server from OpenAPI specs with [Connexions](https://github.com/mockzilla/connexions).

Each service is a Go package with generated handlers, embedded OpenAPI specs, and optional custom logic.
Includes an API Explorer UI at `/api-explorer`.

## Quick start

1. Click [**Use this template**](https://github.com/mockzilla/connexions-codegen-template/generate) to create your own repository
2. Add services (see below)
3. Regenerate and discover:
```bash
make generate && make discover
```
4. Push to main - binaries for Linux, macOS, and Windows are built automatically and published to **Releases**

## Adding services

### From an OpenAPI spec

```bash
make service name=my-api
```

This creates `pkg/my_api/` with a scaffold. Replace `pkg/my_api/setup/openapi.yml` with your spec, then run:

```bash
make generate && make discover
```

### From static responses

```bash
make service-from-static name=my-api
```

Add response files under `pkg/my_api/setup/data/` organized by method and path:

```
pkg/my_api/setup/data/
get/
users/
index.json -> GET /users
users/{id}/
index.json -> GET /users/{id}
post/
users/
index.json -> POST /users
```

Then regenerate:

```bash
make generate && make discover
```

## Service structure

Each service lives in `pkg/{service_name}/` with:

```
pkg/petstore/
setup/
openapi.yml # OpenAPI specification
config.yml # Latency, errors, upstream, caching
codegen.yml # Code generation settings
context.yml # Custom values for mock data
generate.go # go:generate directive
gen.go # Generated handler (do not edit)
service.go # Custom logic (optional overrides)
```

## API Explorer UI

The built-in UI is available at `/` (configurable in `resources/data/app.yml`).
It lets you browse services, view specs, test endpoints, and inspect request/response history.

## Local development

```bash
make build
.build/server/server
```

See the [Connexions docs](https://github.com/mockzilla/connexions) for more options.
Loading
Loading