diff --git a/.gitignore b/.gitignore index 66ee735..75545b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules/ /.env +/.vercel diff --git a/Dockerfile b/Dockerfile index cecbe65..e971b88 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ FROM node:alpine +WORKDIR /app ADD package.json . RUN npm i +ADD api api ADD index.js . CMD ["npm","start"] diff --git a/api/app.js b/api/app.js new file mode 100644 index 0000000..758057f --- /dev/null +++ b/api/app.js @@ -0,0 +1,11 @@ +module.exports = (req, res) => { + res.json({ + headers: req.headers, + body: req.body == null ? null : req.body, + host: req.headers.host, + method: req.method, + target: req.url, + query: req.query, + cookies: req.cookies, + }); +}; diff --git a/index.js b/index.js index 4bfe9e6..668bd02 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,14 @@ const express = require("express"); +const cookieParser = require("cookie-parser"); +const morgan = require("morgan"); const passport = require("passport"); + const { BasicStrategy } = require("passport-http"); const { timingSafeEqual } = require("crypto"); const { HTTP_USERNAME, HTTP_PASSWORD } = process.env; passport.use( - new BasicStrategy(function(username, password, done) { + new BasicStrategy(function (username, password, done) { return done( null, HTTP_USERNAME != null && @@ -19,21 +22,13 @@ passport.use( const app = express(); +app.use(cookieParser()); +app.use(morgan("combined")); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(express.raw({ type: "*/*" })); app.set("json spaces", 2); app.all("/basic-auth", passport.authenticate("basic", { session: false })); -app.all("/*", function(req, res) { - res.json({ - headers: req.headers, - body: req.body.length > 0 ? req.body.toString() : null, - form: req.is("urlencoded") ? req.body : null, - json: req.is("json") ? req.body : null, - method: req.method, - target: req.url, - host: req.headers.host - }); -}); +app.all("/*", require("./api/app.js")); app.listen(process.env.PORT || 8080); diff --git a/package.json b/package.json index c7e667c..7439342 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,11 @@ "start": "node -r dotenv/config index.js" }, "dependencies": { + "cookie-parser": "latest", "express": "latest", - "passport-http": "latest", - "passport": "latest" + "morgan": "latest", + "passport": "latest", + "passport-http": "latest" }, "devDependencies": { "dotenv": "latest", diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..0eefc00 --- /dev/null +++ b/vercel.json @@ -0,0 +1,4 @@ +{ + "version": 2, + "routes": [{ "src": "/.*", "dest": "/api/app.js" }] +}