Added ping service
This commit is contained in:
parent
78464fdd4c
commit
4e01083cd6
|
@ -0,0 +1,47 @@
|
|||
import { Request, Response } from 'express'
|
||||
import { makeResponse } from '../libs'
|
||||
import PingService from '../services/ping.service'
|
||||
|
||||
export default class PingController extends PingService {
|
||||
public ping = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { host } = req.query as { host: string }
|
||||
const data = await this.pingHost(host)
|
||||
res.send(makeResponse(data))
|
||||
} catch (err: any) {
|
||||
res.send(makeResponse(err.message, {}, 'Failed', true))
|
||||
}
|
||||
}
|
||||
|
||||
public pingParallel = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { hosts } = req.query as { hosts: string }
|
||||
const data = await this.pingHostsParallel(hosts.split(','))
|
||||
res.send(makeResponse(data))
|
||||
} catch (err: any) {
|
||||
res.send(makeResponse(err.message, {}, 'Failed', true))
|
||||
}
|
||||
}
|
||||
|
||||
public pingAll = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { hosts } = req.query as { hosts: string }
|
||||
const data = await this.pingHosts(hosts.split(','))
|
||||
res.send(makeResponse(data))
|
||||
} catch (err: any) {
|
||||
res.send(makeResponse(err.message, {}, 'Failed', true))
|
||||
}
|
||||
}
|
||||
|
||||
public pingSelf = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const locIps = ['::1', '::ffff:', '127.0.0.1', 'localhost']
|
||||
if (locIps.includes(req.ip))
|
||||
return res.send(makeResponse('localhost', {}, 'Failed', true))
|
||||
const data = await this.pingHost(req.ip)
|
||||
res.send(makeResponse(data))
|
||||
} catch (err: any) {
|
||||
res.send(makeResponse(err.message, {}, 'Failed', true))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
import { Router } from 'express'
|
||||
import PingController from '../../controllers/ping.controller'
|
||||
|
||||
const router = Router()
|
||||
const pingController = new PingController()
|
||||
|
||||
router.get('/', pingController.ping)
|
||||
router.get('/parallel', pingController.pingParallel)
|
||||
router.get('/all', pingController.pingAll)
|
||||
router.get('/self', pingController.pingSelf)
|
||||
|
||||
export default router
|
|
@ -0,0 +1,36 @@
|
|||
import ping from 'ping'
|
||||
|
||||
export default class PingService {
|
||||
public async pingHost(host: string) {
|
||||
try {
|
||||
const response = await ping.promise.probe(host)
|
||||
return response
|
||||
} catch (error) {
|
||||
return error
|
||||
}
|
||||
}
|
||||
|
||||
public async pingHosts(hosts: string[]) {
|
||||
try {
|
||||
const response = await Promise.all(
|
||||
hosts.map((host) => ping.promise.probe(host))
|
||||
)
|
||||
return response
|
||||
} catch (error) {
|
||||
return error
|
||||
}
|
||||
}
|
||||
|
||||
public async pingHostsParallel(hosts: string[]) {
|
||||
try {
|
||||
const response = await Promise.all(
|
||||
hosts.map((host) =>
|
||||
ping.promise.probe(host, { parallel: true })
|
||||
)
|
||||
)
|
||||
return response
|
||||
} catch (error) {
|
||||
return error
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue