Skip to content

Commit ddf97bd

Browse files
author
Kristjan Kosic - Chris
committed
feat: handler method sample implementation
1 parent 94f9349 commit ddf97bd

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ You should receive the following response:
9393
}
9494
```
9595

96+
You can also try the following call:
97+
```bash
98+
curl --request GET \
99+
--url http://127.0.0.1:5003/config
100+
```
101+
96102
You can see the code and registered route here:
97103
https://github.com/learn-ark/dapp-core-module-http-server-template/blob/master/src/server.ts#L49-L56.
98104

src/handlers.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { app } from "@arkecosystem/core-container";
2+
import { Plugins } from "@arkecosystem/core-utils";
3+
4+
export const config = {
5+
async handler() {
6+
const appConfig = app.getConfig();
7+
8+
return {
9+
data: {
10+
version: app.getVersion(),
11+
network: {
12+
version: appConfig.get("network.pubKeyHash"),
13+
name: appConfig.get("network.name"),
14+
nethash: appConfig.get("network.nethash"),
15+
explorer: appConfig.get("network.client.explorer"),
16+
token: {
17+
name: appConfig.get("network.client.token"),
18+
symbol: appConfig.get("network.client.symbol"),
19+
},
20+
},
21+
plugins: Plugins.transformPlugins(appConfig.config.plugins),
22+
},
23+
};
24+
},
25+
config: {
26+
cors: true,
27+
},
28+
};

src/server.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { app } from "@arkecosystem/core-container";
2-
import { createServer, mountServer } from "@arkecosystem/core-http-utils";
2+
import { createServer, mountServer, plugins } from "@arkecosystem/core-http-utils";
33
import { Logger } from "@arkecosystem/core-interfaces";
44
import Hapi from "@hapi/hapi";
5+
import * as handlers from "./handlers";
56

67
export class Server {
78
private logger = app.resolvePlugin<Logger.ILogger>("logger");
@@ -47,6 +48,10 @@ export class Server {
4748
}
4849

4950
private static async registerRoutes(name: string, server: Hapi.Server): Promise<void> {
51+
await server.register({
52+
plugin: plugins.corsHeaders,
53+
});
54+
5055
server.route({
5156
method: "GET",
5257
path: "/",
@@ -55,6 +60,8 @@ export class Server {
5560
},
5661
});
5762

63+
server.route([{ method: "GET", path: "/config", ...handlers.config }]);
64+
5865
await mountServer(`Custom HTTP Public ${name.toUpperCase()} API`, server);
5966
}
6067
}

0 commit comments

Comments
 (0)