Skip to content

Commit cdc1027

Browse files
committed
[public-api] hello service server impl
1 parent 2186644 commit cdc1027

File tree

7 files changed

+140
-37
lines changed

7 files changed

+140
-37
lines changed

components/public-api/gitpod/experimental/v1/dummy.proto

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ service HelloService {
1616
returns (stream LotsOfRepliesResponse);
1717
}
1818

19-
message SayHelloRequest { string greeting = 1; }
19+
message SayHelloRequest {}
2020
message SayHelloResponse { string reply = 1; }
2121

22-
message LotsOfRepliesRequest { string greeting = 1; }
23-
message LotsOfRepliesResponse { string reply = 1; }
22+
message LotsOfRepliesRequest {
23+
int32 previous_count = 1;
24+
}
25+
message LotsOfRepliesResponse {
26+
string reply = 1;
27+
int32 count = 2;
28+
}

components/public-api/go/experimental/v1/dummy.pb.go

Lines changed: 45 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/public-api/typescript/src/gitpod/experimental/v1/dummy_pb.ts

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/server/src/api/dummy.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { HandlerContext, ServiceImpl } from "@bufbuild/connect";
8+
import { HelloService } from "@gitpod/public-api/lib/gitpod/experimental/v1/dummy_connectweb";
9+
import {
10+
LotsOfRepliesRequest,
11+
LotsOfRepliesResponse,
12+
SayHelloRequest,
13+
SayHelloResponse,
14+
} from "@gitpod/public-api/lib/gitpod/experimental/v1/dummy_pb";
15+
import { injectable } from "inversify";
16+
17+
/**
18+
* TODO(ak):
19+
* - auth
20+
* - server-side observability
21+
* - client-side observability
22+
* - rate limitting
23+
*/
24+
@injectable()
25+
export class APIHelloService implements ServiceImpl<typeof HelloService> {
26+
async sayHello(req: SayHelloRequest, context: HandlerContext): Promise<SayHelloResponse> {
27+
const response = new SayHelloResponse();
28+
response.reply = "Hello " + this.getSubject();
29+
return response;
30+
}
31+
async *lotsOfReplies(req: LotsOfRepliesRequest, context: HandlerContext): AsyncGenerator<LotsOfRepliesResponse> {
32+
let count = req.previousCount || 0;
33+
while (true) {
34+
const response = new LotsOfRepliesResponse();
35+
response.reply = `Hello ${this.getSubject()} ${count}`;
36+
response.count = count;
37+
yield response;
38+
count++;
39+
await new Promise((resolve) => setTimeout(resolve, 30000));
40+
}
41+
}
42+
43+
private getSubject(): string {
44+
// TODO(ak) get identify from JWT
45+
return "World";
46+
}
47+
}

components/server/src/api/server.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,31 @@
44
* See License.AGPL.txt in the project root for license information.
55
*/
66

7-
import * as http from "http";
8-
import express from "express";
9-
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
10-
import { inject, injectable } from "inversify";
11-
import { APITeamsService } from "./teams";
12-
import { APIUserService } from "./user";
137
import { ConnectRouter } from "@bufbuild/connect";
148
import { expressConnectMiddleware } from "@bufbuild/connect-express";
15-
import { UserService as UserServiceDefinition } from "@gitpod/public-api/lib/gitpod/experimental/v1/user_connectweb";
9+
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
10+
import { HelloService } from "@gitpod/public-api/lib/gitpod/experimental/v1/dummy_connectweb";
11+
import { StatsService } from "@gitpod/public-api/lib/gitpod/experimental/v1/stats_connectweb";
1612
import { TeamsService as TeamsServiceDefinition } from "@gitpod/public-api/lib/gitpod/experimental/v1/teams_connectweb";
13+
import { UserService as UserServiceDefinition } from "@gitpod/public-api/lib/gitpod/experimental/v1/user_connectweb";
1714
import { WorkspacesService as WorkspacesServiceDefinition } from "@gitpod/public-api/lib/gitpod/experimental/v1/workspaces_connectweb";
18-
import { StatsService } from "@gitpod/public-api/lib/gitpod/experimental/v1/stats_connectweb";
15+
import express from "express";
16+
import * as http from "http";
17+
import { inject, injectable } from "inversify";
1918
import { AddressInfo } from "net";
20-
import { APIWorkspacesService } from "./workspaces";
19+
import { APIHelloService } from "./dummy";
2120
import { APIStatsService } from "./stats";
21+
import { APITeamsService } from "./teams";
22+
import { APIUserService } from "./user";
23+
import { APIWorkspacesService } from "./workspaces";
2224

2325
@injectable()
2426
export class API {
2527
@inject(APIUserService) protected readonly apiUserService: APIUserService;
2628
@inject(APITeamsService) protected readonly apiTeamService: APITeamsService;
2729
@inject(APIWorkspacesService) protected readonly apiWorkspacesService: APIWorkspacesService;
2830
@inject(APIStatsService) protected readonly apiStatsService: APIStatsService;
31+
@inject(APIHelloService) private readonly apiHelloService: APIHelloService;
2932

3033
public listen(): http.Server {
3134
const app = express();
@@ -50,4 +53,16 @@ export class API {
5053
}),
5154
);
5255
}
56+
57+
get apiRouter(): express.Router {
58+
const router = express.Router();
59+
router.use(
60+
expressConnectMiddleware({
61+
routes: (router: ConnectRouter) => {
62+
router.service(HelloService, this.apiHelloService);
63+
},
64+
}),
65+
);
66+
return router;
67+
}
5368
}

components/server/src/container-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ import { GitpodTokenService } from "./user/gitpod-token-service";
133133
import { EnvVarService } from "./user/env-var-service";
134134
import { ScmService } from "./projects/scm-service";
135135
import { RelationshipUpdateJob } from "./authorization/relationship-updater-job";
136+
import { APIHelloService } from "./api/dummy";
136137

137138
export const productionContainerModule = new ContainerModule(
138139
(bind, unbind, isBound, rebind, unbindAsync, onActivation, onDeactivation) => {
@@ -330,6 +331,7 @@ export const productionContainerModule = new ContainerModule(
330331
bind(RelationshipUpdater).toSelf().inSingletonScope();
331332

332333
// grpc / Connect API
334+
bind(APIHelloService).toSelf().inSingletonScope();
333335
bind(APIUserService).toSelf().inSingletonScope();
334336
bind(APITeamsService).toSelf().inSingletonScope();
335337
bind(APIWorkspacesService).toSelf().inSingletonScope();

components/server/src/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ export class Server {
317317

318318
log.info("Registered Bitbucket Server app at " + BitbucketServerApp.path);
319319
app.use(BitbucketServerApp.path, this.bitbucketServerApp.router);
320+
321+
app.use(this.api.apiRouter);
320322
}
321323

322324
public async start(port: number) {

0 commit comments

Comments
 (0)