Skip to content

Instantly share code, notes, and snippets.

@WomB0ComB0
Last active June 28, 2025 20:40
Show Gist options
  • Select an option

  • Save WomB0ComB0/3fd409ea46e9953449d6a665e190ccee to your computer and use it in GitHub Desktop.

Select an option

Save WomB0ComB0/3fd409ea46e9953449d6a665e190ccee to your computer and use it in GitHub Desktop.
elysia-starter-advanced.ts and related files - with AI-generated descriptions
import { Elysia } from "elysia";
import { cors } from '@elysiajs/cors';
import { serverTiming } from '@elysiajs/server-timing';
import type { SocketAddress } from "bun";
import { rateLimit, DefaultContext, type Generator } from 'elysia-rate-limit';
import { ip } from 'elysia-ip';
import { opentelemetry } from '@elysiajs/opentelemetry'
import { record } from '@elysiajs/opentelemetry';
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-node'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'
import { resourceFromAttributes } from '@opentelemetry/resources';
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { swagger } from '@elysiajs/swagger';
import { elysiaHelmet } from 'elysiajs-helmet'
import logixlysia from 'logixlysia'
import { bearer } from '@elysiajs/bearer'
import { generateKeyPairSync } from 'crypto';
import jwt from 'jsonwebtoken';
import { spawn } from 'node:child_process';
import fs from 'node:fs';
/**
* Stringifies an object with 2-space indentation.
* @param {object} o - The object to stringify.
* @returns {string} The pretty-printed JSON string.
*/
const Stringify = (o: object): string => JSON.stringify(o, null, 2);
/**
* Generates a unique identifier for rate limiting based on the request's IP address.
* @param {*} _r - The request object (unused).
* @param {*} _s - The response object (unused).
* @param {{ ip: SocketAddress }} param2 - The context containing the IP address.
* @returns {string} The IP address or 'unknown' if not available.
*/
const ipGenerator: Generator<{ ip: SocketAddress }> = (_r, _s, { ip }) => ip?.address ?? 'unknown';
/**
* The current application version, loaded from package.json.
* @type {string}
*/
const version: string = await import("../package.json").then(t => t.version).catch(console.error) || 'N/A';
/**
* Checks if Docker is running on the system.
* @async
* @returns {Promise<boolean>} True if Docker is active, false otherwise.
*/
const checkDocker = async (): Promise<boolean> => {
try {
const { stdout } = (await Bun.$`systemctl is-active docker`);
return stdout.toString().trim() === 'active';
} catch (error) {
console.error("Docker is not running or systemctl command failed:", error);
return false;
}
}
/**
* Starts a Jaeger tracing container using Docker.
* Logs output to ./logs/jaeger.log.
* @see http://localhost:16686/search
*/
const runJaeger = (): void => {
const [out, err] =
Array(2)
.fill(
fs.openSync('./logs/jaeger.log', 'a')
);
const jaeger = spawn('docker', [
'run', '--rm', '--name', 'jaeger',
'-p', '5778:5778',
'-p', '16686:16686',
'-p', '4317:4317',
'-p', '4318:4318',
'-p', '14250:14250',
'-p', '14268:14268',
'-p', '9411:9411',
'jaegertracing/jaeger:2.1.0'
], {
detached: true,
stdio: ['ignore', out, err]
});
jaeger.unref();
};
/**
* Middleware for timing and logging the duration of each request.
* Adds a `start` timestamp to the store before handling,
* and logs the duration after handling.
*/
const timingMiddleware = new Elysia()
.state({ start: 0 })
.onBeforeHandle(({ store }) => (store.start = Date.now()))
.onAfterHandle(({ path, store: { start } }) =>
console.info(`[Elysia] ${path} took ${Date.now() - start}ms to execute`),
);
/**
* The secret used for signing and verifying JWT tokens.
* @type {string}
*/
const JWT_SECRET: string = process.env.JWT_SECRET || 'dev_secret';
/**
* Authentication route for registering a new user.
* Generates an RSA key pair and returns a JWT and the private key.
*/
const authRoute = new Elysia()
.post('/auth/register', () => {
const { publicKey, privateKey } = generateKeyPairSync('rsa', {
modulusLength: 2048,
});
const token = jwt.sign(
{ pub: publicKey.export({ type: 'pkcs1', format: 'pem' }) },
JWT_SECRET,
{ algorithm: 'HS256', expiresIn: 30 }
);
return {
token,
privateKey: privateKey.export({ type: 'pkcs1', format: 'pem' })
};
});
/**
* Middleware to require JWT Bearer authentication.
* Throws an error if the token is missing or invalid.
*/
const requireAuth = new Elysia()
.use(bearer())
.derive(({ bearer }) => {
if (!bearer) throw new Error('Missing Bearer token');
try {
const payload = jwt.verify(bearer, JWT_SECRET) as { pub: string };
return { publicKey: payload.pub };
} catch {
throw new Error('Invalid or expired token');
}
});
/**
* Utility routes for root, status, version, info, and health endpoints.
* Includes CORS preflight, HEAD, and GET handlers.
*/
const utilityRoute = new Elysia()
.use(timingMiddleware)
.get('/', () =>
record('root.get', () => {
return Stringify({
message: `Welcome to the API. Don't be naughty >:(`,
status: 200,
});
}), {
detail: {
summary: 'Root endpoint',
description: 'Welcome message for the API',
tags: ['Utility']
}
}
)
.head('/', ({ set }) =>
record('root.head', () => {
set.status = 200;
return;
}), {
detail: {
summary: 'Root HEAD',
description: 'HEAD for root endpoint',
tags: ['Utility']
}
}
)
.options('/', () =>
record('root.options', () => {
return Stringify({
message: 'CORS preflight response',
status: 204,
allow: 'GET,OPTIONS,HEAD',
});
}), {
detail: {
summary: 'Root OPTIONS',
description: 'CORS preflight for root',
tags: ['Utility']
}
}
)
.get('/status', async () =>
record('status.get', async () => {
const uptime = process.uptime();
const memoryUsage = process.memoryUsage();
const appVersion = version;
return Stringify({
message: 'Application status',
status: 200,
data: {
uptime: `${uptime.toFixed(2)} seconds`,
memory: {
rss: `${(memoryUsage.rss / 1_024 / 1_024).toFixed(2)} MB`,
heapTotal: `${(memoryUsage.heapTotal / 1_024 / 1_024).toFixed(2)} MB`,
heapUsed: `${(memoryUsage.heapUsed / 1_024 / 1_024).toFixed(2)} MB`,
external: `${(memoryUsage.external / 1_024 / 1_024).toFixed(2)} MB`,
},
version: appVersion,
environment: process.env.NODE_ENV || 'development',
},
});
}), {
detail: {
summary: 'Get application status',
description: 'Returns uptime, memory usage, version, and environment',
tags: ['Utility']
}
}
)
.head('/status', ({ set }) =>
record('status.head', () => {
set.status = 200;
return;
}), {
detail: {
summary: 'Status HEAD',
description: 'HEAD for status endpoint',
tags: ['Utility']
}
}
)
.options('/status', () =>
record('status.options', () => {
return Stringify({
message: 'CORS preflight response',
status: 204,
allow: 'GET,OPTIONS,HEAD',
});
}), {
detail: {
summary: 'Status OPTIONS',
description: 'CORS preflight for status',
tags: ['Utility']
}
}
)
.get('/version', async () =>
record('version.get', async () => {
const appVersion = version;
return Stringify({
version: appVersion,
status: 200,
});
}), {
detail: {
summary: 'Get API version',
description: 'Returns the current API version',
tags: ['Info']
}
}
)
.head('/version', ({ set }) =>
record('version.head', () => {
set.status = 200;
return;
}), {
detail: {
summary: 'Version HEAD',
description: 'HEAD for version endpoint',
tags: ['Info']
}
}
)
.options('/version', () =>
record('version.options', () => {
return Stringify({
message: 'CORS preflight response',
status: 204,
allow: 'GET,OPTIONS,HEAD',
});
}), {
detail: {
summary: 'Version OPTIONS',
description: 'CORS preflight for version',
tags: ['Info']
}
}
)
.get('/info', () =>
record('info.get', () => {
return Stringify({
message: `Information about the API`,
status: 200,
data: {
contact: `example@example.com`,
documentationUrl: 'https://docs.your-api.com',
},
});
}), {
detail: {
summary: 'Get API info',
description: 'Returns information about the API',
tags: ['Info']
}
}
)
.head('/info', ({ set }) =>
record('info.head', () => {
set.status = 200;
return;
}), {
detail: {
summary: 'Info HEAD',
description: 'HEAD for info endpoint',
tags: ['Info']
}
}
)
.options('/info', () =>
record('info.options', () => {
return Stringify({
message: 'CORS preflight response',
status: 204,
allow: 'GET,OPTIONS,HEAD',
});
}), {
detail: {
summary: 'Info OPTIONS',
description: 'CORS preflight for info',
tags: ['Info']
}
}
)
.get('/health', async () =>
record('health.get', () => {
return Stringify({ message: 'ok', status: 200 });
}), {
detail: {
summary: 'Health check',
description: 'Returns ok if the API is healthy',
tags: ['Health']
}
}
)
.head('/health', ({ set }) =>
record('health.head', () => {
set.status = 200;
return;
}), {
detail: {
summary: 'Health HEAD',
description: 'HEAD for health endpoint',
tags: ['Health']
}
}
)
.options('/health', () =>
record('health.options', () => {
return Stringify({
message: 'CORS preflight response',
status: 204,
allow: 'GET,OPTIONS,HEAD',
});
}), {
detail: {
summary: 'Health OPTIONS',
description: 'CORS preflight for health',
tags: ['Health']
}
}
);
/**
* Protected route that requires authentication.
* Returns the user's public key if access is granted.
*/
const protectedRoute = new Elysia()
.use(requireAuth)
.get('/example', (ctx: { publicKey: string }) =>
record('protected.example.get', () => {
return Stringify({
message: 'You have access!',
yourPublicKey: ctx.publicKey,
});
}), {
detail: {
summary: 'Protected Example',
description: 'An example endpoint that requires authentication',
tags: ['Protected']
}
})
.head('/example', ({ set }) =>
record('protected.example.head', () => {
set.status = 200;
return
}), {
detail: {
summary: 'Protected Example HEAD',
description: 'HEAD for protected example endpoint',
tags: ['Protected']
}
})
.options('/example', () =>
record('protected.example.options', () => {
return Stringify({
message: 'CORS preflight response',
status: 204,
allow: 'GET,OPTIONS,HEAD',
});
}), {
detail: {
summary: 'Protected Example OPTIONS',
description: 'CORS preflight for protected example',
tags: ['Protected']
}
});
/**
* OpenTelemetry resource for Jaeger tracing.
*/
const otelResource = resourceFromAttributes({
[ATTR_SERVICE_NAME]: 'elysia-api'
});
/**
* OTLP trace exporter for sending traces to Jaeger.
*/
const otlpExporter = new OTLPTraceExporter({
url: 'http://localhost:4318/v1/traces',
// compression: 'gzip',
keepAlive: true,
// httpAgentOptions: { keepAlive: true },
});
/**
* Batch span processor for OpenTelemetry.
*/
const batchSpanProcessor = new BatchSpanProcessor(otlpExporter, {
maxExportBatchSize: 512, // Default: 512
scheduledDelayMillis: 5_000, // Default: 5000ms (5s)
exportTimeoutMillis: 30_000, // Default: 30000ms (30s)
maxQueueSize: 2_048, // Default: 2048
});
/**
* Content Security Policy and permissions constants for Helmet.
*/
const permission = {
SELF: "'self'",
UNSAFE_INLINE: "'unsafe-inline'",
HTTPS: "https:",
DATA: "data:",
NONE: "'none'",
BLOB: "blob:",
} as const;
/**
* Main API application instance with all middleware and routes.
* Includes tracing, logging, security, CORS, rate limiting, authentication, and utility/protected routes.
*/
const api = new Elysia({ prefix: '/api/v1' })
.trace(async ({ onBeforeHandle, onAfterHandle, onError }) => {
onBeforeHandle(({ begin, onStop }) => {
onStop(({ end }) => {
console.log('BeforeHandle took', end - begin, 'ms');
});
});
onAfterHandle(({ begin, onStop }) => {
onStop(({ end }) => {
console.log('AfterHandle took', end - begin, 'ms');
});
});
onError(({ begin, onStop }) => {
onStop(({ end, error }) => {
console.error('Error occurred after', end - begin, 'ms', error);
});
});
})
.use(logixlysia({
config: {
showStartupMessage: true,
startupMessageFormat: 'simple',
timestamp: {
translateTime: 'yyyy-mm-dd HH:MM:ss.SSS'
},
logFilePath: './logs/server.log',
ip: true,
customLogFormat:
'🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}'
}
}))
.use(
elysiaHelmet({
csp: {
defaultSrc: [permission.SELF],
scriptSrc: [permission.SELF, permission.UNSAFE_INLINE],
styleSrc: [permission.SELF, permission.UNSAFE_INLINE],
imgSrc: [permission.SELF, permission.DATA, permission.HTTPS],
useNonce: true,
},
hsts: {
maxAge: 31_536_000,
includeSubDomains: true,
preload: true,
},
frameOptions: "DENY",
referrerPolicy: "strict-origin-when-cross-origin",
permissionsPolicy: {
camera: [permission.NONE],
microphone: [permission.NONE],
},
})
)
.use(ip())
.use(opentelemetry({
resource: otelResource,
spanProcessors: [batchSpanProcessor]
}))
.use(
serverTiming({
trace: {
request: true,
parse: true,
transform: true,
beforeHandle: true,
handle: true,
afterHandle: true,
error: true,
mapResponse: true,
total: true,
},
}),
)
.use(
cors({
origin: `http://localhost:3000`,
methods: ['GET', 'POST', 'OPTIONS', 'HEAD'],
exposeHeaders: ['Content-Type', 'Authorization'],
maxAge: 86_400,
credentials: true,
}),
)
.use(
rateLimit({
duration: 60_000,
max: 100,
headers: true,
scoping: 'scoped',
countFailedRequest: true,
errorResponse: new Response(
Stringify({
error: `Too many requests`,
}),
{ status: 429 },
),
generator: ipGenerator,
context: new DefaultContext(10_000)
}),
)
.use(authRoute)
.use(requireAuth)
.use(protectedRoute)
.use(utilityRoute)
.onError(({ code, error, set }) => {
console.error(Stringify({'ERROR': error}));
set.status = code === 'NOT_FOUND' ? 404 : 500;
return Stringify({
error: Error.isError(error) ? Stringify({ error }) : Stringify({ error }),
status: set.status,
});
});
/**
* Root application instance, includes Swagger documentation and the main API.
*/
const root = new Elysia()
.use(
swagger({
path: '/swagger',
documentation: {
info: {
title: '🦊 Elysia Advanced API',
version: '1.0.0',
description: `
Welcome to the **Elysia Advanced API**!
This API demonstrates advanced features including authentication,
security, observability, and more.
- 🚀 **Fast** and modern API with [ElysiaJS](https://elysiajs.com)
- 🔒 Security best practices (Helmet, Rate Limiting, CORS)
- 📊 Observability (OpenTelemetry, Jaeger)
- 📝 Auto-generated OpenAPI docs
> **Contact:** [Your Name](mailto:example@example.com)
> **Docs:** [API Docs](https://docs.your-api.com)
`,
termsOfService: 'https://your-api.com/terms',
contact: {
name: 'API Support',
url: 'https://your-api.com/support',
email: 'support@your-api.com'
},
license: {
name: 'MIT',
url: 'https://opensource.org/licenses/MIT'
}
},
externalDocs: {
description: 'Find more info here',
url: 'https://github.com/your-org/your-repo'
},
tags: [
{
name: 'Utility',
description: 'Endpoints for status, version, and root API info.'
},
{
name: 'Health',
description: 'Health check endpoints for uptime monitoring.'
},
{
name: 'Info',
description: 'General API information endpoints.'
},
{
name: 'Protected',
description: 'Endpoints that require authentication (JWT Bearer).'
}
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'Enter your JWT Bearer token to access protected endpoints.'
}
}
}
}
})
)
.use(api)
.listen(3_000);
/**
* The Elysia API application type.
* @typedef {typeof api} App
*/
export type App = typeof api;
/**
* Gracefully shuts down the application and flushes telemetry.
* @async
* @returns {Promise<void>}
*/
const shutdown = async (): Promise<void> => {
console.info('Shutting down 🦊 Elysia');
await batchSpanProcessor.forceFlush();
await root.stop();
process.exit(0);
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
/**
* Initializes Jaeger tracing by checking Docker and starting the Jaeger container if needed.
* Logs the server address on success.
* @async
* @returns {Promise<void>}
*/
const initializeJaeger = async (): Promise<void> => {
if (await checkDocker()) {
console.info("Docker is running. Checking for Jaeger container...");
try {
await Bun.$`docker inspect -f {{.State.Running}} jaeger`.text();
console.info("Jaeger container is already running.");
} catch {
console.info("Jaeger container not found or not running. Starting Jaeger...");
runJaeger();
}
} else {
console.error("Docker is not running. Please start Docker to use Jaeger tracing.");
process.exit(1);
}
console.info(
`🦊 Elysia is running at ${root.server?.hostname}:${root.server?.port}`
);
}
// Only initialize Jaeger if this file is the entry point.
(require.main === module) && initializeJaeger();
/**
* @fileoverview
* This file defines the advanced Elysia API server with authentication, security, observability,
* and documentation features. All major functions, classes, and constants are documented using JSDoc.
* For more on JSDoc best practices, see:
* https://www.pullrequest.com/blog/leveraging-jsdoc-for-better-code-documentation-in-javascript/
*/

elysia-starter-advanced.ts Description

File Type: ts

Generated Description:

Elysia Starter Advanced: A Detailed Analysis

This TypeScript file (elysia-starter-advanced.ts) defines an advanced Elysia.js web server application showcasing various middleware and features for robust API development. It goes beyond a basic Elysia setup by incorporating authentication, rate limiting, monitoring, logging, and comprehensive error handling.

1. Summary

The file creates a highly functional Elysia application designed for production environments. It includes features such as:

  • Authentication: Uses JWT (JSON Web Tokens) for user authentication and authorization, handling registration and token verification.
  • Rate Limiting: Implements rate limiting to prevent abuse and ensure service stability.
  • Monitoring & Tracing: Integrates with OpenTelemetry for distributed tracing and Jaeger for visualization, allowing for performance monitoring and debugging.
  • Logging: Employs logixlysia for structured logging.
  • Security: Uses elysiajs-helmet for enhanced security headers.
  • API Documentation: Leverages @elysiajs/swagger for automatic API documentation generation.
  • Health Checks: Provides a /status endpoint to monitor application health and resource usage.
  • Docker Integration: Includes a function to check if Docker is running and automatically launches a Jaeger instance if needed.

2. Key Components and Functions

  • checkDocker(): Asynchronously checks if the Docker service is running using the systemctl command. Returns a boolean indicating whether Docker is active.
  • runJaeger(): Spawns a Jaeger tracing instance as a detached Docker container, configuring port mappings for various Jaeger services. Logs are redirected to a local file.
  • timingMiddleware: A custom Elysia middleware that measures the execution time of each request and logs it to the console.
  • authRoute: An Elysia sub-application handling user registration. It generates RSA key pairs, creates JWTs signed with a secret key, and returns the token and private key to the client.
  • requireAuth: An Elysia middleware that verifies JWTs from the Authorization header using the bearer middleware, extracting the public key for subsequent authorization checks. It throws an error if the token is missing or invalid.
  • utilityRoute: An Elysia sub-application containing various endpoints:
    • /: A root endpoint providing a welcome message and demonstrating OpenTelemetry's record function. Supports GET, HEAD, and OPTIONS requests.
    • /status: An endpoint providing application status information (uptime, memory usage, version).
  • ipGenerator: A custom generator function for elysia-rate-limit that extracts the client's IP address from the request context.

3. Notable Patterns and Techniques

  • Modular Design: The application is broken down into smaller, reusable Elysia sub-applications (authRoute, utilityRoute, timingMiddleware) enhancing maintainability and testability.
  • Middleware Stacking: Uses Elysia's middleware capabilities extensively, combining multiple middleware functions (e.g., cors, serverTiming, rateLimit, bearer, elysiaHelmet) to add various functionalities to the application.
  • Asynchronous Operations: Uses async/await for asynchronous operations like checking Docker status and fetching the application version.
  • Error Handling: Includes try...catch blocks to handle potential errors (e.g., Docker check failure, JWT verification failure).
  • Dependency Injection: Implicit dependency injection through Elysia's context mechanism ({ ip } in ipGenerator, bearer in requireAuth).
  • OpenTelemetry Integration: Uses OpenTelemetry for tracing and monitoring, providing insights into application performance and behavior.
  • Structured Logging: Uses logixlysia likely for more informative and machine-readable logs.
  • Environment Variables: Uses environment variables (JWT_SECRET, NODE_ENV) for configuration flexibility.

4. Potential Use Cases

This advanced starter template is well-suited for building production-ready APIs that require:

  • Secure Authentication and Authorization: Suitable for applications needing robust user authentication and role-based access control.
  • High Availability and Scalability: The design allows for easy integration with load balancers and other scaling solutions.
  • Performance Monitoring and Debugging: OpenTelemetry integration provides detailed insights into application performance, making it easy to identify and resolve bottlenecks.
  • API Documentation: Automatically generated Swagger documentation simplifies API integration for developers.
  • Robust Error Handling: The included error handling mechanisms prevent unexpected application crashes.

This comprehensive example demonstrates best practices in building secure, scalable, and maintainable Elysia.js applications. It can serve as a strong foundation for various projects requiring a robust backend system.

Description generated on 6/29/2025, 3:34:16 AM

{
"name": "elysia-testing",
"version": "1.0.50",
"scripts": {
"dev": "bun run --watch ./src/elysia-starter-advanced.ts"
},
"dependencies": {
"@elysiajs/bearer": "^1.3.0",
"@elysiajs/cors": "^1.3.3",
"@elysiajs/opentelemetry": "^1.3.0",
"@elysiajs/server-timing": "^1.3.0",
"@elysiajs/swagger": "^1.3.0",
"@opentelemetry/resources": "^2.0.1",
"@types/jsonwebtoken": "^9.0.10",
"elysia": "1.3.5",
"elysia-ip": "^1.0.10",
"elysia-rate-limit": "^4.4.0",
"elysiajs-helmet": "^1.0.2",
"jsonwebtoken": "^9.0.2",
"logixlysia": "^5.1.0"
},
"devDependencies": {
"bun-types": "latest"
},
"module": "src/index.js"
}
#!/usr/bin/env bash
#
# Demo script for Elysia API endpoints.
# Follows shell scripting best practices:
# - Strict error handling
# - Helpful usage message
# - Functions for organization
# - Quoting variables
# - Debug mode via TRACE=1
# - Reference: https://sharats.me/posts/shell-script-best-practices/
# - Reference: https://learn.openwaterfoundation.org/owf-learn-linux-shell/best-practices/best-practices/
#
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
echo "Usage: ./demo.sh
Runs a series of test requests against the Elysia API.
Environment variables:
TRACE=1 Enable debug output
Dependencies:
curl, jq
"
exit 0
fi
cd "$(dirname "$0")"
API_URL="http://localhost:3000/api/v1"
print_section() {
printf "\n== %s ==\n" "$1"
}
health_check() {
print_section "Health Check"
curl -i "${API_URL}/health"
echo
}
status_check() {
print_section "Status"
curl -i "${API_URL}/status"
echo
}
version_check() {
print_section "Version"
curl -i "${API_URL}/version"
echo
}
info_check() {
print_section "Info"
curl -i "${API_URL}/info"
echo
}
cors_preflight() {
print_section "CORS Preflight (OPTIONS)"
curl -i -X OPTIONS "${API_URL}/"
echo
}
register_and_get_token() {
print_section "Register (Get JWT Token)"
local register_response token
register_response=$(curl --silent -X POST "${API_URL}/auth/register")
if ! echo "$register_response" | jq . >/dev/null 2>&1; then
echo "Error: Failed to parse register response as JSON" >&2
echo "$register_response"
exit 1
fi
echo "$register_response" | jq
token=$(echo "$register_response" | jq -r .token)
if [[ -z "$token" || "$token" == "null" ]]; then
echo "Error: No token found in register response" >&2
exit 1
fi
echo "Token: $token"
export TOKEN="$token"
echo
}
access_protected_with_token() {
print_section "Access Protected Endpoint with Token"
curl -i -H "Authorization: Bearer $TOKEN" "${API_URL}/example"
echo
}
access_protected_without_token() {
print_section "Access Protected Endpoint without Token (should fail)"
curl -i "${API_URL}/example"
echo
}
rate_limiting_test() {
print_section "Rate Limiting Test (should get 429 after 100 requests)"
local status
for ((i=1; i<=105; i++)); do
status=$(curl --silent --output /dev/null --write-out "%{http_code}" "${API_URL}/health")
echo "Request $i: $status"
if [[ "$status" == "429" ]]; then
echo "Rate limit hit at request $i"
break
fi
done
}
main() {
health_check
status_check
version_check
info_check
cors_preflight
register_and_get_token
access_protected_with_token
access_protected_without_token
rate_limiting_test
print_section "Demo Complete"
}
main "$@"
@WomB0ComB0

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment