refactor: move

This commit is contained in:
xiaojunnuo
2021-02-08 00:21:36 +08:00
parent cfb1034450
commit 82f86d9556
150 changed files with 14691 additions and 2059 deletions

View File

@@ -0,0 +1,16 @@
module.exports = {
root: true,
parserOptions: {
sourceType: 'module',
ecmaVersion: '2020'
},
parser: 'babel-eslint',
extends: ['standard'],
env: {
node: true
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
}
}

View File

@@ -0,0 +1,53 @@
import Koa from 'koa'
import json from 'koa-json'
import onerror from 'koa-onerror'
import bodyparser from 'koa-bodyparser'
import logger from 'koa-logger'
import Static from 'koa-static'
import fs from 'fs'
import _ from 'lodash-es'
const app = new Koa()
// error handler
onerror(app)
// middlewares
app.use(bodyparser({
enableTypes: ['json', 'form', 'text']
}))
app.use(json())
app.use(logger())
app.use(Static(new URL('public', import.meta.url).pathname))
// logger
app.use(async (ctx, next) => {
const start = new Date()
await next()
const ms = new Date() - start
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))
// 过滤出.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))
mapping = mapping.default
app.use(mapping.routes(), mapping.allowedMethods())
})
// error-handling
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
})
console.log('http://localhost:3000/')
export default app

View File

@@ -0,0 +1,93 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
import app from '../app.js';
import debuger from 'debug'
const debug = debuger('demo:serer')
// require('debug')('demo:server');
import http from 'http';
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
// app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app.callback());
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

View File

@@ -0,0 +1,19 @@
import Router from 'koa-router'
import { accessProviderRegistry } from '@certd/api'
import DefaultAccessProviders from '@certd/access-providers'
import _ from 'lodash-es'
import { Ret } from '../models/Ret.js'
const router = Router()
router.prefix('/access-providers')
DefaultAccessProviders.install()
router.get('/list', function (ctx, next) {
const list = []
_.forEach(accessProviderRegistry.collection, item => {
list.push(item.define())
})
ctx.body = Ret.success(list)
})
export default router

View File

@@ -0,0 +1,19 @@
import Router from 'koa-router'
import { dnsProviderRegistry } from '@certd/api'
import DefaultDnsProviders from '@certd/dns-providers'
import _ from 'lodash-es'
import { Ret } from '../models/Ret.js'
const router = Router()
router.prefix('/dns-providers')
DefaultDnsProviders.install()
router.get('/list', function (ctx, next) {
const list = []
_.forEach(dnsProviderRegistry.collection, item => {
list.push(item.define())
})
ctx.body = Ret.success(list)
})
export default router

View File

@@ -0,0 +1,23 @@
import Router from 'koa-router'
import fs from 'fs'
import exportsService from '../service/exports-service.js'
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.options, '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

View File

@@ -0,0 +1,10 @@
import Router from 'koa-router'
const router = Router()
router.get('/', async (ctx, next) => {
await ctx.render('index', {
title: 'Hello CertD!'
})
})
export default router

View File

@@ -0,0 +1,19 @@
import Router from 'koa-router'
import { pluginRegistry } from '@certd/api'
import DefaultPlugins from '@certd/plugins'
import _ from 'lodash-es'
import { Ret } from '../models/Ret.js'
const router = Router()
router.prefix('/plugins')
DefaultPlugins.install()
router.get('/list', function (ctx, next) {
const list = []
_.forEach(pluginRegistry.collection, item => {
list.push(item.define())
})
ctx.body = Ret.success(list)
})
export default router

View File

@@ -0,0 +1,15 @@
export class Ret {
constructor (code = 0, msg, data) {
this.code = code
this.msg = msg
this.data = data
}
static success (data) {
return new Ret(0, '', data)
}
static error (msg) {
return new Ret(1, msg)
}
}

5694
packages/ui/certd-server/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,42 @@
{
"name": "@certd/server",
"version": "0.1.14",
"private": false,
"type": "module",
"scripts": {
"start": "node bin/www.js",
"dev": "./node_modules/.bin/nodemon bin/www.js",
"prd": "pm2 start bin/www.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@certd/access-providers": "^0.1.13",
"@certd/api": "^0.1.13",
"@certd/dns-providers": "^0.1.13",
"@certd/executor": "^0.1.14",
"@certd/plugins": "^0.1.13",
"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",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.0",
"koa-onerror": "^4.1.0",
"koa-router": "^7.4.0",
"koa-static": "^5.0.0",
"koa-views": "^6.2.0",
"lodash-es": "^4.17.20"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^7.18.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"nodemon": "^1.19.1"
},
"gitHead": "4a421d5b142d453203c68ce6d1036e168ea2455b"
}

View File

@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}

View File

@@ -0,0 +1,51 @@
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'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
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)
console.log('targetDir', targetDir)
console.log('projectName', projectName)
console.log('tempalteDir', templateDir)
console.log('targetProjectDir', targetProjectDir)
fs.copySync(templateDir, targetProjectDir)
// options
const optionsFilePath = path.join(targetProjectDir, 'options.json')
fs.writeJsonSync(optionsFilePath, options)
// 依赖版本
const exePkgJson = fs.readFileSync('node_modules/@certd/executor/package.json')
const executorPkg = JSON.parse(exePkgJson)
const currentVersion = executorPkg.version
const templatePkg = require('../templates/certd-run/package.json')
templatePkg.dependencies['@certd/executor'] = '^' + currentVersion
const pkgFilePath = path.join(targetProjectDir, 'package.json')
fs.writeJsonSync(pkgFilePath, templatePkg)
const zipName = dirName + '.zip'
const outputFilePath = path.join(targetDir, zipName)
console.log('outputFilePath', outputFilePath)
await zipUtil.compress({ dir: targetProjectDir, output: outputFilePath })
return {
dir: targetDir,
fileName: zipName,
zipPath: outputFilePath
}
}
}

View File

@@ -0,0 +1,4 @@
import { Executor } from '@certd/executor'
import options from './options.json'
const executor = new Executor()
executor.run(options)

View File

@@ -0,0 +1 @@
{}

View File

@@ -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"
}
}

View File

@@ -0,0 +1,12 @@
import os from 'os'
export default {
join (...dirs) {
const url = new URL('../' + dirs.join('/'), import.meta.url)
console.log('url', url)
let path = url.pathname
if (os.type() === 'Windows_NT') {
path = path.substring(1)
}
return path
}
}

View File

@@ -0,0 +1,8 @@
import compressing from 'compressing'
export default {
compress ({
dir, output
}) {
return compressing.zip.compressDir(dir, output)
}
}