Skip to content

Commit 11d7bf4

Browse files
authored
Merge pull request #8 from dragonflydb/joezhou/ad-server-bun-paths
feat(example): ad server update routes and README
2 parents 7f2b6c1 + dd0b182 commit 11d7bf4

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

ad-server-cache-bun/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Example: Ad Server using Bun, ElysiaJS, and Dragonfly
22

3+
In this example, we will build a real-time ad server API using Bun, ElysiaJS, and Dragonfly.
4+
In terms of data types, we use `Hash` to store ad metadata and `Set` to store ad categories and user preferences.
5+
36
## Packages Used
47

58
- [ElysiaJS](https://elysiajs.com/) is a TypeScript framework supercharged by the [Bun](https://bun.sh/) runtime with end-to-end type safety.
@@ -28,3 +31,39 @@ bun run dev # (or `bun dev`)
2831
```
2932

3033
- The ad server API would be running on `http://localhost:3000/`
34+
35+
## Interact with the Ad Server API
36+
37+
- Create ad metadata with the following request:
38+
39+
```shell
40+
curl --request POST \
41+
--url http://localhost:3888/ads \
42+
--header 'Content-Type: application/json' \
43+
--data '{
44+
"id": "1",
45+
"title": "Dragonfly - a data store built for modern workloads",
46+
"category": "technology",
47+
"clickURL": "https://www.dragonflydb.io/",
48+
"imageURL": "https://www.dragonflydb.io/blog"
49+
}'
50+
```
51+
52+
- Create or update user preferences with the following request:
53+
54+
```shell
55+
curl --request POST \
56+
--url http://localhost:3888/ads/user_preferences \
57+
--header 'Content-Type: application/json' \
58+
--data '{
59+
"userId": "1",
60+
"categories": ["technology", "sports"]
61+
}'
62+
```
63+
64+
- Retrieve ads for a specific user with the following request:
65+
66+
```shell
67+
curl --request GET \
68+
--url http://localhost:3888/ads/user_preferences/1
69+
```

ad-server-cache-bun/src/ads.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ export class AdMetadataStore {
1515
this.client = client;
1616
}
1717

18-
metadataKey(metadataId: string): string {
18+
private metadataKey(metadataId: string): string {
1919
return `${AdMetadataStore.AD_METADATA_PREFIX}:${metadataId}`;
2020
}
2121

22-
categoryKey(metadataCategory: string): string {
22+
private categoryKey(metadataCategory: string): string {
2323
return `${AdMetadataStore.AD_CATEGORY_PREFIX}:${metadataCategory}`;
2424
}
2525

26-
userPreferenceKey(userId: string): string {
26+
private userPreferenceKey(userId: string): string {
2727
return `${AdMetadataStore.AD_USER_PREFERENCE_PREFIX}:${userId}`;
2828
}
2929

@@ -72,7 +72,7 @@ export class AdMetadataStore {
7272
return await this.getAdMetadataList(adIds);
7373
}
7474

75-
async getAdMetadataList(ids: Array<string>): Promise<Array<AdMetadata>> {
75+
private async getAdMetadataList(ids: Array<string>): Promise<Array<AdMetadata>> {
7676
const pipeline = this.client.pipeline();
7777
ids.forEach((id: string) => {
7878
pipeline.hgetall(this.metadataKey(id))

ad-server-cache-bun/src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ const client = new Dragonfly();
88

99
const app = new Elysia()
1010
.decorate("adMetadataCache", new AdMetadataStore(client))
11-
.get(
12-
"/ads/:userId",
13-
async (context) => {
14-
return await context.adMetadataCache.getAdMetadataListByUserPreference(context.params.userId);
15-
},
16-
)
1711
.post(
1812
"/ads",
1913
async (context) => {
@@ -24,15 +18,21 @@ const app = new Elysia()
2418
{body: AdMetadata}
2519
)
2620
.post(
27-
"/ads/preferences",
21+
"/ads/user_preferences",
2822
async (context) => {
2923
await context.adMetadataCache.createUserPreference(context.body);
3024
context.set.status = 201;
3125
return;
3226
},
3327
{body: UserAdPreferences}
3428
)
35-
.listen(3000);
29+
.get(
30+
"/ads/user_preferences/:userId",
31+
async (context) => {
32+
return await context.adMetadataCache.getAdMetadataListByUserPreference(context.params.userId);
33+
},
34+
)
35+
.listen(3888);
3636

3737
console.log(
3838
`Ad server API is running at ${app.server?.hostname}:${app.server?.port}`

0 commit comments

Comments
 (0)