Context Management
Optimize request handling and configurations in Pylon with context management. Integrate seamlessly with Hono, access context within service functions, and elevate your web services' performance.
In Pylon, context management plays a crucial role in handling incoming requests, accessing request-specific data, and configuring your application. This document outlines how context management works in Pylon, including accessing the underlying Hono framework and utilizing context within service functions.
Hono Integration
Pylon leverages Hono (opens in a new tab), a fast and lightweight web framework, to power its underlying infrastructure. Understanding how to interact with the Hono framework is essential for customizing routes, middleware, and other configurations within your Pylon application.
app Instance
The app instance in Pylon represents the Hono application and provides a central point for defining routes, middleware, and other application-specific configurations. By accessing the app instance, you can define custom logic and behavior tailored to your application's requirements.
import {app} from '@getcronit/pylon'
export const graphql = {
Query: {
sum: (a: number, b: number) => a + b
},
Mutation: {
divide: (a: number, b: number) => a / b
}
}
app.get('/hello', (ctx, next) => {
return new Response('Hello, world!')
})
export default appCustom Routes and Middleware
By utilizing the app instance, you can define custom routes and middleware to handle incoming requests and implement additional functionality. This allows you to customize your application's behavior and extend its capabilities as needed.
app.get('/hello', (ctx, next) => {
return new Response('Hello, world!')
})There is a gotcha if you utilize app.route() and piggy-back on a Pylon app instance, because there must only be one import of the Pylon app instance. The import is the main entry point for the application and it is important that it should only be used once, because the Pylon app registers some global middlewares and the router. If you import it multiple times, it will register the middlewares multiple times and cause conflicts. Example conflicts include:
- Multiple request and response logs in the console.
- Multiple passes of middlewares (
app.use('*')), which can lead to incorrect variable reads/writes.
To avoid this, all other routers should be created as sub-routers using a new Hono instance.
/src/authRouter.ts:
import {type Bindings, type Variables} from '@getcronit/pylon'
import {Hono} from 'hono'
const authRouter = new Hono<{
Bindings: Bindings
Variables: Variables
}>()
authRouter.get('/', c => {
return c.text('Hello from Auth')
})
export default authRouter/src/index.ts:
import {app} from '@getcronit/pylon'
import authRouter from './authRouter'
app.route('/auth', authRouter)
export const graphql = {...}
export default appFor more detailed information on utilizing the Hono app instance, refer to the Hono documentation (opens in a new tab).
Accessing Context in Service Functions
Service functions in Pylon often require access to request-specific data or context information. Pylon provides mechanisms to access this context within your service functions, allowing you to implement custom logic based on request attributes.
getContext Function
The getContext function from the @getcronit/pylon package enables service functions to access the request context. This includes request headers, parameters, and other relevant information necessary for processing incoming requests.
import {app, getContext} from '@getcronit/pylon'
export const graphql = {
Query: {
protected: () => {
const ctx = getContext()
const header = ctx.req.header('X-API-Key')
if (header !== 'secret') {
return new Response('Unauthorized', {status: 401})
}
return new Response('The secret is safe with me!')
}
}
}
export default appgetEnv Function
Depending on your runtime enviroment, handling enviroment variables might differ. In Node.js or Bun, you can use the process.env object to access enviroment variables. In Cloudflare Workers, this is not possible.
Pylon provides the getEnv function to access enviroment variables in a runtime-agnostic way.
import {getEnv} from '@getcronit/pylon'
export const graphql = {
Query: {
secret: () => {
const secret = getEnv().SECRET_KEY
return new Response(`The secret key is: ${secret}`)
}
}
}
export default appCustom Bindings & Variables
Pylon allows you to define custom bindings and variables types to store sensitive data or request-specific information. By defining custom bindings and variables, you can securely store and access data within your application.
// pylon.d.ts
import '@getcronit/pylon'
declare module '@getcronit/pylon' {
interface Bindings {
SECRET_KEY: string
}
interface Variables {
userId: string
}
}SECRET_KEY=supersecretFor more information on custom bindings and variables, refer to the Pylon documentation.
Handling Authorization Logic
By accessing the request context within service functions, you can implement custom authorization logic based on incoming request attributes. This allows you to enforce access control policies and protect sensitive resources within your application.
Conclusion
Understanding context management in Pylon is essential for building robust and secure web services. By leveraging the Hono framework and accessing context within service functions, developers can customize application behavior and implement sophisticated logic tailored to their specific use cases.