refactor: 重构优化

This commit is contained in:
xiaojunnuo
2021-02-04 18:44:16 +08:00
parent a39dac4dbd
commit a25a15ca6e
59 changed files with 3903 additions and 967 deletions
+5 -7
View File
@@ -1,5 +1,4 @@
import Koa from 'koa'
import views from 'koa-views'
import json from 'koa-json'
import onerror from 'koa-onerror'
import bodyparser from 'koa-bodyparser'
@@ -7,6 +6,7 @@ import logger from 'koa-logger'
import Static from 'koa-static'
import fs from 'fs'
import _ from 'lodash-es'
const app = new Koa()
// error handler
@@ -21,10 +21,6 @@ app.use(logger())
app.use(Static(new URL('public', import.meta.url).pathname))
app.use(views(new URL('views', import.meta.url).pathname, {
extension: 'pug'
}))
// logger
app.use(async (ctx, next) => {
const start = new Date()
@@ -33,15 +29,17 @@ app.use(async (ctx, next) => {
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})
console.log('url', import.meta.url)
// routes
const files = fs.readdirSync(new URL('controllers', import.meta.url).pathname)
const files = fs.readdirSync(new URL('controllers/', import.meta.url))
// 过滤出.js文件:
const jsFiles = files.filter((f) => {
return f.endsWith('.js')
})
_.forEach(jsFiles, async item => {
let mapping = await import(new URL('controllers/' + item, import.meta.url).pathname)
let mapping = await import(new URL('controllers/' + item, import.meta.url))
mapping = mapping.default
app.use(mapping.routes(), mapping.allowedMethods())
})
+23
View File
@@ -0,0 +1,23 @@
import Router from 'koa-router'
import fs from 'fs'
import exportsService from '../service/exports-service.js'
// import executorPkg from '@certd/executor/package.json'
const router = Router()
router.prefix('/exports')
router.post('/toZip', async function (ctx, next) {
// const request = ctx.request
// const query = request.query
const body = ctx.request.body
// const req_queryString = request.queryString
const { zipPath, fileName } = await exportsService.exportsToZip(body, 'certd-run')
console.log('zipFile', zipPath)
ctx.set('Content-disposition', 'attachment;filename=' + fileName)
ctx.set('Content-Type', 'application/zip')
ctx.body = fs.createReadStream(zipPath)
//
// // ctx.body = Ret.success(zipPath)
})
export default router
+1 -1
View File
@@ -1,6 +1,6 @@
import Router from 'koa-router'
import { providerRegistry } from '@certd/api'
import DefaultProviders from '@certd/providers'
import DefaultProviders from '@certd/dns-providers'
import _ from 'lodash-es'
import { Ret } from '../models/Ret.js'
const router = Router()
+541 -715
View File
File diff suppressed because it is too large Load Diff
+6 -4
View File
@@ -10,10 +10,13 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@certd/plugins": "^0.1.11",
"@certd/providers": "^0.1.11",
"@certd/api": "^0.1.11",
"@certd/executor": "^0.1.11",
"@certd/plugins": "^0.1.11",
"@certd/dns-providers": "^0.1.11",
"compressing": "^1.5.1",
"debug": "^4.1.1",
"fs-extra": "^9.1.0",
"koa": "^2.7.0",
"koa-bodyparser": "^4.2.1",
"koa-convert": "^1.2.0",
@@ -23,8 +26,7 @@
"koa-router": "^7.4.0",
"koa-static": "^5.0.0",
"koa-views": "^6.2.0",
"lodash-es": "^4.17.20",
"pug": "^2.0.3"
"lodash-es": "^4.17.20"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
@@ -0,0 +1,39 @@
import os from 'os'
import fs from 'fs-extra'
import pathUtil from '../utils/util.path.js'
import cryptoRandomString from 'crypto-random-string'
import zipUtil from '../utils/util.zip.js'
import path from 'path'
export default {
async exportsToZip (options, dirName) {
const tempDir = os.tmpdir()
const targetDir = path.join(tempDir, 'certd-server', cryptoRandomString(10))
const projectName = dirName
const targetProjectDir = path.join(targetDir, projectName)
const templateDir = pathUtil.join('templates/' + projectName)
fs.copySync(templateDir, targetProjectDir)
// const packageFilePath = path.join(targetProjectDir, 'package.json')
const optionsFilePath = path.join(targetProjectDir, 'options.json')
fs.writeJsonSync(optionsFilePath, options)
const zipName = dirName + '.zip'
const outputFilePath = path.join(targetDir, zipName)
console.log('targetDir', targetDir)
console.log('projectName', projectName)
console.log('tempalteDir', templateDir)
console.log('targetProjectDir', targetProjectDir)
console.log('outputFilePath', outputFilePath)
await zipUtil.compress({ dir: targetProjectDir, output: outputFilePath })
return {
dir: targetDir,
fileName: zipName,
zipPath: outputFilePath
}
}
}
@@ -0,0 +1,4 @@
import { Executor } from '@certd/executor'
import options from './options.json'
const executor = new Executor()
executor.run(options)
@@ -0,0 +1 @@
{}
@@ -0,0 +1,19 @@
{
"name": "certd-run",
"version": "1.0.0",
"description": "certd run",
"main": "index.js",
"scripts": {
"certd": "node index.js"
},
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/certd/certd"
},
"author": "greper",
"license": "MIT",
"dependencies": {
"@certd/executor": "^0.1.11"
}
}
+6
View File
@@ -0,0 +1,6 @@
export default {
join (...dirs) {
const url = new URL('../' + dirs.join('/'), import.meta.url)
return url.href.replace(/^file:\/\/\//, '').replace(/^file:\/\//, '')
}
}
+8
View File
@@ -0,0 +1,8 @@
import compressing from 'compressing'
export default {
compress ({
dir, output
}) {
return compressing.zip.compressDir(dir, output)
}
}
-6
View File
@@ -1,6 +0,0 @@
extends layout
block content
h1= message
h2= error.status
pre #{error.stack}
-5
View File
@@ -1,5 +0,0 @@
extends layout
block content
h1= title
p Welcome to #{title}
-7
View File
@@ -1,7 +0,0 @@
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content