|
6 | 6 | [](https://github.com/stackpress/ingest/commits/main/) |
7 | 7 | [](https://github.com/stackpress/ingest/blob/main/LICENSE) |
8 | 8 |
|
9 | | -An unopinionated, event driven, pluggable, serverless framework. |
| 9 | +An unopinionated, event driven, pluggable, server/less framework. |
10 | 10 |
|
11 | | -## Install |
| 11 | +## Overview |
| 12 | + |
| 13 | +Ingest is a lightweight, flexible server framework that brings the familiar Express.js-like API to serverless environments. Built on top of the powerful `@stackpress/lib` event system, Ingest provides a unified approach to building applications that can run anywhere - from traditional Node.js servers to serverless platforms like AWS Lambda, Vercel, and Netlify. |
| 14 | + |
| 15 | +## Key Features |
| 16 | + |
| 17 | +- **🚀 Serverless-First**: Designed specifically for serverless environments while maintaining compatibility with traditional servers |
| 18 | +- **🔄 Event-Driven**: Built on a robust event system that enables reactive programming patterns |
| 19 | +- **🛣️ Multi-Routing Interface**: Four different routing approaches in one framework |
| 20 | +- **🔌 Plugin System**: Highly extensible with a simple plugin architecture |
| 21 | +- **📦 Build Support**: Exposes routing information for bundlers and build tools |
| 22 | +- **🌐 Cross-Platform**: Works with Node.js HTTP, WHATWG Fetch, and various serverless platforms |
| 23 | + |
| 24 | +## Installation |
12 | 25 |
|
13 | 26 | ```bash |
14 | | -npm i @stackpress/ingest |
| 27 | +npm install @stackpress/ingest |
| 28 | +# or |
| 29 | +yarn add @stackpress/ingest |
15 | 30 | ``` |
16 | 31 |
|
17 | | -## Usage |
| 32 | +## Quick Start |
| 33 | + |
| 34 | +### Basic HTTP Server |
18 | 35 |
|
19 | | -```js |
20 | | -// src/server.ts |
| 36 | +```typescript |
21 | 37 | import { server } from '@stackpress/ingest/http'; |
22 | 38 |
|
23 | | -//make a new app |
24 | 39 | const app = server(); |
25 | | -//add a route |
26 | | -app.get('/', function HomePage(req, res) { |
27 | | - res.setHTML('Hello, World'); |
| 40 | + |
| 41 | +// Traditional Express-like routing |
| 42 | +app.get('/', (req, res) => { |
| 43 | + res.setHTML('<h1>Hello World!</h1>'); |
28 | 44 | }); |
29 | | -//start the server |
30 | | -app.create().listen(3000); |
| 45 | + |
| 46 | +app.get('/api/users/:id', (req, res) => { |
| 47 | + const userId = req.data.get('id'); |
| 48 | + res.setJSON({ id: userId, name: 'John Doe' }); |
| 49 | +}); |
| 50 | + |
| 51 | +// Start the server |
| 52 | +app.create().listen(3000, () => { |
| 53 | + console.log('Server running on port 3000'); |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +### Serverless Function (Vercel) |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { server } from '@stackpress/ingest/whatwg'; |
| 61 | + |
| 62 | +const app = server(); |
| 63 | + |
| 64 | +app.get('/api/hello', (req, res) => { |
| 65 | + res.setJSON({ message: 'Hello from Vercel!' }); |
| 66 | +}); |
| 67 | + |
| 68 | +export default async function handler(request: Request) { |
| 69 | + return await app.handle(request, new Response()); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Multi-Routing Interface |
| 74 | + |
| 75 | +Ingest provides four different ways to define routes, giving you flexibility in how you organize your application: |
| 76 | + |
| 77 | +### 1. Action Router (Traditional) |
| 78 | +Express.js-like inline route handlers: |
| 79 | + |
| 80 | +```typescript |
| 81 | +app.action.get('/users', (req, res) => { |
| 82 | + res.setJSON({ users: [] }); |
| 83 | +}); |
| 84 | +``` |
| 85 | + |
| 86 | +### 2. Entry Router (File-based) |
| 87 | +Route to files that export default handlers: |
| 88 | + |
| 89 | +```typescript |
| 90 | +app.entry.get('/users', './routes/users.js'); |
| 91 | +``` |
| 92 | + |
| 93 | +### 3. Import Router (Lazy Loading) |
| 94 | +Dynamic imports for code splitting: |
| 95 | + |
| 96 | +```typescript |
| 97 | +app.import.get('/users', () => import('./routes/users.js')); |
| 98 | +``` |
| 99 | + |
| 100 | +### 4. View Router (Template-based) |
| 101 | +Direct template rendering: |
| 102 | + |
| 103 | +```typescript |
| 104 | +app.view.get('/users', './views/users.hbs'); |
31 | 105 | ``` |
32 | 106 |
|
33 | | -### Entry Files |
| 107 | +### Inferred Routing |
| 108 | + |
| 109 | +Ingest can automatically determine which router to use based on your input: |
| 110 | + |
| 111 | +```typescript |
| 112 | +// Automatically uses action router |
| 113 | +app.get('/users', (req, res) => { /* handler */ }); |
| 114 | + |
| 115 | +// Automatically uses import router |
| 116 | +app.get('/users', () => import('./routes/users.js')); |
| 117 | + |
| 118 | +// Automatically uses view router |
| 119 | +app.get('/users', './views/users.hbs'); |
| 120 | +``` |
| 121 | + |
| 122 | +## Plugin System |
| 123 | + |
| 124 | +Ingest features a powerful plugin system that allows you to modularize your application: |
| 125 | + |
| 126 | +### Creating a Plugin |
| 127 | + |
| 128 | +```typescript |
| 129 | +// src/plugins/auth.ts |
| 130 | +export default function authPlugin(server) { |
| 131 | + server.on('request', (req, res) => { |
| 132 | + // Add authentication logic |
| 133 | + if (!req.headers.get('authorization')) { |
| 134 | + res.setError('Unauthorized', {}, [], 401); |
| 135 | + return false; // Stop processing |
| 136 | + } |
| 137 | + }); |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +### Registering Plugins |
| 142 | + |
| 143 | +Add plugins to your `package.json`: |
34 | 144 |
|
35 | | -You can route to files as well. |
| 145 | +```json |
| 146 | +{ |
| 147 | + "plugins": [ |
| 148 | + "./src/plugins/auth", |
| 149 | + "./src/plugins/logging", |
| 150 | + "@my-org/ingest-plugin" |
| 151 | + ] |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +### Bootstrapping |
36 | 156 |
|
37 | | -```js |
38 | | -import path from 'node:path'; |
| 157 | +```typescript |
39 | 158 | import { server } from '@stackpress/ingest/http'; |
40 | 159 |
|
41 | | -//make a new app |
42 | 160 | const app = server(); |
43 | | -//add a route |
44 | | -route.get('/', path.join(__dirname, 'home')); |
45 | | -//start the server |
| 161 | + |
| 162 | +// Load all plugins |
| 163 | +await app.bootstrap(); |
| 164 | + |
46 | 165 | app.create().listen(3000); |
47 | 166 | ``` |
48 | 167 |
|
49 | | -```js |
50 | | -// src/home.ts |
51 | | -export default function HomePage(req, res) { |
52 | | - res.setHTML('Hello, World'); |
| 168 | +## Event-Driven Architecture |
| 169 | + |
| 170 | +Ingest is built on a powerful event system that allows for reactive programming: |
| 171 | + |
| 172 | +```typescript |
| 173 | +// Listen to all requests |
| 174 | +app.on('request', (req, res) => { |
| 175 | + console.log(`${req.method} ${req.url.pathname}`); |
| 176 | +}); |
| 177 | + |
| 178 | +// Listen to specific routes |
| 179 | +app.on('GET /api/users', (req, res) => { |
| 180 | + // This runs for GET /api/users |
| 181 | +}); |
| 182 | + |
| 183 | +// Priority-based listeners |
| 184 | +app.on('request', middleware1, 10); // Higher priority |
| 185 | +app.on('request', middleware2, 5); // Lower priority |
| 186 | +``` |
| 187 | + |
| 188 | +## Deployment Examples |
| 189 | + |
| 190 | +### AWS Lambda |
| 191 | + |
| 192 | +```typescript |
| 193 | +import { server } from '@stackpress/ingest/whatwg'; |
| 194 | + |
| 195 | +const app = server(); |
| 196 | +app.get('/api/hello', (req, res) => { |
| 197 | + res.setJSON({ message: 'Hello from Lambda!' }); |
| 198 | +}); |
| 199 | + |
| 200 | +export const handler = async (event, context) => { |
| 201 | + const request = new Request(event.requestContext.http.sourceIp); |
| 202 | + const response = new Response(); |
| 203 | + return await app.handle(request, response); |
| 204 | +}; |
| 205 | +``` |
| 206 | + |
| 207 | +### Vercel |
| 208 | + |
| 209 | +```typescript |
| 210 | +import { server } from '@stackpress/ingest/whatwg'; |
| 211 | + |
| 212 | +const app = server(); |
| 213 | +app.get('/api/users', (req, res) => { |
| 214 | + res.setJSON({ users: [] }); |
| 215 | +}); |
| 216 | + |
| 217 | +export default async function handler(req: Request) { |
| 218 | + return await app.handle(req, new Response()); |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +### Netlify |
| 223 | + |
| 224 | +```typescript |
| 225 | +import { server } from '@stackpress/ingest/whatwg'; |
| 226 | + |
| 227 | +const app = server(); |
| 228 | +app.get('/.netlify/functions/api', (req, res) => { |
| 229 | + res.setJSON({ message: 'Hello from Netlify!' }); |
| 230 | +}); |
| 231 | + |
| 232 | +export const handler = async (event, context) => { |
| 233 | + const request = new Request(event.rawUrl); |
| 234 | + const response = new Response(); |
| 235 | + return await app.handle(request, response); |
53 | 236 | }; |
54 | | -``` |
| 237 | +``` |
| 238 | + |
| 239 | +## Build Support |
| 240 | + |
| 241 | +Ingest exposes routing information that can be used by bundlers and build tools: |
| 242 | + |
| 243 | +```typescript |
| 244 | +const app = server(); |
| 245 | +app.get('/users', () => import('./routes/users.js')); |
| 246 | +app.get('/posts', () => import('./routes/posts.js')); |
| 247 | + |
| 248 | +// Access routing information |
| 249 | +console.log(app.routes); // Route definitions |
| 250 | +console.log(app.imports); // Dynamic imports |
| 251 | +console.log(app.entries); // File entries |
| 252 | +console.log(app.views); // View templates |
| 253 | +``` |
| 254 | + |
| 255 | +This information can be used by bundlers to: |
| 256 | +- Pre-bundle route modules |
| 257 | +- Generate static route manifests |
| 258 | +- Optimize code splitting |
| 259 | +- Create deployment artifacts |
| 260 | + |
| 261 | +## Documentation |
| 262 | + |
| 263 | +- [API Reference](./docs/api/README.md) - Complete API documentation |
| 264 | +- [Examples](./docs/examples.md) - Comprehensive usage examples |
| 265 | +- [Plugin Development](./docs/plugin-development.md) - Guide to creating plugins |
| 266 | + |
| 267 | +## Examples |
| 268 | + |
| 269 | +Check out the `examples/` directory for complete working examples: |
| 270 | + |
| 271 | +- `with-http` - Basic HTTP server |
| 272 | +- `with-vercel` - Vercel deployment |
| 273 | +- `with-lambda` - AWS Lambda deployment |
| 274 | +- `with-netlify` - Netlify deployment |
| 275 | +- `with-plugins` - Plugin system usage |
| 276 | +- `with-handlebars` - Template engine integration |
0 commit comments