Skip to content

Commit 2f9e066

Browse files
authored
add golang full example (#48)
1 parent f9f0b47 commit 2f9e066

File tree

7 files changed

+524
-0
lines changed

7 files changed

+524
-0
lines changed

examples/golang/BUILD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@bazel_gazelle//:def.bzl", "gazelle")
2+
3+
# gazelle:prefix examples/golang
4+
gazelle(name = "gazelle")
5+
6+
gazelle(
7+
name = "gazelle-update-repos",
8+
args = [
9+
"-from_file=go.mod",
10+
"-to_macro=workspace.bzl%go_dependencies",
11+
"-prune",
12+
],
13+
command = "update-repos",
14+
)

examples/golang/WORKSPACE

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
local_repository(
2+
name = "openapi_tools_generator_bazel",
3+
path = "../..",
4+
)
5+
6+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
7+
8+
######################
9+
# Golang
10+
######################
11+
# https://github.com/bazelbuild/rules_go/releases
12+
http_archive(
13+
name = "io_bazel_rules_go",
14+
sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996",
15+
urls = ["https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.0.zip"],
16+
)
17+
18+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
19+
20+
go_rules_dependencies()
21+
22+
go_register_toolchains(version = "1.20.3")
23+
24+
# Gazelle: https://github.com/bazelbuild/bazel-gazelle/releases
25+
http_archive(
26+
name = "bazel_gazelle",
27+
sha256 = "727f3e4edd96ea20c29e8c2ca9e8d2af724d8c7778e7923a854b2c80952bc405",
28+
urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz"],
29+
)
30+
31+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
32+
load("//:workspace.bzl", "go_dependencies")
33+
34+
# gazelle:repository_macro workspace.bzl%go_dependencies
35+
go_dependencies()
36+
37+
gazelle_dependencies()
38+
39+
######################
40+
# Aspect's Bazel
41+
######################
42+
# https://github.com/aspect-build/bazel-lib/releases
43+
http_archive(
44+
name = "aspect_bazel_lib",
45+
sha256 = "5f3443b1d98a462a8b7330f4742483afc8b2d17c8555dd97ce4146f43e961718",
46+
strip_prefix = "bazel-lib-1.31.1",
47+
url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.31.1/bazel-lib-v1.31.1.tar.gz",
48+
)
49+
50+
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
51+
52+
aspect_bazel_lib_dependencies()
53+
54+
#######################
55+
# OpenAPI Generator
56+
#######################
57+
load("@openapi_tools_generator_bazel//:defs.bzl", "openapi_tools_generator_bazel_repositories")
58+
59+
openapi_tools_generator_bazel_repositories()

examples/golang/go.mod

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module examples/golang
2+
3+
go 1.20
4+
5+
require github.com/gin-gonic/gin v1.9.0
6+
7+
require (
8+
github.com/bytedance/sonic v1.8.0 // indirect
9+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
10+
github.com/gin-contrib/sse v0.1.0 // indirect
11+
github.com/gin-gonic/gin v1.9.0 // indirect
12+
github.com/go-playground/locales v0.14.1 // indirect
13+
github.com/go-playground/universal-translator v0.18.1 // indirect
14+
github.com/go-playground/validator/v10 v10.11.2 // indirect
15+
github.com/goccy/go-json v0.10.0 // indirect
16+
github.com/json-iterator/go v1.1.12 // indirect
17+
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
18+
github.com/leodido/go-urn v1.2.1 // indirect
19+
github.com/mattn/go-isatty v0.0.17 // indirect
20+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
21+
github.com/modern-go/reflect2 v1.0.2 // indirect
22+
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
23+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
24+
github.com/ugorji/go/codec v1.2.9 // indirect
25+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
26+
golang.org/x/crypto v0.5.0 // indirect
27+
golang.org/x/net v0.7.0 // indirect
28+
golang.org/x/sys v0.5.0 // indirect
29+
golang.org/x/text v0.7.0 // indirect
30+
google.golang.org/protobuf v1.28.1 // indirect
31+
gopkg.in/yaml.v2 v2.4.0 // indirect
32+
gopkg.in/yaml.v3 v3.0.1 // indirect
33+
)

examples/golang/src/BUILD

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
load("@openapi_tools_generator_bazel//:defs.bzl", "openapi_generator")
2+
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
3+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
4+
5+
openapi_generator(
6+
name = "petstore",
7+
generator = "go-gin-server",
8+
spec = "petstore.yml",
9+
)
10+
11+
copy_to_directory(
12+
name = "petstore.go",
13+
srcs = [":petstore"],
14+
exclude_srcs_patterns = ["**/main.go"],
15+
include_srcs_patterns = ["**/*.go"],
16+
)
17+
18+
go_library(
19+
name = "petstore_lib",
20+
srcs = [":petstore.go"],
21+
importpath = "petstore",
22+
visibility = ["//visibility:private"],
23+
deps = ["@com_github_gin_gonic_gin//:gin"],
24+
)
25+
26+
go_library(
27+
name = "server_lib",
28+
srcs = ["main.go"],
29+
importpath = "examples/golang",
30+
visibility = ["//visibility:private"],
31+
deps = [":petstore_lib"],
32+
)
33+
34+
go_binary(
35+
name = "server",
36+
embed = [":server_lib"],
37+
visibility = ["//visibility:public"],
38+
)

examples/golang/src/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"log"
5+
sw "petstore"
6+
)
7+
8+
func main() {
9+
log.Printf("Server started")
10+
router := sw.NewRouter()
11+
log.Fatal(router.Run(":8080"))
12+
}

examples/golang/src/petstore.yml

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
maximum: 100
24+
format: int32
25+
responses:
26+
'200':
27+
description: A paged array of pets
28+
headers:
29+
x-next:
30+
description: A link to the next page of responses
31+
schema:
32+
type: string
33+
content:
34+
application/json:
35+
schema:
36+
$ref: "#/components/schemas/Pets"
37+
default:
38+
description: unexpected error
39+
content:
40+
application/json:
41+
schema:
42+
$ref: "#/components/schemas/Error"
43+
post:
44+
summary: Create a pet
45+
operationId: createPets
46+
tags:
47+
- pets
48+
responses:
49+
'201':
50+
description: Null response
51+
default:
52+
description: unexpected error
53+
content:
54+
application/json:
55+
schema:
56+
$ref: "#/components/schemas/Error"
57+
/pets/{petId}:
58+
get:
59+
summary: Info for a specific pet
60+
operationId: showPetById
61+
tags:
62+
- pets
63+
parameters:
64+
- name: petId
65+
in: path
66+
required: true
67+
description: The id of the pet to retrieve
68+
schema:
69+
type: string
70+
responses:
71+
'200':
72+
description: Expected response to a valid request
73+
content:
74+
application/json:
75+
schema:
76+
$ref: "#/components/schemas/Pet"
77+
default:
78+
description: unexpected error
79+
content:
80+
application/json:
81+
schema:
82+
$ref: "#/components/schemas/Error"
83+
components:
84+
schemas:
85+
Pet:
86+
type: object
87+
required:
88+
- id
89+
- name
90+
properties:
91+
id:
92+
type: integer
93+
format: int64
94+
name:
95+
type: string
96+
tag:
97+
type: string
98+
Pets:
99+
type: array
100+
maxItems: 100
101+
items:
102+
$ref: "#/components/schemas/Pet"
103+
Error:
104+
type: object
105+
required:
106+
- code
107+
- message
108+
properties:
109+
code:
110+
type: integer
111+
format: int32
112+
message:
113+
type: string

0 commit comments

Comments
 (0)