Skip to content

Commit 7ef3c01

Browse files
feat: http server type
1 parent 55965c0 commit 7ef3c01

File tree

4 files changed

+65
-40
lines changed

4 files changed

+65
-40
lines changed

README.md

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# sqlc-gen-go-server
22

3-
[Sqlc plugin](https://sqlc.dev) to generate [gRPC](https://grpc.io/) or [Connect](https://connectrpc.com/) server from SQL.
3+
[Sqlc plugin](https://sqlc.dev) to generate [gRPC](https://grpc.io/), [Connect](https://connectrpc.com/) or [HTTP](https://pkg.go.dev/net/http) server from SQL.
44

55
## Usage
66

@@ -24,7 +24,58 @@ sql:
2424
server_type: grpc
2525
```
2626
27-
## Post-process
27+
All [plugins options](https://github.com/walterwanderley/sqlc-gen-go-server?tab=readme-ov-file#plugin-options).
28+
29+
### Customizing HTTP endpoints
30+
31+
You can customize the HTTP endpoints (server_type: http or grpc) by adding comments to the queries.
32+
33+
```sql
34+
-- http: Method Path
35+
```
36+
37+
Here’s an example of a queries file that has custom HTTP endpoints:
38+
```sql
39+
-- name: ListAuthors :many
40+
-- http: GET /authors
41+
SELECT * FROM authors
42+
ORDER BY name;
43+
44+
-- name: UpdateAuthorBio :exec
45+
-- http: PATCH /authors/{id}/bio
46+
UPDATE authors
47+
SET bio = $1
48+
WHERE id = $2;
49+
```
50+
51+
## Post-process for server_type: http
52+
53+
>**Note:** If you’d rather not execute these steps, you might want to use [sqlc-http](https://github.com/walterwanderley/sqlc-http) instead of this plugin.
54+
55+
After execute `sqlc generate` you need to organize imports and fix go.mod.
56+
57+
1. Install the required tools:
58+
59+
```sh
60+
go install golang.org/x/tools/cmd/goimports@latest
61+
```
62+
63+
2. Organize imports:
64+
65+
```sh
66+
goimports -w *.go **/*.go **/**/*.go
67+
```
68+
69+
3. Fix go.mod:
70+
71+
```sh
72+
go mod tidy
73+
```
74+
75+
4. If you define more than one package, you’ll need to add them to the generated **registry.go** file.
76+
77+
78+
## Post-process for server_type: grpc or connect
2879

2980
>**Note:** If you’d rather not execute these steps, you might want to use [sqlc-grpc](https://github.com/walterwanderley/sqlc-grpc) or [sqlc-connect](https://github.com/walterwanderley/sqlc-connect) instead of this plugin.
3081
@@ -63,28 +114,6 @@ go mod tidy
63114

64115
5. If you define more than one package, you’ll need to add them to the generated **registry.go** file.
65116

66-
### Customizing HTTP endpoints
67-
68-
You can customize the HTTP endpoints generated by grpc-gateway (server_type: grpc) by adding comments to the queries.
69-
70-
```sql
71-
-- http: Method Path
72-
```
73-
74-
Here’s an example of a queries file that has custom HTTP endpoints:
75-
```sql
76-
-- name: ListAuthors :many
77-
-- http: GET /authors
78-
SELECT * FROM authors
79-
ORDER BY name;
80-
81-
-- name: UpdateAuthorBio :exec
82-
-- http: PATCH /authors/{id}/bio
83-
UPDATE authors
84-
SET bio = $1
85-
WHERE id = $2;
86-
```
87-
88117
## Plugin options
89118

90119
You can use all of the [Golang plugin's options](https://docs.sqlc.dev/en/latest/reference/config.html#go) as well as the options described below.
@@ -104,7 +133,7 @@ sql:
104133
- plugin: go-server
105134
out: "internal/db"
106135
options:
107-
server_type: "grpc" # The server type: grpc or connect.
136+
server_type: "http" # The server type: grpc, connect or http.
108137
module: "my-module" # The module name for the generated go.mod.
109138
metric: false # If true, enable open telemetry metrics.
110139
tracing: false # If true, enable open telemetry distributed tracing.

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ require (
99
github.com/sqlc-dev/plugin-sdk-go v1.23.0
1010
github.com/walterwanderley/sqlc-connect v0.3.1
1111
github.com/walterwanderley/sqlc-grpc v0.19.2
12+
github.com/walterwanderley/sqlc-http v0.0.2
1213
)
1314

1415
require (
1516
github.com/emicklei/proto v1.13.2 // indirect
1617
github.com/gogo/protobuf v1.3.2 // indirect
1718
github.com/golang/protobuf v1.5.3 // indirect
18-
golang.org/x/mod v0.14.0 // indirect
1919
golang.org/x/net v0.20.0 // indirect
2020
golang.org/x/sys v0.16.0 // indirect
2121
golang.org/x/text v0.14.0 // indirect
22-
golang.org/x/tools v0.17.0 // indirect
2322
google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 // indirect
2423
google.golang.org/grpc v1.60.1 // indirect
2524
google.golang.org/protobuf v1.32.0 // indirect
26-
gopkg.in/yaml.v3 v3.0.1 // indirect
2725
)

go.sum

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
1616
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
1717
github.com/sqlc-dev/plugin-sdk-go v1.23.0 h1:iSeJhnXPlbDXlbzUEebw/DxsGzE9rdDJArl8Hvt0RMM=
1818
github.com/sqlc-dev/plugin-sdk-go v1.23.0/go.mod h1:I1r4THOfyETD+LI2gogN2LX8wCjwUZrgy/NU4In3llA=
19-
github.com/walterwanderley/sqlc-connect v0.3.0 h1:RlNcWrp5s+ephiCzPCC/v+B7HA7yvDDeEwodB9yLtWQ=
20-
github.com/walterwanderley/sqlc-connect v0.3.0/go.mod h1:dTMXf4h8Lw9DJVbLTkSbiHAA5o7kZAq5pESPZLvRZ9U=
2119
github.com/walterwanderley/sqlc-connect v0.3.1 h1:dTmRU3lukwFBZqp05i8UCO9tJlUoPn87ZH0KvATkEEc=
2220
github.com/walterwanderley/sqlc-connect v0.3.1/go.mod h1:ElxeTpFoj5xhCLW6lMSy9akuhTW7zpygx8N98bGGj6g=
2321
github.com/walterwanderley/sqlc-grpc v0.19.2 h1:ytEGN1QQ3y/FrCe1CYvqiNnCJhua7c/QJw07hJuaFs8=
2422
github.com/walterwanderley/sqlc-grpc v0.19.2/go.mod h1:bfMjDJUNyUkk3+OM/we8OKDYPVodms+ekOgHvV2hXFY=
23+
github.com/walterwanderley/sqlc-http v0.0.2 h1:N2HIPQoXmuV2Y6P2k+d3tAnTl/J9uid6hQgkld0Roxg=
24+
github.com/walterwanderley/sqlc-http v0.0.2/go.mod h1:NZDNp753Cp6NaGcdJFBCZ3Y28ZqKm8TuYcNbWqBhMtU=
2525
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
2626
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
2727
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2828
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
2929
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
3030
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
3131
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
32-
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
33-
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
3432
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
3533
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
3634
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -53,8 +51,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
5351
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
5452
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
5553
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
56-
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
57-
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
5854
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5955
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6056
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -67,6 +63,3 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
6763
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
6864
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
6965
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
70-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
71-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
72-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/server.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/walterwanderley/sqlc-grpc/converter"
2020
"github.com/walterwanderley/sqlc-grpc/metadata"
2121
grpctemplates "github.com/walterwanderley/sqlc-grpc/templates"
22+
httptemplates "github.com/walterwanderley/sqlc-http/templates"
2223
)
2324

2425
func serverFiles(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) ([]*plugin.File, error) {
@@ -28,14 +29,17 @@ func serverFiles(req *plugin.GenerateRequest, options *opts.Options, enums []Enu
2829
)
2930

3031
switch options.ServerType {
31-
case "", "grpc": // the default server type
32+
case "grpc":
3233
tmplFS = grpctemplates.Files
3334
tmplFuncs = grpctemplates.Funcs
3435
case "connect":
3536
tmplFS = connecttemplates.Files
3637
tmplFuncs = connecttemplates.Funcs
38+
case "", "http": // the default server type
39+
tmplFS = httptemplates.Files
40+
tmplFuncs = httptemplates.Funcs
3741
default:
38-
return nil, fmt.Errorf("invalid server_type %q. Choose 'connect' or 'grpc'", options.ServerType)
42+
return nil, fmt.Errorf("invalid server_type %q. Choose 'connect', 'grpc' or 'http'", options.ServerType)
3943
}
4044
def := toServerDefinition(req, options, enums, structs, queries)
4145
if err := def.Validate(); err != nil {
@@ -77,7 +81,8 @@ func serverFiles(req *plugin.GenerateRequest, options *opts.Options, enums []Enu
7781
return nil
7882
}
7983

80-
if strings.HasSuffix(newPath, "adapters.go") || strings.HasSuffix(newPath, "service.go") || strings.HasSuffix(newPath, "service.factory.go") {
84+
if strings.HasSuffix(newPath, "adapters.go") || strings.HasSuffix(newPath, "service.go") ||
85+
strings.HasSuffix(newPath, "service.factory.go") || strings.HasSuffix(newPath, "routes.go") {
8186
if options.Append && strings.HasSuffix(newPath, "service.factory.go") {
8287
return nil
8388
}

0 commit comments

Comments
 (0)