Skip to content

Commit

Permalink
begin admin module
Browse files Browse the repository at this point in the history
  • Loading branch information
anneb committed Sep 6, 2019
1 parent 43f5348 commit a4e5ab7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
config/dbconfig.json
public/files
admin/files
cache
3 changes: 3 additions & 0 deletions admin/pgserver.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body,html {
font-family: Verdana, Geneva, Tahoma, sans-serif
}
File renamed without changes.
3 changes: 2 additions & 1 deletion config/pgserver.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"port": 8090
"userport": 8090,
"adminport": 8090
}
38 changes: 34 additions & 4 deletions pgserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ app.use(logger('dev'));
app.use(cors());
app.use('/', express.static(__dirname + '/public'));

let adminApp;
if (pgserverconfig.userport === pgserverconfig.adminport) {
adminApp = app;
} else {
if (pgserverconfig.adminport > 0) {
adminApp = express();
adminApp.use(logger('dev'));
adminApp.use(cors());
}
}
if (adminApp) {
adminApp.use('/admin', express.static(__dirname + '/admin'));
}


const dbconfig = require('./config/dbconfig.json');
const pgp = require('pg-promise')({
Expand All @@ -18,15 +32,24 @@ const pgp = require('pg-promise')({
}*/
});
const readOnlyPool = pgp(dbconfig);
//readOnlyPool.connect();
let adminPool;
if (adminApp) {
if (dbconfig.adminuser && dbconfig.user !== dbconfig.adminuser) {
dbconfig.user = dbconfig.adminuser;
dbconfig.password = dbconfig.adminpassword;
adminPool = pgp(dbconfig);
} else {
adminPool = readOnlyPool;
}
}

const DirCache = require('./utils/dircache.js')
const cache = new DirCache(`./cache/${dbconfig.database?dbconfig.database:process.env.PGDATABASE?process.env.PGDATABASE:''}`);


const swagger = require('./swagger.js')(app);
const login = require('./login.js')(app);
const upload = require('./upload.js')(app);
const upload = require('./upload.js')(adminApp);
const mvt = require('./mvt.js')(app, readOnlyPool, cache);
const geojson = require('./geojson.js')(app, readOnlyPool);
const geobuf = require('./geobuf.js')(app, readOnlyPool);
Expand All @@ -36,9 +59,16 @@ const bbox = require('./bbox.js')(app, readOnlyPool, cache);
const query = require('./query.js')(app, readOnlyPool);
const columnStats = require('./column_stats.js')(app, readOnlyPool, cache);

const server = app.listen(pgserverconfig.port);
const server = app.listen(pgserverconfig.userport);
server.setTimeout(600000);
console.log(`pgserver listening on port ${pgserverconfig.userport}`);

console.log(`pgserver listening on port ${pgserverconfig.port}`);
if (adminApp && adminApp !== app) {
const adminServer = adminApp.listen(pgserverconfig.adminport);
adminServer.setTimeout(600000);
}
if (adminApp) {
console.log(`pgserver admin listening on port ${pgserverconfig.adminport}`)
}

module.exports = app;
13 changes: 11 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="pgserver.css">
<title>PGServer UI</title>
<script>
function setUrlPort() {
document.querySelectorAll('.modifyport').forEach(el=>{
let url = new URL(el.href);
let port = url.port;
el.href = `${url.protocol}//${url.hostname}${port!==''?`:${port}`:''}${url.pathname}${url.search}${url.hash}`
})
}
</script>
</head>
<style>

</style>
<body>
<body onload="setUrlPort()">
<h1>PGServer</h1>
Welcome to pgserver<br>
<a href="/info.html">Browse geo-data</a><br>
<a href="/api-docs">API documentation (swagger)</a><br>
<a href="/upload.html">Upload files</a>
<a href="/admin/upload.html" class="modifyport">Upload files</a>
</body>
</html>
15 changes: 9 additions & 6 deletions upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ const fs = require('fs');
const fileUpload = require('express-fileupload');

module.exports = function(app) {
if (!app) {
return;
}
app.use(fileUpload({
useTempFiles: true,
tempFileDir : `${__dirname}/public/files/`
tempFileDir : `${__dirname}/admin/files/`
}));

app.post('/upload', (req, res) => {
app.post('/admin/upload', (req, res) => {
let uploadFile = req.files.uploadfile;
const fileName = uploadFile.name;
res.json({
file: `${fileName}`
})
uploadFile.mv(
`${__dirname}/public/files/${fileName}`,
`${__dirname}/admin/files/${fileName}`,
function (err) {
if (err) {
return res.status(500).send(err);
Expand All @@ -25,16 +28,16 @@ module.exports = function(app) {
)
});

app.get('/upload', (req, res) =>{
app.get('/admin/upload', (req, res) =>{
url = req.query.fetch;
console.log(url);
res.json({
file: 'index.html'
});
})

app.delete('/upload', express.json({type: '*/*'}), (req, res) => {
fs.unlinkSync(`${__dirname}/public/files/${req.body.file}`);
app.delete('/admin/upload', express.json({type: '*/*'}), (req, res) => {
fs.unlinkSync(`${__dirname}/admin/files/${req.body.file}`);
res.json({
file: 'done'
});
Expand Down

0 comments on commit a4e5ab7

Please sign in to comment.