mirror of
https://github.com/danbulant/koa-plugins
synced 2026-05-27 13:51:48 +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 PHPFPM = require("./phpfpm");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
const fs = require("fs");
|
||||||
const phpfpm = new PHPFPM({
|
const phpfpm = new PHPFPM({
|
||||||
sockFile: "/run/php/php7.3-fpm.sock",
|
sockFile: "/run/php/php7.3-fpm.sock",
|
||||||
documentRoot: path.join(__dirname, "../../contents")
|
documentRoot: path.join(__dirname, "../../contents")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const useIndex = true;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
exec(ctx){
|
exec(ctx){
|
||||||
return new Promise((resolve, reject)=>{
|
return new Promise((resolve, reject)=>{
|
||||||
|
if(ctx.request.url.split("/")[ctx.request.url.split("/").length - 1] == "")ctx.request.url += "index.php";
|
||||||
|
|
||||||
phpfpm.run({
|
phpfpm.run({
|
||||||
hostname: ctx.hostname,
|
hostname: ctx.hostname,
|
||||||
remote_addr: ctx.request.ip,
|
remote_addr: ctx.request.ip,
|
||||||
|
|
@ -22,15 +27,22 @@ module.exports = {
|
||||||
|
|
||||||
ctx.body = output;
|
ctx.body = output;
|
||||||
|
|
||||||
if (phpErrors) console.error(err, phpErrors);
|
if (phpErrors) console.error(phpErrors);
|
||||||
|
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
rules(ctx){
|
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();
|
var ext = ctx.request.url.split('.').pop();
|
||||||
console.log("Filetype: " + ext);
|
|
||||||
return ext == "php";
|
return ext == "php";
|
||||||
},
|
},
|
||||||
priority: -2,
|
priority: -2,
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,40 @@ const fs = require("fs");
|
||||||
const path = require("path")
|
const path = require("path")
|
||||||
const PassThrough = require('stream').PassThrough;
|
const PassThrough = require('stream').PassThrough;
|
||||||
|
|
||||||
|
const useIndex = true;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
exec(ctx){
|
exec(ctx){
|
||||||
var p = path.join(__dirname, "../../contents", ctx.request.url);
|
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)){
|
if(!fs.existsSync(p)){
|
||||||
ctx.body = "Not Found";
|
ctx.body = "Not Found";
|
||||||
ctx.status = 404;
|
ctx.status = 404;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.set('Content-Type', 'text/html');
|
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){
|
rules(ctx){
|
||||||
return ctx.request.url.split('.').pop() != "php";
|
return ctx.request.url.split('.').pop() != "php";
|
||||||
},
|
},
|
||||||
priority: -5
|
priority: -1
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue