Skip to content

Commit

Permalink
Added wiki contents loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Rackover committed Feb 4, 2020
1 parent cda8259 commit 738d5a4
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 20 deletions.
10 changes: 6 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ global.APPLICATION_ROOT = argv.path || process.cwd();
process.env = require("./app/env.js")(argv);
global.WIKI_PATH = process.env.WIKI_PATH
global.WIKI_NAME = argv.name || process.env.GIT_REPO_URL.split("/").pop().replace(".git", "").toUpperCase()
global.WIKI_CONTENTS_DIRECTORY_NAME = "_contents";

global.logger = require("./app/log/logger.js");
global.git = require("./app/git.js");
global.logger = require("./app/log/logger.js")
global.git = require("./app/git.js")
global.markdown = require("./app/markdown.js")
global.wikiMap = require("./app/map.js");
global.wikiPage = require("./app/page.js");
global.wikiMap = require("./app/map.js")
global.wikiPage = require("./app/page.js")
global.wikiContents = require('./app/content.js')

logger.debug("Using root path: "+APPLICATION_ROOT+" and wiki path: "+WIKI_PATH)
logger.debug("Parchment currently running as user: "+require('os').userInfo().username)
Expand Down
39 changes: 39 additions & 0 deletions app/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const path = require("path");
const fs = require("fs")

let contents = {};
const contentsDirName = WIKI_CONTENTS_DIRECTORY_NAME;
const units = ["css", "favicon", "header", "background"];

module.exports = function(){
updateContents();

setInterval(updateContents, process.env.WIKI_CONTENT_UPDATE_INTERVAL * 60 * 1000);

return function(){return contents;}()
}()

function updateContents(){
logger.debug("Updating contents...")
contents = {
"css": "/style.css",
"favicon": "/res/img/favicon.png",
"header": false,
"background": false
}

for (k in units){
const v = units[k]
const dirPath = path.join(WIKI_PATH, contentsDirName, v);
if (fs.existsSync(dirPath)){
const files = fs.readdirSync(dirPath)
if (files.length > 0){
contents[v] = path.join(dirPath, files[0]).replace(WIKI_PATH, "")
logger.debug("Found custom content for "+v+"! ("+files[0]+")")
continue
}
}
}

logger.debug("Done!")
}
5 changes: 4 additions & 1 deletion app/defaultenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ module.exports = {
GIT_PEM_FILE: "git.pem",
GIT_REPO_URL: "git@github.com:louvekingdoms/Wiki.git",
GIT_REPO_BRANCH: "auto",
WIKI_PATH: require("path").join(APPLICATION_ROOT, "wiki")
GIT_PULL_INTERVAL: 1, // minutes
GIT_PUSH_INTERVAL: 0, // minutes
WIKI_PATH: require("path").join(APPLICATION_ROOT, "wiki"),
WIKI_CONTENT_UPDATE_INTERVAL: 1 //minutes
}
12 changes: 8 additions & 4 deletions app/env.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module.exports = function(env){
cEnv = process.env;
cEnv = {};
defaultEnv = require("./defaultenv.js");
cEnv.DEBUG_LEVEL = env.debuglvl || process.env.DEBUG_LEVEL || defaultEnv.DEBUG_LEVEL
cEnv.PORT = env.port || process.env.PORT || defaultEnv.PORT
cEnv.LOG_PORT = env.logport || process.env.LOG_PORT || defaultEnv.LOG_PORT
cEnv.PORT = parseInt(env.port || process.env.PORT || defaultEnv.PORT)
cEnv.LOG_PORT = parseInt(env.logport || process.env.LOG_PORT || defaultEnv.LOG_PORT)
cEnv.GIT_PEM_FILE = env.pemfile || process.env.GIT_PEM_FILE || defaultEnv.GIT_PEM_FILE
cEnv.GIT_REPO_URL = env.repo || process.env.GIT_REPO_URL || defaultEnv.GIT_REPO_URL
cEnv.GIT_REPO_BRANCH = env.branch || process.env.GIT_REPO_BRANCH || defaultEnv.GIT_REPO_BRANCH
cEnv.GIT_PULL_INTERVAL = parseInt(process.env.GIT_PULL_INTERVAL || defaultEnv.GIT_PULL_INTERVAL)
cEnv.GIT_PUSH_INTERVAL = parseInt(process.env.GIT_PUSH_INTERVAL || defaultEnv.GIT_PUSH_INTERVAL)
cEnv.WIKI_PATH = env.wikipath || process.env.WIKI_PATH || defaultEnv.WIKI_PATH
cEnv.GIT_PUSH_INTERVAL = process.env.GIT_PUSH_INTERVAL || defaultEnv.GIT_PUSH_INTERVAL
cEnv.WIKI_CONTENT_UPDATE_INTERVAL = parseInt(process.env.WIKI_CONTENT_UPDATE_INTERVAL || defaultEnv.WIKI_CONTENT_UPDATE_INTERVAL)
return cEnv;
}
}
9 changes: 8 additions & 1 deletion app/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ module.exports = function(port){
)
})

