feat: add zero-configuration Brainy service template with augmentation-first architecture
- Implement WebSocket augmentation for real-time communication - Implement WebRTC augmentation for peer-to-peer connections - Implement HTTP augmentation as minimal REST fallback - Add auto-discovery augmentation for data pattern analysis - Add adaptive storage augmentation for intelligent resource management - Add environment adapter augmentation for universal compatibility - Template auto-detects environment (browser, Node.js, serverless, containers) - Intelligent transport selection (WebRTC → WebSocket → HTTP) - Automatic storage optimization (memory → filesystem → S3) - Zero configuration required - just npm start - Includes intelligent verb scoring by default - Works in any environment without configuration - Full documentation and examples included
This commit is contained in:
parent
880b8f74e3
commit
ee003a9473
30 changed files with 3548 additions and 0 deletions
95
examples/brainy-service-template/src/utils/errors.js
Normal file
95
examples/brainy-service-template/src/utils/errors.js
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
export class ApiError extends Error {
|
||||
constructor(statusCode, message, isOperational = true, stack = '') {
|
||||
super(message)
|
||||
this.name = this.constructor.name
|
||||
this.statusCode = statusCode
|
||||
this.isOperational = isOperational
|
||||
|
||||
if (stack) {
|
||||
this.stack = stack
|
||||
} else {
|
||||
Error.captureStackTrace(this, this.constructor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class ValidationError extends ApiError {
|
||||
constructor(message, field = null) {
|
||||
super(400, message)
|
||||
this.field = field
|
||||
this.name = 'ValidationError'
|
||||
}
|
||||
}
|
||||
|
||||
export class NotFoundError extends ApiError {
|
||||
constructor(resource, id = null) {
|
||||
const message = id
|
||||
? `${resource} with ID ${id} not found`
|
||||
: `${resource} not found`
|
||||
super(404, message)
|
||||
this.name = 'NotFoundError'
|
||||
this.resource = resource
|
||||
this.resourceId = id
|
||||
}
|
||||
}
|
||||
|
||||
export class ConflictError extends ApiError {
|
||||
constructor(message) {
|
||||
super(409, message)
|
||||
this.name = 'ConflictError'
|
||||
}
|
||||
}
|
||||
|
||||
export class DatabaseError extends ApiError {
|
||||
constructor(message, operation = null) {
|
||||
super(500, message)
|
||||
this.name = 'DatabaseError'
|
||||
this.operation = operation
|
||||
}
|
||||
}
|
||||
|
||||
export class AuthenticationError extends ApiError {
|
||||
constructor(message = 'Authentication failed') {
|
||||
super(401, message)
|
||||
this.name = 'AuthenticationError'
|
||||
}
|
||||
}
|
||||
|
||||
export class AuthorizationError extends ApiError {
|
||||
constructor(message = 'Insufficient permissions') {
|
||||
super(403, message)
|
||||
this.name = 'AuthorizationError'
|
||||
}
|
||||
}
|
||||
|
||||
export class RateLimitError extends ApiError {
|
||||
constructor(message = 'Rate limit exceeded') {
|
||||
super(429, message)
|
||||
this.name = 'RateLimitError'
|
||||
}
|
||||
}
|
||||
|
||||
// Error factory for common scenarios
|
||||
export const createError = {
|
||||
validation: (message, field) => new ValidationError(message, field),
|
||||
notFound: (resource, id) => new NotFoundError(resource, id),
|
||||
conflict: (message) => new ConflictError(message),
|
||||
database: (message, operation) => new DatabaseError(message, operation),
|
||||
auth: (message) => new AuthenticationError(message),
|
||||
forbidden: (message) => new AuthorizationError(message),
|
||||
rateLimit: (message) => new RateLimitError(message),
|
||||
badRequest: (message) => new ApiError(400, message),
|
||||
internal: (message) => new ApiError(500, message)
|
||||
}
|
||||
|
||||
export default {
|
||||
ApiError,
|
||||
ValidationError,
|
||||
NotFoundError,
|
||||
ConflictError,
|
||||
DatabaseError,
|
||||
AuthenticationError,
|
||||
AuthorizationError,
|
||||
RateLimitError,
|
||||
createError
|
||||
}
|
||||
63
examples/brainy-service-template/src/utils/logger.js
Normal file
63
examples/brainy-service-template/src/utils/logger.js
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import winston from 'winston'
|
||||
import config from 'config'
|
||||
|
||||
const logLevel = config.get('logging.level') || 'info'
|
||||
const logFormat = config.get('logging.format') || 'combined'
|
||||
|
||||
// Create custom format for development
|
||||
const devFormat = winston.format.combine(
|
||||
winston.format.timestamp({ format: 'HH:mm:ss' }),
|
||||
winston.format.colorize(),
|
||||
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
||||
const metaStr = Object.keys(meta).length ? JSON.stringify(meta, null, 2) : ''
|
||||
return `${timestamp} [${level}]: ${message} ${metaStr}`
|
||||
})
|
||||
)
|
||||
|
||||
// Create custom format for production
|
||||
const prodFormat = winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
winston.format.errors({ stack: true }),
|
||||
winston.format.json()
|
||||
)
|
||||
|
||||
// Create logger instance
|
||||
export const logger = winston.createLogger({
|
||||
level: logLevel,
|
||||
format: logFormat === 'dev' ? devFormat : prodFormat,
|
||||
defaultMeta: {
|
||||
service: 'brainy-service',
|
||||
version: process.env.npm_package_version || '1.0.0'
|
||||
},
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
handleExceptions: true,
|
||||
handleRejections: true
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
// Add file logging for production
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
logger.add(new winston.transports.File({
|
||||
filename: 'logs/error.log',
|
||||
level: 'error',
|
||||
handleExceptions: true,
|
||||
maxsize: 5242880, // 5MB
|
||||
maxFiles: 5
|
||||
}))
|
||||
|
||||
logger.add(new winston.transports.File({
|
||||
filename: 'logs/combined.log',
|
||||
maxsize: 5242880, // 5MB
|
||||
maxFiles: 5
|
||||
}))
|
||||
}
|
||||
|
||||
// Capture unhandled errors
|
||||
logger.exceptions.handle(
|
||||
new winston.transports.Console(),
|
||||
new winston.transports.File({ filename: 'logs/exceptions.log' })
|
||||
)
|
||||
|
||||
export default logger
|
||||
Reference in a new issue