ryanemax 8 місяців тому
батько
коміт
da36f00089

+ 1 - 0
docker/.gitignore

@@ -0,0 +1 @@
+/opt/*

+ 109 - 0
docker/Dockerfile

@@ -0,0 +1,109 @@
+FROM debian:bookworm-slim
+MAINTAINER FmodeInc "support@fmode.cn"
+
+ARG NODE_VERSION
+ARG TERM
+ARG CMS_DOMAIN
+ARG CMS_DISABLE
+ARG NGINX_DISABLE
+######################################## ENV LIST
+ENV NODE_VERSION $NODE_VERSION
+ENV TERM $TERM
+ENV CMS_DOMAIN $CMS_DOMAIN
+ENV CMS_DISABLE $CMS_DISABLE
+ENV NGINX_DISABLE $NGINX_DISABLE
+
+# ENV NODE_VERSION 18.15.0
+# ENV NODE_VERSION 16.3.0
+# ENV NODE_SERVER_PATH $nova_server_path
+# ENV NODE_CMS_PATH $nova_cms_path
+
+
+######################################## Copy Latest Nova Cloud
+COPY ./opt/ /opt/
+COPY ./var/ /var/
+
+RUN ls /opt/ && chown -R root:root /opt/*
+
+######################################## APT Repos to 163
+# RUN apt-get update
+
+ADD sources.list /etc/apt/
+
+RUN apt-get update
+
+######################################## Nginx
+# curl http检测
+# procps 进程管理
+RUN apt-get install -y nginx procps && apt-get install -y nginx curl --no-install-recommends \
+  && rm -rf /etc/nginx/sites-enabled/* && cp -rf /opt/nginx/* /etc/nginx/sites-enabled && service nginx restart
+
+######################################## NODE
+
+RUN groupadd --gid 1000 node \
+  && useradd --uid 1000 --gid node --shell /bin/bash --create-home node
+
+RUN apt-get update --fix-missing
+
+RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \
+    && case "${dpkgArch##*-}" in \
+      amd64) ARCH='x64';; \
+      ppc64el) ARCH='ppc64le';; \
+      s390x) ARCH='s390x';; \
+      arm64) ARCH='arm64';; \
+      armhf) ARCH='armv7l';; \
+      i386) ARCH='x86';; \
+      *) echo "unsupported architecture"; exit 1 ;; \
+    esac \
+    && set -ex \
+    # libatomic1 for arm
+    && apt-get update && apt-get install -y ca-certificates apt-utils curl wget gnupg dirmngr xz-utils libatomic1 --no-install-recommends \
+    && rm -rf /var/lib/apt/lists/* \
+    && curl -fsSLO --compressed "https://npmmirror.com/mirrors/node/v$NODE_VERSION/node-v$NODE_VERSION-linux-$ARCH.tar.xz" \
+    && curl -fsSLO --compressed "https://npmmirror.com/mirrors/node/v$NODE_VERSION/SHASUMS256.txt.asc" \
+    && tar -xJf "node-v$NODE_VERSION-linux-$ARCH.tar.xz" -C /usr/local --strip-components=1 --no-same-owner \
+    && rm "node-v$NODE_VERSION-linux-$ARCH.tar.xz" \
+    && ln -s /usr/local/bin/node /usr/local/bin/nodejs
+
+######################################## PM2 && bytenode
+RUN npm config set registry https://registry.npmmirror.com/ && npm i -g pm2@5.1.1 bytenode@1.3.6
+# RUN npm i -g bytenode@1.3.6
+
+######################################## Deploy Latest Nova Cloud
+RUN cd /opt/edu-textbook-server && pm2 start server.js \
+    && sleep 5s \
+    && pm2 startup && pm2 save
+
+######################################## Verify All Depands is OK
+
+    && node --version \
+    && pm2 ls \
+    && node --version \
+    && npm --version \
+
+######################################## Clean All Resources
+RUN apt-mark auto '.*' > /dev/null \
+    && find /usr/local -type f -executable -exec ldd '{}' ';' \
+      | awk '/=>/ { print $(NF-1) }' \
+      | sort -u \
+      | xargs -r dpkg-query --search \
+      | cut -d: -f1 \
+      | sort -u \
+      | xargs -r apt-mark manual \
+    && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
+    && rm -rf /var/log/* \
+    && env
+    # && rm -r /var/log/postgresql/* \
+    # && rm -r /var/log/apt/* \
+    # && rm -r /var/log/nginx/* \
+
+
+# COPY docker-entrypoint.sh /usr/local/bin/
+
+# DEFAULT WEB
+EXPOSE 80
+EXPOSE 61337
+# DEFAULT SSL
+EXPOSE 443
+
+ENTRYPOINT [ "node","/opt/edu-textbook-server/keepalive.js" ]

+ 30 - 0
docker/build.sh

@@ -0,0 +1,30 @@
+PROJECT_NAME=edu-textbook
+PROJECT_VERSION=1.0.0
+NODE_VERSION=18.19.1
+
+
+
+# 服务端
+mkdir -p opt/edu-textbook-server
+mkdir -p opt/certs/tbook.com.cn/
+cp ../server/dist/server/server.js opt/edu-textbook-server/
+cp ../server/package.json opt/edu-textbook-server/
+cp ../server/config.js opt/edu-textbook-server/
+cp ../server/keepalive.js opt/edu-textbook-server/
+cp ../server/config/certs/* opt/certs/tbook.com.cn/
+
+mkdir -p opt/nginx
+cp ../server/config/nginx-server.conf opt/nginx/
+# 前端
+mkdir -p var/www/edu-textbook
+cp ../dist/edu-textbook-www.tar.gz var/www/edu-textbook
+
+
+# 构建
+sudo docker build \
+--build-arg CMS_DOMAIN=$CMS_DOMAIN \
+--build-arg CMS_DISABLE=$CMS_DISABLE \
+--build-arg NODE_VERSION=$NODE_VERSION \
+--build-arg TERM=$TERM \
+--build-arg NGINX_DISABLE=$NGINX_DISABLE \
+--tag fmode:$PROJECT_NAME-$PROJECT_VERSION .

+ 5 - 0
docker/sources.list

@@ -0,0 +1,5 @@
+#deb https://mirrors.aliyun.com/debian bookworm main contrib non-free
+#deb https://mirrors.aliyun.com/debian bookworm-updates main contrib non-free
+
+deb http://mirrors.163.com/debian/ bookworm main non-free contrib
+deb http://mirrors.163.com/debian/ bookworm-updates main non-free contrib

BIN
docker/var/www/edu-textbook/edu-textbook-www.tar.gz


+ 71 - 0
server/keepalive.js

@@ -0,0 +1,71 @@
+const schedule = require('node-schedule');
+const request = require("request");
+const shell = require("shelljs")
+const pm2 = require("pm2")
+const exec = require('child_process').exec;
+
+// const express = require('express');
+// const app = express();
+
+    let appList = [
+        {host:"127.0.0.1",port:"61337",httpchk:"/parse",name:"server",cmd:"pm2 restart server"},
+    ]
+const keepaliveTask = ()=>{
+    //每分钟的1-10秒都会触发,其它通配符依次类推
+    schedule.scheduleJob('*/10 * * * * *', ()=>{
+        appList.forEach(app=>{
+            checkHttpAlive(app);
+        })
+    })
+}
+keepaliveTask()
+
+
+async function checkHttpAlive(app){
+    let info = shell.exec(`pm2 info ${app.name}`,{ silent: true })
+    // pm2服务不存在,无需检查
+    if(!info?.stdout){
+        return
+    }
+
+    // pm2服务存在,判断是否正常
+    jsonRequest(`http://${app.host}:${app.port}${app.httpchk}`,"GET").then(data=>{
+        // console.log(data)
+    }).catch(err=>{
+        console.log(err)
+        console.log("开始重启->"+app.host);
+        let com = exec(`${app.cmd}`)
+        com.stdout.on('data', function (data) {
+            console.log("重启成功->"+data);
+            // res();
+        });
+
+        com.stderr.on('data', function (data) {
+            console.log('重启出错->' + data);
+            // res();
+        });
+    })
+}
+
+function jsonRequest(url,method, body){
+    body = body || {}
+    method = method || "GET"
+    return new Promise((resolve,reject)=>{
+        request({
+            url: url,
+            method: method,
+            json:true,
+            headers:{
+                "Content-Type": "application/json"
+            },
+            body:body
+        },(error, response, body) => {
+            if (!error && response.statusCode == 200) {
+                resolve(body)
+            }else{
+                console.error(error)
+                reject(error)
+            }
+        })
+    })
+}

