-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathserver.js
More file actions
182 lines (157 loc) · 4.87 KB
/
server.js
File metadata and controls
182 lines (157 loc) · 4.87 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { WebflowClient } from "webflow-api";
import Fastify from "fastify";
import fastifyStatic from "@fastify/static";
import path from "path";
import url from "url";
import { Level } from "level";
import fs from "fs/promises";
// Load environment variables from .env file
const {
WEBFLOW_CLIENT_ID,
WEBFLOW_SECRET,
PORT,
NODE_ENV = "development",
} = process.env;
// Validate required environment variables
if (!WEBFLOW_CLIENT_ID || !WEBFLOW_SECRET) {
console.error(
"Missing required environment variables. Please check your .env file:"
);
console.error("WEBFLOW_CLIENT_ID and WEBFLOW_SECRET are required");
process.exit(1);
}
// Initialize our server with basic security headers
const server = Fastify({
logger: true,
trustProxy: true, // Required for secure cookies behind a proxy
});
// Add security headers
server.addHook("onSend", async (request, reply) => {
reply.headers({
"X-Content-Type-Options": "nosniff", // Prevent MIME type sniffing
"X-Frame-Options": "DENY", // Prevent clickjacking
"Strict-Transport-Security": "max-age=31536000; includeSubDomains", // Enforce HTTPS
});
});
// Initialize the database (Note: Use a proper database in production)
const db = new Level("data", { valueEncoding: "json" });
await db.open();
// OAuth 2.0 authentication endpoint
server.get("/auth", async (req, reply) => {
const { code, error, error_description } = req.query;
// Handle OAuth errors
if (error) {
console.error("OAuth Error:", error, error_description);
return reply.code(400).send({
error,
message: error_description,
});
}
// If no code is provided, redirect to the authorization URL
if (!code) {
const installUrl = WebflowClient.authorizeURL({
scope: scopes,
clientId: WEBFLOW_CLIENT_ID,
// Optional: Add state parameter for CSRF protection
state: Math.random().toString(36).substring(7),
});
return reply.redirect(installUrl);
}
try {
// Exchange the code for an access token
const token = await WebflowClient.getAccessToken({
clientId: WEBFLOW_CLIENT_ID,
clientSecret: WEBFLOW_SECRET,
code: code,
});
// Store the token in the database
await db.put("token", token);
if (NODE_ENV === "development") {
console.log("\nAccess Token Received:", token, "\n");
}
return reply.redirect("/?authorized=true");
} catch (error) {
console.error("Auth Error:", error);
return reply.code(500).send({
error: "Authentication failed",
message: error.message,
});
}
});
// Define the permissions your app needs
// See https://developers.webflow.com/v2.0.0/data/reference/scopes for all available scopes
const scopes = [
"sites:read",
// Uncomment scopes as needed:
// "pages:read",
// "pages:write",
// "collections:read",
// "collections:write",
];
// Example API endpoint
server.get("/sites", async (req, reply) => {
try {
const accessToken = await db.get("token");
const webflow = new WebflowClient({
accessToken,
// Add rate limiting and timeout options
timeOutInSeconds: 10000, // 10 second timeout
maxRetries: 3, // Retry up to 3 times
});
const sites = await webflow.sites.list();
return sites;
} catch (error) {
console.error("API Error:", error);
// Handle different types of errors
if (error.code === "LEVEL_NOT_FOUND") {
return reply.code(401).send({
error: "Not authenticated",
message: "Please authenticate first",
});
}
if (error.response?.status === 401) {
return reply.code(401).send({
error: "Invalid token",
message: "Please authenticate again",
});
}
return reply.code(500).send({
error: "Server error",
message: "Failed to fetch sites",
});
}
});
// --- Helper Functions and Setup Below --- //
// Set up static file serving for our frontend
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
server.register(fastifyStatic, {
root: path.join(__dirname, "static"),
});
// Home page - serves static/index.html
server.get("/", async (req, reply) => {
await reply.sendFile("index.html");
});
// Handle cleanup on server shutdown
const cleanup = async () => {
try {
await db.close();
await fs.rm("data", { recursive: true, force: true });
console.log("Database cleaned up successfully");
} catch (error) {
console.error("Error cleaning up database:", error);
}
process.exit(0);
};
// Listen for shutdown signals
process.on("SIGINT", cleanup); // Ctrl+C
process.on("SIGTERM", cleanup); // Kill command
process.on("SIGUSR2", cleanup); // Nodemon restart
// Start the server
server.listen({ port: PORT, host: "localhost" }, (err) => {
if (err) {
console.error("Failed to start server:", err);
process.exit(1);
}
console.log(`Server running at http://localhost:${PORT}`);
console.log("To use with ngrok: ngrok http " + PORT);
});