mirror of
https://github.com/danbulant/koa-plugins
synced 2026-05-19 04:08:43 +00:00
Add index support
This commit is contained in:
parent
bd46585c40
commit
70ece68ce6
2 changed files with 36 additions and 4 deletions
|
|
@ -1,14 +1,19 @@
|
|||
const PHPFPM = require("./phpfpm");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const phpfpm = new PHPFPM({
|
||||
sockFile: "/run/php/php7.3-fpm.sock",
|
||||
documentRoot: path.join(__dirname, "../../contents")
|
||||
});
|
||||
|
||||
const useIndex = true;
|
||||
|
||||
module.exports = {
|
||||
enabled: true,
|
||||
exec(ctx){
|
||||
return new Promise((resolve, reject)=>{
|
||||
if(ctx.request.url.split("/")[ctx.request.url.split("/").length - 1] == "")ctx.request.url += "index.php";
|
||||
|
||||
phpfpm.run({
|
||||
hostname: ctx.hostname,
|
||||
remote_addr: ctx.request.ip,
|
||||
|
|
@ -22,15 +27,22 @@ module.exports = {
|
|||
|
||||
ctx.body = output;
|
||||
|
||||
if (phpErrors) console.error(err, phpErrors);
|
||||
if (phpErrors) console.error(phpErrors);
|
||||
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
},
|
||||
rules(ctx){
|
||||
var p = path.join(__dirname, "../../contents/", ctx.request.url);
|
||||
if(ctx.request.url.split("/")[ctx.request.url.split("/").length - 1] == "" && useIndex)p = path.join(p, "index.php");
|
||||
|
||||
|
||||
if(!fs.existsSync(p))return false;
|
||||
|
||||
if(ctx.request.url.split("/")[ctx.request.url.split("/").length - 1] == "" && useIndex)return true;
|
||||
|
||||
var ext = ctx.request.url.split('.').pop();
|
||||
console.log("Filetype: " + ext);
|
||||
return ext == "php";
|
||||
},
|
||||
priority: -2,
|
||||
|
|
|
|||
|
|
@ -6,20 +6,40 @@ const fs = require("fs");
|
|||
const path = require("path")
|
||||
const PassThrough = require('stream').PassThrough;
|
||||
|
||||
const useIndex = true;
|
||||
|
||||
module.exports = {
|
||||
enabled: true,
|
||||
exec(ctx){
|
||||
var p = path.join(__dirname, "../../contents", ctx.request.url);
|
||||
|
||||
if(ctx.request.url.split("/")[ctx.request.url.split("/").length - 1] == "" && useIndex){
|
||||
p = path.join(p, "index.html");
|
||||
|
||||
if(!fs.existsSync(p)){
|
||||
ctx.body = "Forbidden";
|
||||
ctx.status = 403;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(fs.lstatSync(path_string).isDirectory()){
|
||||
ctx.body = "Forbidden";
|
||||
ctx.status = 403;
|
||||
return;
|
||||
}
|
||||
|
||||
if(!fs.existsSync(p)){
|
||||
ctx.body = "Not Found";
|
||||
ctx.status = 404;
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.set('Content-Type', 'text/html');
|
||||
ctx.body = fs.createReadStream(p).on('error', ctx.onerror).pipe(PassThrough());
|
||||
ctx.body = fs.createReadStream(p).on('error', (e)=>{ctx.onerror(e)}).pipe(PassThrough());
|
||||
},
|
||||
rules(ctx){
|
||||
return ctx.request.url.split('.').pop() != "php";
|
||||
},
|
||||
priority: -5
|
||||
priority: -1
|
||||
}
|
||||
Loading…
Reference in a new issue