1
0
forked from noxious/server

64 lines
2.5 KiB
TypeScript

import cors from 'cors'
import type { Application } from 'express'
import config from '#application/config'
import { AuthController } from '#controllers/auth'
import { AvatarController } from '#controllers/avatar'
import { CacheController } from '#controllers/cache'
import { TexturesController } from '#controllers/textures'
/**
* HTTP manager
*/
class HttpManager {
private readonly authController: AuthController = new AuthController()
private readonly avatarController: AvatarController = new AvatarController()
private readonly texturesController: TexturesController = new TexturesController()
private readonly cacheController: CacheController = new CacheController()
/**
* Initialize HTTP manager
* @param app
*/
public async boot(app: Application) {
// Add CORS middleware
app.use(
cors({
origin: config.CLIENT_URL,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], // Add supported methods
allowedHeaders: ['Content-Type', 'Authorization'], // Add allowed headers
credentials: true
})
)
// Add routes
await this.addRoutes(app)
}
private async addRoutes(app: Application) {
// Auth routes
app.post('/login', (req, res) => this.authController.login(req, res))
app.post('/register', (req, res) => this.authController.register(req, res))
app.post('/reset-password-request', (req, res) => this.authController.requestPasswordReset(req, res))
app.post('/reset-password', (req, res) => this.authController.resetPassword(req, res))
// Avatar routes
app.get('/avatar/:characterName', (req, res) => this.avatarController.getByName(req, res))
app.get('/avatar/s/:characterTypeId/:characterHairId?', (req, res) => this.avatarController.getByParams(req, res))
// Download texture file
app.get('/textures/:type/:spriteId?/:file', (req, res) => this.texturesController.download(req, res))
// Cache routes
app.get('/cache/tiles', (req, res) => this.cacheController.tiles(req, res))
app.get('/cache/maps', (req, res) => this.cacheController.maps(req, res))
app.get('/cache/map_objects', (req, res) => this.cacheController.mapObjects(req, res))
app.get('/cache/sprites', (req, res) => this.cacheController.sprites(req, res))
app.get('/cache/character_types', (req, res) => this.cacheController.characterTypes(req, res))
app.get('/cache/character_hair', (req, res) => this.cacheController.characterHair(req, res))
}
}
export default new HttpManager()