Skip to content

Commit 29760cc

Browse files
committed
feat(example): ad server update routes and README
1 parent 7f2b6c1 commit 29760cc

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
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/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)