|
| 1 | +import { Request, Response } from 'express' |
| 2 | +import { tunnelStartSchema } from './Tunnel.validator' |
| 3 | +import tunnelService from './Tunnel.service' |
| 4 | +import { SendResponse } from '../../../utils/SendResponse.utils' |
| 5 | +import TunnelConstant from './Tunnel.constant' |
| 6 | +import { StatusConstant } from '../../../constant/Status.constant' |
| 7 | + |
| 8 | +class TunnelController { |
| 9 | + constructor() { |
| 10 | + this.startTunnel = this.startTunnel.bind(this) |
| 11 | + this.stopTunnel = this.stopTunnel.bind(this) |
| 12 | + this.getTunnelStatus = this.getTunnelStatus.bind(this) |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Start a cloudflared tunnel |
| 17 | + * POST /api/v1/expose/cloudflared |
| 18 | + */ |
| 19 | + async startTunnel(req: Request, res: Response): Promise<void> { |
| 20 | + try { |
| 21 | + const validatedData = await tunnelStartSchema.parseAsync(req.body) |
| 22 | + |
| 23 | + const result = await tunnelService.startTunnel(validatedData.port, validatedData.subdomain) |
| 24 | + |
| 25 | + SendResponse.success(res, TunnelConstant.TUNNEL_STARTED, result, StatusConstant.CREATED) |
| 26 | + } catch (err: any) { |
| 27 | + if (err?.name === 'ZodError') { |
| 28 | + SendResponse.error( |
| 29 | + res, |
| 30 | + err.errors?.[0]?.message || TunnelConstant.INVALID_PORT, |
| 31 | + StatusConstant.BAD_REQUEST, |
| 32 | + err |
| 33 | + ) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + if (err.message === TunnelConstant.TUNNEL_ALREADY_RUNNING) { |
| 38 | + SendResponse.error(res, err.message, StatusConstant.CONFLICT, err) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + if (err.message === TunnelConstant.CLOUDFLARED_NOT_INSTALLED) { |
| 43 | + SendResponse.error(res, err.message, StatusConstant.SERVICE_UNAVAILABLE, err) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + SendResponse.error( |
| 48 | + res, |
| 49 | + err.message || TunnelConstant.TUNNEL_START_FAILED, |
| 50 | + StatusConstant.INTERNAL_SERVER_ERROR, |
| 51 | + err |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Stop the active tunnel |
| 58 | + * DELETE /api/v1/expose/cloudflared/stop |
| 59 | + */ |
| 60 | + async stopTunnel(req: Request, res: Response): Promise<void> { |
| 61 | + try { |
| 62 | + const result = await tunnelService.stopTunnel() |
| 63 | + |
| 64 | + SendResponse.success(res, result.message, { previousUrl: result.previousUrl }, StatusConstant.OK) |
| 65 | + } catch (err: any) { |
| 66 | + if (err.message === TunnelConstant.TUNNEL_NOT_RUNNING) { |
| 67 | + SendResponse.error(res, err.message, StatusConstant.NOT_FOUND, err) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + SendResponse.error( |
| 72 | + res, |
| 73 | + err.message || TunnelConstant.TUNNEL_STOP_FAILED, |
| 74 | + StatusConstant.INTERNAL_SERVER_ERROR, |
| 75 | + err |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get tunnel status |
| 82 | + * GET /api/v1/expose/cloudflared/status |
| 83 | + */ |
| 84 | + async getTunnelStatus(req: Request, res: Response): Promise<void> { |
| 85 | + try { |
| 86 | + const status = tunnelService.getTunnelStatus() |
| 87 | + |
| 88 | + SendResponse.success(res, TunnelConstant.TUNNEL_STATUS_RETRIEVED, status, StatusConstant.OK) |
| 89 | + } catch (err: any) { |
| 90 | + SendResponse.error( |
| 91 | + res, |
| 92 | + err.message || TunnelConstant.TUNNEL_ERROR, |
| 93 | + StatusConstant.INTERNAL_SERVER_ERROR, |
| 94 | + err |
| 95 | + ) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export default new TunnelController() |
0 commit comments