-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
164 lines (140 loc) · 5.17 KB
/
server.ts
File metadata and controls
164 lines (140 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { TuskDrift } from './tuskDriftInit.ts';
import express from 'express';
import type { Request, Response } from 'express';
import axios from 'axios';
const app = express();
const PORT = 3000;
app.use(express.json());
// GET /api/weather-activity - Get location from IP, weather, and activity recommendations
app.get('/api/weather-activity', async (req: Request, res: Response) => {
try {
// First API call: Get user's location from IP
const locationResponse = await axios.get('http://ip-api.com/json/');
const { city, lat, lon, country } = locationResponse.data;
// Business logic: Determine activity based on location
const isCoastal = Math.abs(lon) > 50 || Math.abs(lat) < 30;
// Second API call: Get weather for the location
const weatherResponse = await axios.get(
`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t_weather=true`
);
const weather = weatherResponse.data.current_weather;
// Business logic: Recommend activity based on weather
let recommendedActivity = 'Play a board game';
if (weather.temperature > 40) {
recommendedActivity = 'Too hot - stay indoors';
} else if (weather.temperature > 20 && weather.windspeed < 20) {
recommendedActivity = isCoastal ? 'Beach day!' : 'Perfect for hiking!';
} else if (weather.temperature < 10) {
recommendedActivity = 'Hot chocolate weather';
} else if (weather.windspeed > 30) {
recommendedActivity = 'Too windy - indoor activities recommended';
} else {
recommendedActivity = 'Nice day for a walk';
}
// Third API call: Get a random activity suggestion
const activityResponse = await axios.get('https://bored-api.appbrewery.com/random');
const alternativeActivity = activityResponse.data;
res.json({
location: {
city,
country,
coordinates: { lat, lon },
isCoastal
},
weather: {
temperature: weather.temperature,
windspeed: weather.windspeed,
weathercode: weather.weathercode,
time: weather.time
},
recommendations: {
weatherBased: recommendedActivity,
alternative: {
activity: alternativeActivity.activity,
type: alternativeActivity.type,
participants: alternativeActivity.participants
}
}
});
} catch (error) {
res.status(500).json({ error: 'Failed to fetch weather and activity data' });
}
});
// GET /user/:id - Get random user with seed parameter
app.get('/api/user/:id', async (req: Request, res: Response) => {
try {
const { id } = req.params;
const response = await axios.get(`https://randomuser.me/api/?seed=${id}`);
res.json(response.data);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch user data' });
}
});
// POST /user - Create random user (no seed)
app.post('/api/user', async (req: Request, res: Response) => {
try {
const response = await axios.get('https://randomuser.me/api/');
res.json(response.data);
} catch (error) {
res.status(500).json({ error: 'Failed to create user' });
}
});
// GET /post/:id - Get post with comments
app.get('/api/post/:id', async (req: Request, res: Response) => {
try {
const { id } = req.params;
// Fetch post and comments in parallel
const [postResponse, commentsResponse] = await Promise.all([
axios.get(`https://jsonplaceholder.typicode.com/posts/${id}`),
axios.get(`https://jsonplaceholder.typicode.com/posts/${id}/comments`)
]);
res.json({
post: postResponse.data,
comments: commentsResponse.data
});
} catch (error) {
res.status(500).json({ error: 'Failed to fetch post data' });
}
});
// POST /post - Create new post
app.post('/api/post', async (req: Request, res: Response) => {
try {
const { title, body, userId } = req.body;
const response = await axios.post('https://jsonplaceholder.typicode.com/posts', {
title,
body,
userId
});
res.status(201).json(response.data);
} catch (error) {
res.status(500).json({ error: 'Failed to create post' });
}
});
// DELETE /api/post/:id - Delete post
app.delete('/api/post/:id', async (req: Request, res: Response) => {
try {
const { id } = req.params;
await axios.delete(`https://jsonplaceholder.typicode.com/posts/${id}`);
res.json({ message: `Post ${id} deleted successfully` });
} catch (error) {
res.status(500).json({ error: 'Failed to delete post' });
}
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'healthy' });
});
const main = async () => {
app.listen(PORT, () => {
TuskDrift.markAppAsReady();
console.log(`Server is running on http://localhost:${PORT}`);
console.log('\nAvailable endpoints:');
console.log(' GET /api/weather-activity - Recommend activity based on location and weather');
console.log(' GET /api/user/:id - Get user');
console.log(' POST /api/user - Create user');
console.log(' GET /api/post/:id - Get post, with comments');
console.log(' POST /api/post - Create post');
console.log(' DELETE /api/post/:id - Delete post');
});
};
main();