feat(dayjs): integrate dayjs for date handling and formatting across the application refactor(routes): update date handling in id-tags, transactions, users, and dashboard routes to use dayjs style(globals): improve CSS variable definitions for better readability and consistency deps: add dayjs as a dependency for date manipulation
106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
import 'dotenv/config'
|
|
import { serve } from '@hono/node-server'
|
|
import { Hono } from 'hono'
|
|
import { createNodeWebSocket } from '@hono/node-ws'
|
|
import { getConnInfo } from '@hono/node-server/conninfo'
|
|
import { cors } from 'hono/cors'
|
|
import { logger } from 'hono/logger'
|
|
import { showRoutes } from 'hono/dev'
|
|
import { auth } from './lib/auth.ts'
|
|
import { createOcppHandler } from './ocpp/handler.ts'
|
|
import statsRoutes from './routes/stats.ts'
|
|
import statsChartRoutes from './routes/stats-chart.ts'
|
|
import chargePointRoutes from './routes/charge-points.ts'
|
|
import transactionRoutes from './routes/transactions.ts'
|
|
import idTagRoutes from './routes/id-tags.ts'
|
|
import userRoutes from './routes/users.ts'
|
|
import setupRoutes from './routes/setup.ts'
|
|
|
|
import type { HonoEnv } from './types/hono.ts'
|
|
|
|
const app = new Hono<HonoEnv>()
|
|
|
|
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app })
|
|
|
|
app.use(logger())
|
|
|
|
app.use('*', async (c, next) => {
|
|
const session = await auth.api.getSession({ headers: c.req.raw.headers })
|
|
if (!session) {
|
|
c.set('user', null)
|
|
c.set('session', null)
|
|
await next()
|
|
return
|
|
}
|
|
c.set('user', session.user)
|
|
c.set('session', session.session)
|
|
await next()
|
|
})
|
|
|
|
app.use(
|
|
'/api/*',
|
|
cors({
|
|
origin: process.env.WEB_ORIGIN ?? '*',
|
|
allowMethods: ['GET', 'POST', 'PATCH', 'DELETE', 'OPTIONS'],
|
|
allowHeaders: ['Content-Type', 'Authorization'],
|
|
exposeHeaders: ['Content-Length'],
|
|
credentials: true,
|
|
}),
|
|
)
|
|
|
|
app.on(['POST', 'GET'], '/api/auth/*', (c) => auth.handler(c.req.raw))
|
|
|
|
// REST API routes
|
|
app.route('/api/stats', statsRoutes)
|
|
app.route('/api/stats/chart', statsChartRoutes)
|
|
app.route('/api/charge-points', chargePointRoutes)
|
|
app.route('/api/transactions', transactionRoutes)
|
|
app.route('/api/id-tags', idTagRoutes)
|
|
app.route('/api/users', userRoutes)
|
|
app.route('/api/setup', setupRoutes)
|
|
|
|
app.get('/api', (c) => {
|
|
const user = c.get('user')
|
|
const session = c.get('session')
|
|
|
|
const payload = {
|
|
platform: 'Helios CSMS',
|
|
message: 'ok',
|
|
}
|
|
|
|
if (user) {
|
|
Object.assign(payload, { user })
|
|
}
|
|
|
|
if (session) {
|
|
Object.assign(payload, { session })
|
|
}
|
|
|
|
return c.json(payload)
|
|
})
|
|
|
|
app.get(
|
|
'/ocpp/:chargePointId',
|
|
upgradeWebSocket((c) => {
|
|
const chargePointId = c.req.param('chargePointId')
|
|
const connInfo = getConnInfo(c)
|
|
return createOcppHandler(chargePointId, connInfo.remote.address)
|
|
}),
|
|
)
|
|
|
|
showRoutes(app, {
|
|
verbose: true,
|
|
})
|
|
|
|
const server = serve(
|
|
{
|
|
fetch: app.fetch,
|
|
port: 3001,
|
|
},
|
|
(info) => {
|
|
console.log(`Server is running on http://localhost:${info.port}`)
|
|
},
|
|
)
|
|
|
|
injectWebSocket(server)
|