Skip to content

Web Server Service

REST API and Web Admin Panel server.

Overview

WebServer provides HTTP API and serves the admin panel.

Architecture

javascript
class WebServer {
  constructor() {
    this.app = express()
    this.setupMiddleware()
    this.setupRoutes()
  }

  setupMiddleware() {
    this.app.use(cors())
    this.app.use(express.json())
    this.app.use(authMiddleware)
    this.app.use(rateLimiter)
  }

  setupRoutes() {
    this.app.use('/api/auth', authRoutes)
    this.app.use('/api/config', configRoutes)
    this.app.use('/api/tools', toolRoutes)
    this.app.use('/api/mcp', mcpRoutes)
  }
}

Route Modules

RouteFileDescription
/api/authauth.jsAuthentication
/api/configconfig.jsConfiguration
/api/channelschannels.jsChannel management
/api/toolstools.jsTool management
/api/mcpmcp.jsMCP servers
/api/conversationsconversations.jsChat history

Authentication

JWT-based authentication:

javascript
const authMiddleware = (req, res, next) => {
  const token = req.cookies.auth_token || 
                req.headers.authorization?.replace('Bearer ', '')
  
  if (!token) {
    return res.status(401).json({ error: 'Unauthorized' })
  }
  
  try {
    req.user = jwt.verify(token, SECRET)
    next()
  } catch {
    res.status(401).json({ error: 'Invalid token' })
  }
}

Response Format

Unified response wrapper:

javascript
const sendSuccess = (res, data) => {
  res.json({ success: true, data })
}

const sendError = (res, error, code = 400) => {
  res.status(code).json({ 
    success: false, 
    error: error.message,
    code: error.code 
  })
}

Rate Limiting

javascript
const rateLimiter = rateLimit({
  windowMs: 60 * 1000,  // 1 minute
  max: 60,              // 60 requests per window
  message: { error: 'Too many requests' }
})

Static Files

Serves Next.js admin panel:

javascript
// Serve static files
app.use(express.static('resources/web'))

// SPA fallback
app.get('*', (req, res) => {
  res.sendFile('resources/web/index.html')
})

Configuration

yaml
web:
  enabled: true
  port: 3000
  host: "127.0.0.1"

Next Steps

Released under the MIT License