create-svc 0.1.9 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -11
- package/package.json +9 -4
- package/src/cli.test.ts +29 -8
- package/src/cli.ts +103 -70
- package/src/naming.test.ts +4 -2
- package/src/naming.ts +9 -1
- package/src/neon.ts +10 -8
- package/src/post-scaffold.ts +7 -28
- package/src/profiles.ts +28 -0
- package/src/scaffold.test.ts +126 -15
- package/src/scaffold.ts +94 -23
- package/src/vault.test.ts +33 -9
- package/src/vault.ts +4 -3
- package/templates/shared/README.md +135 -24
- package/templates/shared/docker-compose.yml +19 -0
- package/templates/shared/scripts/cloudrun/bootstrap.ts +15 -42
- package/templates/shared/scripts/cloudrun/cleanup.ts +17 -31
- package/templates/shared/scripts/cloudrun/config.ts +14 -19
- package/templates/shared/scripts/cloudrun/deploy.ts +19 -10
- package/templates/shared/scripts/cloudrun/integrations.ts +111 -0
- package/templates/shared/scripts/cloudrun/lib.ts +88 -112
- package/templates/shared/scripts/cloudrun/neon.ts +82 -13
- package/templates/shared/service.yaml +44 -1
- package/templates/variants/bun-connectrpc/Dockerfile +1 -0
- package/templates/variants/bun-connectrpc/Makefile +4 -1
- package/templates/variants/bun-connectrpc/gen/protos/chat/v1/chat_pb.ts +1078 -0
- package/templates/variants/bun-connectrpc/migrations/0000_init.sql +63 -0
- package/templates/variants/bun-connectrpc/package.json +17 -0
- package/templates/variants/bun-connectrpc/protos/chat/v1/chat.proto +228 -0
- package/templates/variants/bun-connectrpc/scripts/codegen.ts +31 -1
- package/templates/variants/bun-connectrpc/scripts/migrate.ts +46 -0
- package/templates/variants/bun-connectrpc/src/chat/service.ts +384 -0
- package/templates/variants/bun-connectrpc/src/chat/types.ts +142 -0
- package/templates/variants/bun-connectrpc/src/db/client.ts +15 -0
- package/templates/variants/bun-connectrpc/src/db/repository.ts +479 -0
- package/templates/variants/bun-connectrpc/src/db/schema.ts +75 -0
- package/templates/variants/bun-connectrpc/src/index.ts +294 -22
- package/templates/variants/bun-connectrpc/src/storage.ts +72 -0
- package/templates/variants/bun-connectrpc/src/webhooks.ts +35 -0
- package/templates/variants/bun-connectrpc/test/app.test.ts +14 -13
- package/templates/variants/bun-connectrpc/test/list-messages.integration.test.ts +182 -0
- package/templates/variants/bun-connectrpc/tsconfig.json +2 -1
- package/templates/variants/bun-hono/Makefile +4 -1
- package/templates/variants/bun-hono/migrations/0000_init.sql +63 -0
- package/templates/variants/bun-hono/package.json +13 -0
- package/templates/variants/bun-hono/scripts/migrate.ts +46 -0
- package/templates/variants/bun-hono/src/chat/service.ts +384 -0
- package/templates/variants/bun-hono/src/chat/types.ts +142 -0
- package/templates/variants/bun-hono/src/db/client.ts +15 -0
- package/templates/variants/bun-hono/src/db/repository.ts +479 -0
- package/templates/variants/bun-hono/src/db/schema.ts +75 -0
- package/templates/variants/bun-hono/src/index.ts +254 -8
- package/templates/variants/bun-hono/src/storage.ts +72 -0
- package/templates/variants/bun-hono/src/webhooks.ts +35 -0
- package/templates/variants/bun-hono/test/app.test.ts +60 -6
- package/templates/variants/bun-hono/test/list-messages.integration.test.ts +256 -0
- package/templates/variants/bun-hono/tsconfig.json +1 -0
- package/templates/variants/go-chi/Makefile +6 -2
- package/templates/variants/go-chi/buf.gen.yaml +2 -0
- package/templates/variants/go-chi/cmd/migrate/main.go +101 -0
- package/templates/variants/go-chi/cmd/server/main.go +16 -15
- package/templates/variants/go-chi/go.mod +3 -0
- package/templates/variants/go-chi/internal/app/service.go +763 -71
- package/templates/variants/go-chi/internal/config/config.go +22 -7
- package/templates/variants/go-chi/internal/httpapi/list_messages_integration_test.go +298 -0
- package/templates/variants/go-chi/internal/httpapi/routes.go +245 -43
- package/templates/variants/go-chi/migrations/0000_init.sql +63 -0
- package/templates/variants/go-chi/protos/chat/v1/chat.proto +219 -0
- package/templates/variants/go-chi/test/go.test.ts +4 -1
- package/templates/variants/go-connectrpc/Makefile +6 -2
- package/templates/variants/go-connectrpc/buf.gen.yaml +2 -0
- package/templates/variants/go-connectrpc/cmd/migrate/main.go +101 -0
- package/templates/variants/go-connectrpc/cmd/server/main.go +35 -11
- package/templates/variants/go-connectrpc/gen/chat/v1/chat.pb.go +2512 -0
- package/templates/variants/go-connectrpc/gen/chat/v1/chatv1connect/chat.connect.go +571 -0
- package/templates/variants/go-connectrpc/go.mod +4 -0
- package/templates/variants/go-connectrpc/internal/app/service.go +763 -71
- package/templates/variants/go-connectrpc/internal/config/config.go +22 -7
- package/templates/variants/go-connectrpc/internal/connectapi/handler.go +254 -42
- package/templates/variants/go-connectrpc/internal/connectapi/list_messages_integration_test.go +216 -0
- package/templates/variants/go-connectrpc/internal/httpapi/routes.go +41 -56
- package/templates/variants/go-connectrpc/migrations/0000_init.sql +63 -0
- package/templates/variants/go-connectrpc/protos/chat/v1/chat.proto +232 -0
- package/templates/shared/.env.example +0 -10
- package/templates/variants/go-chi/gen/dns/v1/dns.pb.go +0 -623
- package/templates/variants/go-chi/gen/dns/v1/dnsv1connect/dns.connect.go +0 -192
- package/templates/variants/go-chi/internal/connectapi/handler.go +0 -79
- package/templates/variants/go-chi/protos/dns/v1/dns.proto +0 -58
- package/templates/variants/go-connectrpc/gen/dns/v1/dns.pb.go +0 -623
- package/templates/variants/go-connectrpc/gen/dns/v1/dnsv1connect/dns.connect.go +0 -192
- package/templates/variants/go-connectrpc/protos/dns/v1/dns.proto +0 -58
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
create table if not exists users (
|
|
2
|
+
id text primary key,
|
|
3
|
+
username text not null unique,
|
|
4
|
+
display_name text,
|
|
5
|
+
created_at timestamptz not null default now(),
|
|
6
|
+
updated_at timestamptz not null default now()
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
create table if not exists conversations (
|
|
10
|
+
id text primary key,
|
|
11
|
+
title text,
|
|
12
|
+
created_by_user_id text not null references users(id),
|
|
13
|
+
deleted_at timestamptz,
|
|
14
|
+
created_at timestamptz not null default now(),
|
|
15
|
+
updated_at timestamptz not null default now()
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
create table if not exists conversation_participants (
|
|
19
|
+
conversation_id text not null references conversations(id),
|
|
20
|
+
user_id text not null references users(id),
|
|
21
|
+
joined_at timestamptz not null default now(),
|
|
22
|
+
primary key (conversation_id, user_id)
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
create table if not exists messages (
|
|
26
|
+
id text primary key,
|
|
27
|
+
conversation_id text not null references conversations(id),
|
|
28
|
+
user_id text not null references users(id),
|
|
29
|
+
body text not null,
|
|
30
|
+
edited_at timestamptz,
|
|
31
|
+
deleted_at timestamptz,
|
|
32
|
+
created_at timestamptz not null default now(),
|
|
33
|
+
updated_at timestamptz not null default now()
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
create table if not exists attachments (
|
|
37
|
+
id text primary key,
|
|
38
|
+
conversation_id text not null references conversations(id),
|
|
39
|
+
message_id text references messages(id),
|
|
40
|
+
uploaded_by_user_id text not null references users(id),
|
|
41
|
+
storage_bucket text not null,
|
|
42
|
+
storage_key text not null,
|
|
43
|
+
content_type text not null,
|
|
44
|
+
byte_size bigint not null,
|
|
45
|
+
filename text not null,
|
|
46
|
+
status text not null,
|
|
47
|
+
deleted_at timestamptz,
|
|
48
|
+
created_at timestamptz not null default now(),
|
|
49
|
+
updated_at timestamptz not null default now()
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
create table if not exists webhook_events (
|
|
53
|
+
id text primary key,
|
|
54
|
+
provider text not null,
|
|
55
|
+
external_event_id text not null,
|
|
56
|
+
event_type text not null,
|
|
57
|
+
signature_valid text not null,
|
|
58
|
+
status text not null,
|
|
59
|
+
payload_json text not null,
|
|
60
|
+
received_at timestamptz not null default now(),
|
|
61
|
+
processed_at timestamptz,
|
|
62
|
+
unique (provider, external_event_id)
|
|
63
|
+
);
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
package chat.v1;
|
|
4
|
+
|
|
5
|
+
option go_package = "{{MODULE_PATH}}/gen/chat/v1;chatv1";
|
|
6
|
+
|
|
7
|
+
service ChatService {
|
|
8
|
+
rpc CreateUser(CreateUserRequest) returns (CreateUserResponse);
|
|
9
|
+
rpc GetUser(GetUserRequest) returns (GetUserResponse);
|
|
10
|
+
rpc GetUserByUsername(GetUserByUsernameRequest) returns (GetUserByUsernameResponse);
|
|
11
|
+
rpc CreateConversation(CreateConversationRequest) returns (CreateConversationResponse);
|
|
12
|
+
rpc GetConversation(GetConversationRequest) returns (GetConversationResponse);
|
|
13
|
+
rpc UpdateConversation(UpdateConversationRequest) returns (UpdateConversationResponse);
|
|
14
|
+
rpc DeleteConversation(DeleteConversationRequest) returns (DeleteConversationResponse);
|
|
15
|
+
rpc AddConversationParticipant(AddConversationParticipantRequest) returns (AddConversationParticipantResponse);
|
|
16
|
+
rpc RemoveConversationParticipant(RemoveConversationParticipantRequest) returns (RemoveConversationParticipantResponse);
|
|
17
|
+
rpc ListMessages(ListMessagesRequest) returns (ListMessagesResponse);
|
|
18
|
+
rpc CreateMessage(CreateMessageRequest) returns (CreateMessageResponse);
|
|
19
|
+
rpc UpdateMessage(UpdateMessageRequest) returns (UpdateMessageResponse);
|
|
20
|
+
rpc DeleteMessage(DeleteMessageRequest) returns (DeleteMessageResponse);
|
|
21
|
+
rpc CreateAttachmentUpload(CreateAttachmentUploadRequest) returns (CreateAttachmentUploadResponse);
|
|
22
|
+
rpc FinalizeAttachment(FinalizeAttachmentRequest) returns (FinalizeAttachmentResponse);
|
|
23
|
+
rpc GetAttachment(GetAttachmentRequest) returns (GetAttachmentResponse);
|
|
24
|
+
rpc DeleteAttachment(DeleteAttachmentRequest) returns (DeleteAttachmentResponse);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
message User {
|
|
28
|
+
string id = 1;
|
|
29
|
+
string username = 2;
|
|
30
|
+
string display_name = 3;
|
|
31
|
+
string created_at = 4;
|
|
32
|
+
string updated_at = 5;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
message Conversation {
|
|
36
|
+
string id = 1;
|
|
37
|
+
string title = 2;
|
|
38
|
+
string created_by_user_id = 3;
|
|
39
|
+
repeated User participants = 4;
|
|
40
|
+
string created_at = 5;
|
|
41
|
+
string updated_at = 6;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
message Message {
|
|
45
|
+
string id = 1;
|
|
46
|
+
string conversation_id = 2;
|
|
47
|
+
string user_id = 3;
|
|
48
|
+
string body = 4;
|
|
49
|
+
string edited_at = 5;
|
|
50
|
+
string created_at = 6;
|
|
51
|
+
string updated_at = 7;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
message Attachment {
|
|
55
|
+
string id = 1;
|
|
56
|
+
string conversation_id = 2;
|
|
57
|
+
string message_id = 3;
|
|
58
|
+
string uploaded_by_user_id = 4;
|
|
59
|
+
string storage_bucket = 5;
|
|
60
|
+
string storage_key = 6;
|
|
61
|
+
string content_type = 7;
|
|
62
|
+
int64 byte_size = 8;
|
|
63
|
+
string filename = 9;
|
|
64
|
+
string status = 10;
|
|
65
|
+
string public_url = 11;
|
|
66
|
+
string created_at = 12;
|
|
67
|
+
string updated_at = 13;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
message UploadTarget {
|
|
71
|
+
string method = 1;
|
|
72
|
+
string url = 2;
|
|
73
|
+
map<string, string> headers = 3;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
message CreateUserRequest {
|
|
77
|
+
string username = 1;
|
|
78
|
+
string display_name = 2;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
message CreateUserResponse {
|
|
82
|
+
User user = 1;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
message GetUserRequest {
|
|
86
|
+
string user_id = 1;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
message GetUserResponse {
|
|
90
|
+
User user = 1;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
message GetUserByUsernameRequest {
|
|
94
|
+
string username = 1;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
message GetUserByUsernameResponse {
|
|
98
|
+
User user = 1;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
message CreateConversationRequest {
|
|
102
|
+
string created_by_user_id = 1;
|
|
103
|
+
string title = 2;
|
|
104
|
+
repeated string participant_user_ids = 3;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
message CreateConversationResponse {
|
|
108
|
+
Conversation conversation = 1;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
message GetConversationRequest {
|
|
112
|
+
string conversation_id = 1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
message GetConversationResponse {
|
|
116
|
+
Conversation conversation = 1;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
message UpdateConversationRequest {
|
|
120
|
+
string conversation_id = 1;
|
|
121
|
+
string title = 2;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
message UpdateConversationResponse {
|
|
125
|
+
Conversation conversation = 1;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
message DeleteConversationRequest {
|
|
129
|
+
string conversation_id = 1;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
message DeleteConversationResponse {}
|
|
133
|
+
|
|
134
|
+
message AddConversationParticipantRequest {
|
|
135
|
+
string conversation_id = 1;
|
|
136
|
+
string user_id = 2;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
message AddConversationParticipantResponse {
|
|
140
|
+
Conversation conversation = 1;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
message RemoveConversationParticipantRequest {
|
|
144
|
+
string conversation_id = 1;
|
|
145
|
+
string user_id = 2;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
message RemoveConversationParticipantResponse {}
|
|
149
|
+
|
|
150
|
+
message ListMessagesRequest {
|
|
151
|
+
string conversation_id = 1;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
message ListMessagesResponse {
|
|
155
|
+
repeated Message messages = 1;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
message CreateMessageRequest {
|
|
159
|
+
string conversation_id = 1;
|
|
160
|
+
string user_id = 2;
|
|
161
|
+
string body = 3;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
message CreateMessageResponse {
|
|
165
|
+
Message message = 1;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
message UpdateMessageRequest {
|
|
169
|
+
string conversation_id = 1;
|
|
170
|
+
string message_id = 2;
|
|
171
|
+
string body = 3;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
message UpdateMessageResponse {
|
|
175
|
+
Message message = 1;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
message DeleteMessageRequest {
|
|
179
|
+
string conversation_id = 1;
|
|
180
|
+
string message_id = 2;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
message DeleteMessageResponse {}
|
|
184
|
+
|
|
185
|
+
message CreateAttachmentUploadRequest {
|
|
186
|
+
string conversation_id = 1;
|
|
187
|
+
string user_id = 2;
|
|
188
|
+
string filename = 3;
|
|
189
|
+
string content_type = 4;
|
|
190
|
+
int64 byte_size = 5;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
message CreateAttachmentUploadResponse {
|
|
194
|
+
Attachment attachment = 1;
|
|
195
|
+
UploadTarget upload = 2;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
message FinalizeAttachmentRequest {
|
|
199
|
+
string attachment_id = 1;
|
|
200
|
+
string message_id = 2;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
message FinalizeAttachmentResponse {
|
|
204
|
+
Attachment attachment = 1;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
message GetAttachmentRequest {
|
|
208
|
+
string attachment_id = 1;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
message GetAttachmentResponse {
|
|
212
|
+
Attachment attachment = 1;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
message DeleteAttachmentRequest {
|
|
216
|
+
string attachment_id = 1;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
message DeleteAttachmentResponse {}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { expect, test } from "bun:test";
|
|
2
2
|
|
|
3
3
|
test("go test ./...", { timeout: 60_000 }, () => {
|
|
4
|
-
const
|
|
4
|
+
const go = Bun.which("go");
|
|
5
|
+
expect(go, "go must be installed and available on PATH").toBeString();
|
|
6
|
+
|
|
7
|
+
const result = Bun.spawnSync([go, "test", "./..."], {
|
|
5
8
|
cwd: process.cwd(),
|
|
6
9
|
stdout: "pipe",
|
|
7
10
|
stderr: "pipe",
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
.PHONY: dev gen lint test bootstrap deploy cleanup
|
|
1
|
+
.PHONY: dev migrate gen lint test bootstrap deploy cleanup
|
|
2
2
|
|
|
3
3
|
CLOUDRUN := npx --no-install svc-cloudrun
|
|
4
|
+
WITH_ENV := set -a; [ ! -f .env.local ] || . ./.env.local; set +a;
|
|
4
5
|
|
|
5
6
|
dev:
|
|
6
|
-
go run ./cmd/server
|
|
7
|
+
@$(WITH_ENV) go run ./cmd/server
|
|
8
|
+
|
|
9
|
+
migrate:
|
|
10
|
+
@$(WITH_ENV) go run ./cmd/migrate
|
|
7
11
|
|
|
8
12
|
gen:
|
|
9
13
|
buf generate
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"log"
|
|
6
|
+
"os"
|
|
7
|
+
"sort"
|
|
8
|
+
"strings"
|
|
9
|
+
"time"
|
|
10
|
+
|
|
11
|
+
_ "github.com/jackc/pgx/v5/stdlib"
|
|
12
|
+
"github.com/jmoiron/sqlx"
|
|
13
|
+
|
|
14
|
+
"{{MODULE_PATH}}/internal/app"
|
|
15
|
+
"{{MODULE_PATH}}/internal/config"
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
func main() {
|
|
19
|
+
cfg, err := config.Load()
|
|
20
|
+
if err != nil {
|
|
21
|
+
log.Fatal(err)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
db, err := app.OpenDatabase(context.Background(), cfg.DatabaseURL)
|
|
25
|
+
if err != nil {
|
|
26
|
+
db, err = waitForDatabase(cfg.DatabaseURL, 30*time.Second)
|
|
27
|
+
if err != nil {
|
|
28
|
+
log.Fatal(err)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if _, err := db.ExecContext(context.Background(), `
|
|
33
|
+
create table if not exists schema_migrations (
|
|
34
|
+
version text primary key,
|
|
35
|
+
applied_at timestamptz not null default now()
|
|
36
|
+
)`); err != nil {
|
|
37
|
+
log.Fatal(err)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
entries, err := os.ReadDir("migrations")
|
|
41
|
+
if err != nil {
|
|
42
|
+
log.Fatal(err)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
versions := make([]string, 0, len(entries))
|
|
46
|
+
for _, entry := range entries {
|
|
47
|
+
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
|
|
48
|
+
continue
|
|
49
|
+
}
|
|
50
|
+
versions = append(versions, entry.Name())
|
|
51
|
+
}
|
|
52
|
+
sort.Strings(versions)
|
|
53
|
+
|
|
54
|
+
for _, version := range versions {
|
|
55
|
+
var count int
|
|
56
|
+
if err := db.GetContext(context.Background(), &count, `select count(*) from schema_migrations where version = $1`, version); err != nil {
|
|
57
|
+
log.Fatal(err)
|
|
58
|
+
}
|
|
59
|
+
if count > 0 {
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
sqlBytes, err := os.ReadFile("migrations/" + version)
|
|
64
|
+
if err != nil {
|
|
65
|
+
log.Fatal(err)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
tx, err := db.BeginTxx(context.Background(), nil)
|
|
69
|
+
if err != nil {
|
|
70
|
+
log.Fatal(err)
|
|
71
|
+
}
|
|
72
|
+
if _, err := tx.ExecContext(context.Background(), string(sqlBytes)); err != nil {
|
|
73
|
+
_ = tx.Rollback()
|
|
74
|
+
log.Fatal(err)
|
|
75
|
+
}
|
|
76
|
+
if _, err := tx.ExecContext(context.Background(), `insert into schema_migrations (version) values ($1)`, version); err != nil {
|
|
77
|
+
_ = tx.Rollback()
|
|
78
|
+
log.Fatal(err)
|
|
79
|
+
}
|
|
80
|
+
if err := tx.Commit(); err != nil {
|
|
81
|
+
log.Fatal(err)
|
|
82
|
+
}
|
|
83
|
+
log.Printf("applied migration %s", version)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
func waitForDatabase(databaseURL string, timeout time.Duration) (*sqlx.DB, error) {
|
|
88
|
+
deadline := time.Now().Add(timeout)
|
|
89
|
+
var lastErr error
|
|
90
|
+
|
|
91
|
+
for time.Now().Before(deadline) {
|
|
92
|
+
db, err := app.OpenDatabase(context.Background(), databaseURL)
|
|
93
|
+
if err == nil {
|
|
94
|
+
return db, nil
|
|
95
|
+
}
|
|
96
|
+
lastErr = err
|
|
97
|
+
time.Sleep(time.Second)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return nil, lastErr
|
|
101
|
+
}
|
|
@@ -4,12 +4,18 @@ import (
|
|
|
4
4
|
"context"
|
|
5
5
|
"log"
|
|
6
6
|
"net/http"
|
|
7
|
+
"os"
|
|
8
|
+
"strings"
|
|
7
9
|
"time"
|
|
8
10
|
|
|
11
|
+
"cloud.google.com/go/storage"
|
|
12
|
+
"connectrpc.com/grpcreflect"
|
|
9
13
|
"github.com/go-chi/chi/v5"
|
|
14
|
+
_ "github.com/jackc/pgx/v5/stdlib"
|
|
10
15
|
"golang.org/x/net/http2"
|
|
11
16
|
"golang.org/x/net/http2/h2c"
|
|
12
17
|
|
|
18
|
+
chatv1connect "{{MODULE_PATH}}/gen/chat/v1/chatv1connect"
|
|
13
19
|
"{{MODULE_PATH}}/internal/app"
|
|
14
20
|
"{{MODULE_PATH}}/internal/config"
|
|
15
21
|
"{{MODULE_PATH}}/internal/connectapi"
|
|
@@ -22,24 +28,34 @@ func main() {
|
|
|
22
28
|
log.Fatal(err)
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
|
|
26
|
-
if
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
Proxied: false,
|
|
33
|
-
}); err != nil {
|
|
34
|
-
log.Fatal(err)
|
|
35
|
-
}
|
|
31
|
+
db, err := app.OpenDatabase(context.Background(), cfg.DatabaseURL)
|
|
32
|
+
if err != nil {
|
|
33
|
+
log.Fatal(err)
|
|
34
|
+
}
|
|
35
|
+
storageClient, err := storage.NewClient(context.Background())
|
|
36
|
+
if err != nil {
|
|
37
|
+
log.Fatal(err)
|
|
36
38
|
}
|
|
37
39
|
|
|
40
|
+
service := app.NewChatService(
|
|
41
|
+
db,
|
|
42
|
+
app.NewGCSStorage(cfg.AttachmentBucket, cfg.AttachmentPublicBaseURL, storageClient),
|
|
43
|
+
app.GenericWebhookAdapter{},
|
|
44
|
+
)
|
|
45
|
+
|
|
38
46
|
router := chi.NewRouter()
|
|
39
47
|
connectPath, connectHandler := connectapi.NewHandler(service)
|
|
40
48
|
router.Mount(connectPath, connectHandler)
|
|
41
49
|
httpapi.RegisterRoutes(router, service)
|
|
42
50
|
|
|
51
|
+
if localRPCIntrospectionEnabled() {
|
|
52
|
+
reflector := grpcreflect.NewStaticReflector(chatv1connect.ChatServiceName)
|
|
53
|
+
reflectionV1Path, reflectionV1Handler := grpcreflect.NewHandlerV1(reflector)
|
|
54
|
+
reflectionV1AlphaPath, reflectionV1AlphaHandler := grpcreflect.NewHandlerV1Alpha(reflector)
|
|
55
|
+
router.Mount(reflectionV1Path, reflectionV1Handler)
|
|
56
|
+
router.Mount(reflectionV1AlphaPath, reflectionV1AlphaHandler)
|
|
57
|
+
}
|
|
58
|
+
|
|
43
59
|
server := &http.Server{
|
|
44
60
|
Addr: ":" + cfg.Port,
|
|
45
61
|
ReadHeaderTimeout: 10 * time.Second,
|
|
@@ -49,3 +65,11 @@ func main() {
|
|
|
49
65
|
log.Printf("listening on %s", server.Addr)
|
|
50
66
|
log.Fatal(server.ListenAndServe())
|
|
51
67
|
}
|
|
68
|
+
|
|
69
|
+
func localRPCIntrospectionEnabled() bool {
|
|
70
|
+
override := strings.TrimSpace(strings.ToLower(os.Getenv("ENABLE_RPC_INTROSPECTION")))
|
|
71
|
+
if override != "" {
|
|
72
|
+
return override != "0" && override != "false" && override != "no" && override != "off"
|
|
73
|
+
}
|
|
74
|
+
return os.Getenv("K_SERVICE") == ""
|
|
75
|
+
}
|