Skip to content

Commit

Permalink
fixed http pipe error
Browse files Browse the repository at this point in the history
  • Loading branch information
lixinyang123 committed Mar 20, 2024
1 parent 6f0ec21 commit 8319fe9
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ EXPOSE 80
WORKDIR /app
ENV IN_DOCKER=true

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

RUN apt update && apt upgrade -y
RUN apt install curl nginx -y

RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash
RUN apt install nodejs -y

RUN touch ./docker

COPY ["./assets","./assets"]
COPY ["./src","./src"]
COPY ["./package.json","./package.json"]
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
dockerfile: Dockerfile
restart: always
volumes:
- .:/app
- /usr/bin/docker:/usr/bin/docker
- /var/run/docker.sock:/var/run/docker.sock
user: root
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"type": "module",
"dependencies": {
"dotenv": "^16.4.5",
"moment": "^2.30.1",
"pejs": "^0.6.5"
},
"devDependencies": {},
"scripts": {
"restore": "npm install",
"start": "node ./src/main.js"
Expand Down
20 changes: 12 additions & 8 deletions src/core/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import http from 'http'
import path from 'path'
import moment from 'moment'
import { pathToFileURL } from 'url'
import { Group } from './group.js'
import { Middleware } from './middleware.js'
Expand All @@ -14,18 +15,22 @@ export class App {
this.middlewares = []
this.actions = []
this.logger = logger
this.server = http.createServer((req, res) => {

this.server = http.createServer(async (req, res) => {
this.logger.info(`${new Date()}\n${req.method} ${req.url}`)
let start = moment()

this.pipeline.invoke(
await this.pipeline.invoke(
Request.object(req, this.host),
Response.object(res),
this.pipeline
)

this.logger.info(moment().diff(start) + "ms\n")
})
}

#endpoint(req, res) {
async #endpoint(req, res) {
let action = this.actions
.filter(i => new RegExp(`^${i.path}$`).test(req.pathname))
.sort((a, b) => b.path.length - a.path.length)
Expand All @@ -36,7 +41,7 @@ export class App {
return
}

action.invoke(req, res).catch(err => {
await action.invoke(req, res).catch(err => {
this.logger.error(err)
res.error('Server Error')
})
Expand Down Expand Up @@ -75,7 +80,7 @@ export class App {
try {
callback(req, res)
resolve()
}
}
catch (err) { reject(err) }
})
})
Expand Down Expand Up @@ -123,15 +128,14 @@ export class App {
}

build() {

this.middlewares.forEach((middleware, index) => {
if (index >= this.middlewares.length - 1) return
middleware.next = this.middlewares[index + 1]
})

this.middlewares[this.middlewares.length - 1].next = {
handler: (req, res, _) => {
this.#endpoint(req, res)
handler: async (req, res, _) => {
await this.#endpoint(req, res)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export class Middleware {
this.next = undefined
}

invoke(req, res, middleware) {
middleware.handler(req, res, (req, res) =>
this.invoke(req, res, middleware.next)
async invoke(req, res, middleware) {
await middleware.handler(req, res, async (req, res) =>
await this.invoke(req, res, middleware.next)
)
}
}
8 changes: 0 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ config()
const app = new App()
const ttyd = new TTYD()

// Compute the api response time

app.use((req, res, next) => {
console.time()
next(req, res)
console.timeEnd()
})

// Map Static file
app.use(new StaticFile('/wwwroot'))
app.use(new Cors())
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Middleware } from '../core/middleware.js';
export class Cors extends Middleware {

constructor() {
let func = (req, res, next) => {
let func = async (req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', '*')
res.setHeader('Access-Control-Allow-Headers', '*')
Expand All @@ -13,7 +13,7 @@ export class Cors extends Middleware {
return;
}

next(req, res)
await next(req, res)
}

super(func)
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/pathbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Middleware } from "../core/middleware.js";
export class PathBase extends Middleware {

constructor(basePath) {
let func = (req, res, next) => {
let func = async (req, res, next) => {
if (req.pathname.startsWith(basePath)) {
req.pathname = req.pathname.replace(basePath, '')
}

next(req, res)
await next(req, res)
}

super(func)
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/staticfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { Middleware } from "../core/middleware.js";
export class StaticFile extends Middleware {

constructor(wwwroot) {
let func = (req, res, next) => {
let func = async (req, res, next) => {

let pathname = new URL(req.url, 'http://localhost').pathname
let fileName = path.join(process.cwd(), 'src', wwwroot, pathname)

if (!fs.existsSync(fileName) || !fs.lstatSync(fileName).isFile()) {
next(req, res)
await next(req, res)
return
}

Expand Down

0 comments on commit 8319fe9

Please sign in to comment.