+ 27 - 27
server/vite.config.js

@@ -13,33 +13,33 @@ export default defineConfig({
             exportName:"EduTextbookServer",
             // initAppOnBoot:false,
         }),
-        // obfuscatorPlugin({
-        //     include: ["**/*.js"],
-        //     exclude: [/node_modules/],
-        //     apply: "build",
-        //     debugger: true,
-        //     options: {
-        //         debugProtection:false,
-        //         debugProtectionInterval:0,
-        //         // ...  [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator)
-        //         compact: true,
-        //         controlFlowFlattening: false,
-        //         controlFlowFlatteningThreshold: 0.5,
-        //         deadCodeInjection: false,
-        //         deadCodeInjectionThreshold: 0.2,
-        //         disableConsoleOutput: true,
-        //         identifierNamesGenerator: 'hexadecimal',
-        //         log: true,
-        //         renameGlobals: false,
-        //         rotateStringArray: true,
-        //         selfDefending: false,
-        //         stringArray: true,
-        //         stringArrayEncoding: ['base64'],
-        //         stringArrayThreshold: 0.5,
-        //         transformObjectKeys: false,
-        //         unicodeEscapeSequence: false
-        //     },
-        // }),
+        obfuscatorPlugin({
+            include: ["**/*.js"],
+            exclude: [/node_modules/],
+            apply: "build",
+            debugger: true,
+            options: {
+                debugProtection:false,
+                debugProtectionInterval:0,
+                // ...  [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator)
+                compact: true,
+                controlFlowFlattening: false,
+                controlFlowFlatteningThreshold: 0.5,
+                deadCodeInjection: false,
+                deadCodeInjectionThreshold: 0.2,
+                disableConsoleOutput: true,
+                identifierNamesGenerator: 'hexadecimal',
+                log: true,
+                renameGlobals: false,
+                rotateStringArray: true,
+                selfDefending: false,
+                stringArray: true,
+                stringArrayEncoding: ['base64'],
+                stringArrayThreshold: 0.5,
+                transformObjectKeys: false,
+                unicodeEscapeSequence: false
+            },
+        }),
     ],
     build: {
         lib: {