Commit ab8a7a25 authored by rockyl's avatar rockyl

增加eureka

parent 3bc0aee9
# node
# Version v10.8
FROM harbor.dui88.com/library/node10:cat
LABEL MAINTAINER op@duiba.com.cn
ARG appname
ARG NODE_ENV
RUN echo "export LANG=en_US.UTF-8" && echo "Asia/Shanghai" > /etc/timezone
# RUN npm install pm2 -g --register=https://registry.npm.taobao.org/
ENV LANG='en_US.UTF-8'
RUN mkdir /root/duiba-deploy/
ADD ./duiba-deploy /root/duiba-deploy/
WORKDIR /root/duiba-deploy/
#define entry point which will be run first when the container starts up
ENTRYPOINT npm start
...@@ -6,12 +6,57 @@ var restify = _interopDefault(require('restify')); ...@@ -6,12 +6,57 @@ var restify = _interopDefault(require('restify'));
var imagemin = _interopDefault(require('imagemin')); var imagemin = _interopDefault(require('imagemin'));
var imageminJpegtran = _interopDefault(require('imagemin-jpegtran')); var imageminJpegtran = _interopDefault(require('imagemin-jpegtran'));
var imageminPngquant = _interopDefault(require('imagemin-pngquant')); var imageminPngquant = _interopDefault(require('imagemin-pngquant'));
var Eurekaclient = _interopDefault(require('eureka-client-ts'));
var log4js = require('log4js');
var path = _interopDefault(require('path'));
var fs = _interopDefault(require('fs-extra'));
/**
* Created by rockyl on 2020-02-13.
*/
const logPath = path.join(process.env.HOME || process.env.USERPROFILE, 'logs', 'tiny-image');
fs.ensureDir(logPath);
log4js.configure({
appenders: {
out: { type: 'console' },
allLog: { type: 'file', filename: path.join(logPath, 'log.log'), keepFileExt: true, maxLogSize: 10485760, backups: 3 },
},
categories: {
default: { appenders: ['out', 'allLog'], level: 'debug' },
}
});
const logger = log4js.getLogger();
const eurekaConfig = {
appName: 'tiny-image',
services: ['tiny-image'],
};
async function getEurekaClient(port) {
eurekaConfig.port = port;
let eureka;
try {
eureka = new Eurekaclient(eurekaConfig);
await eureka.start();
} catch (e) {
logger.error(e);
}
return eureka;
}
/** /**
* Created by rockyl on 2020-02-12. * Created by rockyl on 2020-02-12.
*/ */
let [_, __, port = '8090'] = process.argv; let [_, __, port = '8090'] = process.argv;
let eureka;
port = parseInt(port);
const acceptTypes = ['image/png', 'image/jpeg']; const acceptTypes = ['image/png', 'image/jpeg'];
...@@ -62,7 +107,24 @@ server.post('/tinify', async function (req, res, next) { ...@@ -62,7 +107,24 @@ server.post('/tinify', async function (req, res, next) {
return next(); return next();
}); });
server.listen(parseInt(port), function () { process.on('uncaughtException', (err) => {
console.log('%s listening at %s', server.name, server.url); logger.error(err);
});
process.on("SIGTERM", () => {
eureka.stop();
setTimeout(() => {
process.exit(1);
}, 6000);
});
server.listen(port, async function () {
try {
eureka = await getEurekaClient(port);
logger.info('eureka register success');
} catch (e) {
logger.error(e.name);
}
logger.info('%s listening at %s', server.name, server.url);
}); });
//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map
{"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["/**\n * Created by rockyl on 2020-02-12.\n */\n\nimport restify from 'restify';\nimport imagemin from 'imagemin';\nimport imageminJpegtran from 'imagemin-jpegtran';\nimport imageminPngquant from 'imagemin-pngquant';\n\nlet [_, __, port = '8090'] = process.argv;\n\nconst acceptTypes = ['image/png', 'image/jpeg'];\n\nconst server = restify.createServer({\n\tname: 'tiny-image',\n\tversion: '1.0.0'\n});\n\nserver.use(restify.plugins.acceptParser(server.acceptable));\nserver.use(restify.plugins.queryParser());\nserver.use(restify.plugins.bodyParser());\n\nserver.get('/echo/:name', function (req, res, next) {\n\tres.send(req.params);\n\treturn next();\n});\n\nserver.post('/tinify', async function (req, res, next) {\n\tlet file = req.files.file;\n\n\tif(acceptTypes.includes(file.type)){\n\t\ttry {\n\t\t\tconst buffer = await imagemin([file.path], {\n\t\t\t\tplugins: [\n\t\t\t\t\timageminJpegtran(),\n\t\t\t\t\timageminPngquant({\n\t\t\t\t\t\tquality: [0.6, 0.8]\n\t\t\t\t\t})\n\t\t\t\t]\n\t\t\t});\n\n\t\t\tres.sendRaw(buffer[0].data, {\n\t\t\t\t'Content-Type': file.type,\n\t\t\t});\n\t\t}catch (e) {\n\t\t\tres.send({\n\t\t\t\tcode: 2,\n\t\t\t\tmsg: 'compress failed'\n\t\t\t});\n\t\t}\n\t}else{\n\t\tres.send({\n\t\t\tcode: 1,\n\t\t\tmsg: 'type error'\n\t\t});\n\t}\n\n\treturn next();\n});\n\nserver.listen(parseInt(port), function () {\n\tconsole.log('%s listening at %s', server.name, server.url);\n});\n"],"names":[],"mappings":";;;;;;;;;AAAA;;;AAGA,AAKA;AACA,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;;AAE1C,MAAM,WAAW,GAAG,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;;AAEhD,MAAM,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CACnC,IAAI,EAAE,YAAY;CAClB,OAAO,EAAE,OAAO;CAChB,CAAC,CAAC;;AAEH,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;AAC5D,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;AAC1C,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;;AAEzC,MAAM,CAAC,GAAG,CAAC,aAAa,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE;CACnD,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;CACrB,OAAO,IAAI,EAAE,CAAC;CACd,CAAC,CAAC;;AAEH,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,iBAAiB,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE;CACvD,IAAI,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC;;CAE1B,GAAG,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;EAClC,IAAI;GACH,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;IAC1C,OAAO,EAAE;KACR,gBAAgB,EAAE;KAClB,gBAAgB,CAAC;MAChB,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;MACnB,CAAC;KACF;IACD,CAAC,CAAC;;GAEH,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE;IAC3B,cAAc,EAAE,IAAI,CAAC,IAAI;IACzB,CAAC,CAAC;GACH,OAAO,CAAC,EAAE;GACV,GAAG,CAAC,IAAI,CAAC;IACR,IAAI,EAAE,CAAC;IACP,GAAG,EAAE,iBAAiB;IACtB,CAAC,CAAC;GACH;EACD,IAAI;EACJ,GAAG,CAAC,IAAI,CAAC;GACR,IAAI,EAAE,CAAC;GACP,GAAG,EAAE,YAAY;GACjB,CAAC,CAAC;EACH;;CAED,OAAO,IAAI,EAAE,CAAC;CACd,CAAC,CAAC;;AAEH,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,YAAY;CACzC,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;CAC3D,CAAC,CAAC"} {"version":3,"file":"index.js","sources":["../src/logger.js","../src/eureka.js","../src/index.js"],"sourcesContent":["/**\n * Created by rockyl on 2020-02-13.\n */\n\nimport { configure, getLogger } from 'log4js';\nimport path from \"path\";\nimport fs from \"fs-extra\";\n\nconst logPath = path.join(process.env.HOME || process.env.USERPROFILE, 'logs', 'tiny-image');\n\nfs.ensureDir(logPath);\n\nconfigure({\n\tappenders: {\n\t\tout: { type: 'console' },\n\t\tallLog: { type: 'file', filename: path.join(logPath, 'log.log'), keepFileExt: true, maxLogSize: 10485760, backups: 3 },\n\t},\n\tcategories: {\n\t\tdefault: { appenders: ['out', 'allLog'], level: 'debug' },\n\t}\n\n});\n\nconst logger = getLogger();\n\nexport default logger;","import Eurekaclient from 'eureka-client-ts';\nimport logger from './logger';\n\nconst eurekaConfig = {\n appName: 'tiny-image',\n services: ['tiny-image'],\n};\n\nexport async function getEurekaClient(port) {\n eurekaConfig.port = port;\n let eureka;\n try {\n eureka = new Eurekaclient(eurekaConfig);\n await eureka.start();\n } catch (e) {\n logger.error(e);\n }\n\n return eureka;\n}\n","/**\n * Created by rockyl on 2020-02-12.\n */\n\nimport restify from 'restify';\nimport imagemin from 'imagemin';\nimport imageminJpegtran from 'imagemin-jpegtran';\nimport imageminPngquant from 'imagemin-pngquant';\nimport {getEurekaClient} from \"./eureka\";\nimport logger from \"./logger\";\n\nlet [_, __, port = '8090'] = process.argv;\nlet eureka;\nport = parseInt(port);\n\nconst acceptTypes = ['image/png', 'image/jpeg'];\n\nconst server = restify.createServer({\n\tname: 'tiny-image',\n\tversion: '1.0.0'\n});\n\nserver.use(restify.plugins.acceptParser(server.acceptable));\nserver.use(restify.plugins.queryParser());\nserver.use(restify.plugins.bodyParser());\n\nserver.get('/echo/:name', function (req, res, next) {\n\tres.send(req.params);\n\treturn next();\n});\n\nserver.post('/tinify', async function (req, res, next) {\n\tlet file = req.files.file;\n\n\tif(acceptTypes.includes(file.type)){\n\t\ttry {\n\t\t\tconst buffer = await imagemin([file.path], {\n\t\t\t\tplugins: [\n\t\t\t\t\timageminJpegtran(),\n\t\t\t\t\timageminPngquant({\n\t\t\t\t\t\tquality: [0.6, 0.8]\n\t\t\t\t\t})\n\t\t\t\t]\n\t\t\t});\n\n\t\t\tres.sendRaw(buffer[0].data, {\n\t\t\t\t'Content-Type': file.type,\n\t\t\t});\n\t\t}catch (e) {\n\t\t\tres.send({\n\t\t\t\tcode: 2,\n\t\t\t\tmsg: 'compress failed'\n\t\t\t});\n\t\t}\n\t}else{\n\t\tres.send({\n\t\t\tcode: 1,\n\t\t\tmsg: 'type error'\n\t\t});\n\t}\n\n\treturn next();\n});\n\nprocess.on('uncaughtException', (err) => {\n\tlogger.error(err);\n});\n\nprocess.on(\"SIGTERM\", () => {\n\teureka.stop();\n\tsetTimeout(() => {\n\t\tprocess.exit(1)\n\t}, 6000);\n});\n\nserver.listen(port, async function () {\n\ttry {\n\t\teureka = await getEurekaClient(port);\n\t\tlogger.info('eureka register success')\n\t} catch (e) {\n\t\tlogger.error(e.name);\n\t}\n\tlogger.info('%s listening at %s', server.name, server.url);\n});\n"],"names":["configure","getLogger"],"mappings":";;;;;;;;;;;;;AAAA;;;AAGA,AAIA;AACA,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;;AAE7F,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;;AAEtBA,gBAAS,CAAC;CACT,SAAS,EAAE;EACV,GAAG,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;EACxB,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,EAAE;EACtH;CACD,UAAU,EAAE;EACX,OAAO,EAAE,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE;EACzD;;CAED,CAAC,CAAC;;AAEH,MAAM,MAAM,GAAGC,gBAAS,EAAE,CAAC;;ACpB3B,MAAM,YAAY,GAAG;EACnB,OAAO,EAAE,YAAY;EACrB,QAAQ,EAAE,CAAC,YAAY,CAAC;CACzB,CAAC;;AAEF,AAAO,eAAe,eAAe,CAAC,IAAI,EAAE;EAC1C,YAAY,CAAC,IAAI,GAAG,IAAI,CAAC;EACzB,IAAI,MAAM,CAAC;EACX,IAAI;IACF,MAAM,GA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,MAAM,CAAC,IAAI,CAAC,oBAAoB,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;CAC3D,CAAC,CAAC"}
\ No newline at end of file \ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<module type="WEB_MODULE" version="4"> <module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$/../duiba-tiny-image-node" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>
\ No newline at end of file
This diff is collapsed.
{ {
"name": "tiny-image", "name": "duiba-tiny-image-node",
"version": "1.0.0", "version": "1.0.0",
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"eureka-client-ts": "^0.2.10",
"fs-extra": "^8.1.0",
"imagemin": "^7.0.1", "imagemin": "^7.0.1",
"imagemin-jpegtran": "^6.0.0", "imagemin-jpegtran": "^6.0.0",
"imagemin-pngquant": "^8.0.0", "imagemin-pngquant": "^8.0.0",
"jpegtran-bin": "^4.0.0", "jpegtran-bin": "^4.0.0",
"log4js": "^6.1.2",
"restify": "^8.5.1" "restify": "^8.5.1"
}, },
"scripts": { "scripts": {
"dev": "rollup -c -w", "dev": "rollup -c -w",
"build": "rollup -c", "build": "rollup -c",
"server": "node dist/index.js" "start": "node dist/index.js"
}, },
"devDependencies": { "devDependencies": {
"rollup-plugin-progress": "^1.1.1" "rollup-plugin-progress": "^1.1.1"
......
import Eurekaclient from 'eureka-client-ts';
import logger from './logger';
const eurekaConfig = {
appName: 'tiny-image',
services: ['tiny-image'],
};
export async function getEurekaClient(port) {
eurekaConfig.port = port;
let eureka;
try {
eureka = new Eurekaclient(eurekaConfig);
await eureka.start();
} catch (e) {
logger.error(e);
}
return eureka;
}
...@@ -6,8 +6,12 @@ import restify from 'restify'; ...@@ -6,8 +6,12 @@ import restify from 'restify';
import imagemin from 'imagemin'; import imagemin from 'imagemin';
import imageminJpegtran from 'imagemin-jpegtran'; import imageminJpegtran from 'imagemin-jpegtran';
import imageminPngquant from 'imagemin-pngquant'; import imageminPngquant from 'imagemin-pngquant';
import {getEurekaClient} from "./eureka";
import logger from "./logger";
let [_, __, port = '8090'] = process.argv; let [_, __, port = '8090'] = process.argv;
let eureka;
port = parseInt(port);
const acceptTypes = ['image/png', 'image/jpeg']; const acceptTypes = ['image/png', 'image/jpeg'];
...@@ -58,6 +62,23 @@ server.post('/tinify', async function (req, res, next) { ...@@ -58,6 +62,23 @@ server.post('/tinify', async function (req, res, next) {
return next(); return next();
}); });
server.listen(parseInt(port), function () { process.on('uncaughtException', (err) => {
console.log('%s listening at %s', server.name, server.url); logger.error(err);
});
process.on("SIGTERM", () => {
eureka.stop();
setTimeout(() => {
process.exit(1)
}, 6000);
});
server.listen(port, async function () {
try {
eureka = await getEurekaClient(port);
logger.info('eureka register success')
} catch (e) {
logger.error(e.name);
}
logger.info('%s listening at %s', server.name, server.url);
}); });
/**
* Created by rockyl on 2020-02-13.
*/
import { configure, getLogger } from 'log4js';
import path from "path";
import fs from "fs-extra";
const logPath = path.join(process.env.HOME || process.env.USERPROFILE, 'logs', 'tiny-image');
fs.ensureDir(logPath);
configure({
appenders: {
out: { type: 'console' },
allLog: { type: 'file', filename: path.join(logPath, 'log.log'), keepFileExt: true, maxLogSize: 10485760, backups: 3 },
},
categories: {
default: { appenders: ['out', 'allLog'], level: 'debug' },
}
});
const logger = getLogger();
export default logger;
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment