UNPKG

@qigy/run

Version:

A collection of tools capable of executing complex commands through their abbreviation.

62 lines (60 loc) 2.16 kB
#! /usr/bin/env node const { fastify, fastifyMultipart, fastifyStatic, fastifyCors, fastifyRateLimit } = require('./lib/libs'); const { yellow, green, multipartConfig, staticConfig, corsConfig, rateLimitConfig, port } = require('./lib/utils'); const core = require('./lib/core'); /* 全局错误处理 */ fastify.setErrorHandler((error, _, reply) => { console.error(error); reply.status(500).send({ error: 'Internal Server Error' }); }); /* register */ fastify.register(fastifyMultipart, multipartConfig); fastify.register(fastifyRateLimit, rateLimitConfig); fastify.register(fastifyStatic, staticConfig); fastify.register(fastifyCors, corsConfig); /* static */ fastify.get('/', (_, reply) => reply.sendFile('index.html')); /* list */ fastify.post('/run/list', async (request, reply) => { const result = await core.get(request.body.keyword); reply.send(result); }); /* delete */ fastify.post('/run/delete', async (request, reply) => { const result = await core.del(request.body); reply.send(result); }); /* add */ fastify.post('/run/add', async (request, reply) => { const { alias, command, remark } = request.body; const result = await core.add(alias, command, remark); reply.send(result); }); /* update */ fastify.post('/run/update', async (request, reply) => { const { alias, command, remark } = request.body; const result = await core.update(alias, command, remark); reply.send(result); }); /* import */ fastify.post('/run/import', async (request, reply) => { try { const data = await request.file(); if (data.mimetype !== 'application/json') { return reply.code(400).send({ error: 'Only JSON files are allowed' }); } const buffer = await data.toBuffer(); const rawStr = buffer.toString('utf8'); const result = await core.import(JSON.parse(rawStr)); reply.send(result); } catch (err) { console.error(err); reply.code(400).send({ error: 'File processing failure' }); } }); /* listen */ fastify.listen({ port }, (err) => { if (err) return process.exit(1); console.log(yellow(`The ui server is listening on ${port}`)); console.log(green(`Address: http://127.0.0.1:${port}`)); });