// Wiki contents route
app.get('/'+WIKI_CONTENTS_DIRECTORY_NAME+"/*", function(req, res){
res.sendFile(path.join(WIKI_PATH, req.path))
})

// Public directory
app.use(express.static(path.join(APPLICATION_ROOT, 'public')));

Expand All @@ -59,12 +64,14 @@ module.exports = function(port){
}

function getPageInfo(req){

return {
header: getHeaderInfo(),
navigation: getNavigationInfo(req),
footer: getFooterInfo(),
website: {
name: WIKI_NAME
name: WIKI_NAME,
links: wikiContents
}
}
}
Expand Down
46 changes: 42 additions & 4 deletions app/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,19 @@ module.exports = async function(){
process.exit(1)
});

logger.info("Git client OK!")
logger.info("Git client OK! I will pull every "+
process.env.GIT_PULL_INTERVAL+" minute"
+(process.env.GIT_PUSH_INTERVAL >0? " and push every "+process.env.GIT_PUSH_INTERVAL+" minute": "")
+ "!")

await pull();

setInterval(async function(){ await pull() }, 1000 * 60)
if (process.env.GIT_PULL_INTERVAL > 0) setInterval(async function(){ await pull() }, process.env.GIT_PULL_INTERVAL * 1000 * 60)
else{
logger.error("Impossible pull interval ("+process.env.GIT_PULL_INTERVAL+"), aborting to avoid damage.");
process.exit(1);
}
if (process.env.GIT_PUSH_INTERVAL > 0) setInterval(async function(){ await checkAndUploadModifications() }, process.env.GIT_PUSH_INTERVAL * 1000 * 60)

return {
checkAndUploadModifications: checkAndUploadModifications
Expand All @@ -85,13 +93,43 @@ async function pull(){
if (!error) logger.info("Done!")
}

async function checkAndUploadModifications(){
if (isOperating) return;
async function checkAndUploadModifications(changesName=null){
if(isOperating){
const wait = 2;
logger.debug("Could not check and upload modifications because of another operation, waiting for "+wait+" seconds");
setTimeout(async function(){await checkAndUploadModifications(changesName)}, wait * 1000);
return;
}
isOperating = true;

// 1. git add -A
// 2. git commit
// 3. git pull
// 4. commit the merge
// 5. git push origin branch

logger.info("Checking for modifications...")
await gitClient.add("-A")
.then(()=>{
logger.debug("Committing...")
return gitClient.commit(changesName || ("Update at "+new Date().toString()))
})
.then(() => {
logger.debug("Pulling...")
return gitClient.pull("origin", process.env.GIT_REPO_BRANCH);
})
.then(() =>{
logger.debug("Merging...")
return gitClient.commit("Merge")
})
.catch((e) =>{
logger.debug("There was an error during the merge, but this is probably because there is nothing to merge.")
})
.then(() => {
logger.debug("Pushing...")
return gitClient.push("origin", process.env.GIT_REPO_BRANCH)
});
logger.info("Done!")

isOperating = false;
}
8 changes: 4 additions & 4 deletions app/layout/default.pug
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ mixin recursive_loop(array, index, current, prefix)
doctype html
html
head
title #{website.name} wiki
link(rel="stylesheet", href="/style.css")
link(rel="icon", type="image/png", href="/res/img/favicon.png")
title #{website.name}
link(rel="stylesheet", href=website.links.css)
link(rel="icon", type="image/png", href=website.links.favicon)
body
.header
.header(style=(website.links.background ? "background-image:url('"+website.links.background+"')" : ""))
include elements/header.pug
.mainContainer
.navigation
Expand Down
9 changes: 7 additions & 2 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ body{
}

.header{
padding: 5px;
padding-left:20px;
border-bottom:1px dotted grey;
background-size:cover;
background-repeat: no-repeat;
background-origin: content-box;
background-position: center;
}

.header h1{
text-indent: 1%;
font-weight:normal;
color: rgb(255, 255, 255);
mix-blend-mode: difference;
}

.mainContainer{
Expand Down
1 change: 1 addition & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node app.js run

0 comments on commit 738d5a4

Please sign in to comment.