Skip to content

Commit 80f45fd

Browse files
authored
Merge pull request #1 from mockzilla/ig/add-template
Add template
2 parents 13008e6 + bd7b9cd commit 80f45fd

29 files changed

Lines changed: 2742 additions & 0 deletions

.build/server/server

47.1 MB
Binary file not shown.

.env.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LOG_FORMAT=text
2+
3+
# STORAGE_TYPE=redis
4+
# REDIS_HOST=localhost
5+
# REDIS_USERNAME=
6+
# REDIS_PASSWORD=

.github/dependabot.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: gomod
4+
directory: /
5+
schedule:
6+
interval: daily
7+
commit-message:
8+
prefix: "deps"

.github/workflows/ci.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches: "**"
6+
7+
permissions: read-all
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- uses: actions/setup-go@v5
16+
with:
17+
go-version-file: go.mod
18+
19+
- name: Run golangci-lint
20+
uses: golangci/golangci-lint-action@v7
21+
with:
22+
version: v2.10.1
23+
args: --timeout 5m
24+
25+
test:
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: actions/checkout@v4
29+
30+
- uses: actions/setup-go@v5
31+
with:
32+
go-version-file: go.mod
33+
34+
- run: go mod download && go mod tidy && go mod verify
35+
- run: git --no-pager diff --exit-code
36+
37+
- run: go vet ./...
38+
- run: git --no-pager diff --exit-code
39+
40+
- run: go fmt ./...
41+
- run: git --no-pager diff --exit-code
42+
43+
- name: Check generate produces no diff
44+
run: |
45+
go generate ./...
46+
go run github.com/mockzilla/connexions/v2/cmd/gen/discover pkg
47+
git --no-pager diff --exit-code
48+
49+
- name: Test
50+
run: go test -race -count=1 ./...

.github/workflows/release.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
7+
env:
8+
BINARY_NAME: ${{ github.event.repository.name }}
9+
10+
permissions:
11+
contents: write
12+
13+
jobs:
14+
generate:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/setup-go@v5
20+
with:
21+
go-version-file: go.mod
22+
23+
- name: Generate and discover
24+
run: |
25+
go generate ./...
26+
go run github.com/mockzilla/connexions/v2/cmd/gen/discover pkg
27+
28+
- uses: actions/upload-artifact@v4
29+
with:
30+
name: generated-sources
31+
path: |
32+
pkg/*/gen.go
33+
pkg/*/service.go
34+
pkg/*/setup/openapi.yml
35+
cmd/server/services_gen.go
36+
37+
build:
38+
needs: generate
39+
runs-on: ubuntu-latest
40+
strategy:
41+
matrix:
42+
include:
43+
- goos: linux
44+
goarch: amd64
45+
- goos: linux
46+
goarch: arm64
47+
- goos: darwin
48+
goarch: amd64
49+
- goos: darwin
50+
goarch: arm64
51+
- goos: windows
52+
goarch: amd64
53+
- goos: windows
54+
goarch: arm64
55+
56+
steps:
57+
- uses: actions/checkout@v4
58+
59+
- uses: actions/setup-go@v5
60+
with:
61+
go-version-file: go.mod
62+
63+
- uses: actions/download-artifact@v4
64+
with:
65+
name: generated-sources
66+
67+
- name: Build
68+
env:
69+
GOOS: ${{ matrix.goos }}
70+
GOARCH: ${{ matrix.goarch }}
71+
run: |
72+
ext=""
73+
if [ "$GOOS" = "windows" ]; then ext=".exe"; fi
74+
go build -o ${BINARY_NAME}-${GOOS}-${GOARCH}${ext} ./cmd/server/
75+
76+
- uses: actions/upload-artifact@v4
77+
with:
78+
name: ${{ env.BINARY_NAME }}-${{ matrix.goos }}-${{ matrix.goarch }}
79+
path: ${{ env.BINARY_NAME }}-*
80+
81+
release:
82+
needs: build
83+
runs-on: ubuntu-latest
84+
steps:
85+
- uses: actions/download-artifact@v4
86+
with:
87+
path: artifacts
88+
pattern: ${{ env.BINARY_NAME }}-*
89+
merge-multiple: true
90+
91+
- name: Delete existing latest release
92+
env:
93+
GH_TOKEN: ${{ github.token }}
94+
GH_REPO: ${{ github.repository }}
95+
run: gh release delete latest --yes 2>/dev/null || true
96+
97+
- name: Create release
98+
env:
99+
GH_TOKEN: ${{ github.token }}
100+
GH_REPO: ${{ github.repository }}
101+
run: |
102+
gh release create latest artifacts/* \
103+
--title "Latest build" \
104+
--notes "Built from $(echo ${GITHUB_SHA} | cut -c1-7)" \
105+
--latest

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
.build

.golangci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: "2"
2+
3+
run:
4+
timeout: 3m
5+
tests: false
6+
7+
issues:
8+
max-same-issues: 0
9+
10+
linters:
11+
enable:
12+
- wastedassign
13+
- goconst
14+
- cyclop
15+
disable:
16+
- unused
17+
- revive
18+
exclusions:
19+
paths:
20+
- pkg/.*/handler
21+
- pkg/.*/types
22+
- "generate\\.go"
23+
24+
formatters:
25+
enable:
26+
- goimports

Makefile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
SHELL = /bin/bash
2+
VERSION ?= "latest"
3+
build_dir := ./.build
4+
5+
.PHONY: clean
6+
clean:
7+
rm -rf ${build_dir}
8+
9+
.PHONY: clean-cache
10+
clean-cache:
11+
go clean -cache -modcache -i -r
12+
13+
.PHONY: build
14+
build: clean generate discover
15+
@echo "Go version: $(GO_VERSION)"
16+
@go mod download
17+
@go build $(GO_BUILD_FLAGS) -o ${build_dir}/server/server ./cmd/server/
18+
19+
.PHONY: mod
20+
mod:
21+
go mod download && go mod tidy && go mod verify
22+
23+
.PHONY: vet
24+
vet:
25+
go vet ./...
26+
27+
.PHONY: fmt
28+
fmt:
29+
go fmt ./...
30+
31+
.PHONY: lint
32+
lint: mod vet fmt
33+
echo "Running golangci-lint..."
34+
@golangci-lint run -c .golangci.yml --timeout=5m
35+
36+
.PHONY: test
37+
test:
38+
go test -race -count=1 ./...
39+
40+
.PHONY: service-from-static
41+
service-from-static:
42+
@echo "Generating new static service..."
43+
@go run github.com/mockzilla/connexions/v2/cmd/gen/service -type static -name=$(name) -output=pkg/$(name)
44+
45+
.PHONY: service
46+
service:
47+
@echo "Generating new OpenAPI service..."
48+
@go run github.com/mockzilla/connexions/v2/cmd/gen/service -type openapi -name=$(name) -output=pkg/$(name)
49+
50+
.PHONY: discover
51+
discover:
52+
@echo "Discovering services to generate service imports..."
53+
@go run github.com/mockzilla/connexions/v2/cmd/gen/discover pkg
54+
55+
#.PHONY: generate
56+
generate:
57+
@go generate ./...

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Connexions Codegen Template
2+
3+
Generate a Go mock server from OpenAPI specs with [Connexions](https://github.com/mockzilla/connexions).
4+
5+
Each service is a Go package with generated handlers, embedded OpenAPI specs, and optional custom logic.
6+
Includes an API Explorer UI at `/api-explorer`.
7+
8+
## Quick start
9+
10+
1. Click [**Use this template**](https://github.com/mockzilla/connexions-codegen-template/generate) to create your own repository
11+
2. Add services (see below)
12+
3. Regenerate and discover:
13+
```bash
14+
make generate && make discover
15+
```
16+
4. Push to main - binaries for Linux, macOS, and Windows are built automatically and published to **Releases**
17+
18+
## Adding services
19+
20+
### From an OpenAPI spec
21+
22+
```bash
23+
make service name=my-api
24+
```
25+
26+
This creates `pkg/my_api/` with a scaffold. Replace `pkg/my_api/setup/openapi.yml` with your spec, then run:
27+
28+
```bash
29+
make generate && make discover
30+
```
31+
32+
### From static responses
33+
34+
```bash
35+
make service-from-static name=my-api
36+
```
37+
38+
Add response files under `pkg/my_api/setup/data/` organized by method and path:
39+
40+
```
41+
pkg/my_api/setup/data/
42+
get/
43+
users/
44+
index.json -> GET /users
45+
users/{id}/
46+
index.json -> GET /users/{id}
47+
post/
48+
users/
49+
index.json -> POST /users
50+
```
51+
52+
Then regenerate:
53+
54+
```bash
55+
make generate && make discover
56+
```
57+
58+
## Service structure
59+
60+
Each service lives in `pkg/{service_name}/` with:
61+
62+
```
63+
pkg/petstore/
64+
setup/
65+
openapi.yml # OpenAPI specification
66+
config.yml # Latency, errors, upstream, caching
67+
codegen.yml # Code generation settings
68+
context.yml # Custom values for mock data
69+
generate.go # go:generate directive
70+
gen.go # Generated handler (do not edit)
71+
service.go # Custom logic (optional overrides)
72+
```
73+
74+
## API Explorer UI
75+
76+
The built-in UI is available at `/` (configurable in `resources/data/app.yml`).
77+
It lets you browse services, view specs, test endpoints, and inspect request/response history.
78+
79+
## Local development
80+
81+
```bash
82+
make build
83+
.build/server/server
84+
```
85+
86+
See the [Connexions docs](https://github.com/mockzilla/connexions) for more options.

0 commit comments

Comments
 (0)