Created
June 6, 2018 04:41
-
-
Save Ethan-Arrowood/d83536d484028de6a894d12692a24479 to your computer and use it in GitHub Desktop.
HDB_Studio with harperDBCallout replaced by HarperDBConnect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const express = require('express'), | |
app = express(), | |
bodyParser = require('body-parser'), | |
path = require('path'), | |
favicon = require('express-favicon'), | |
passport = require('passport'), | |
LocalStrategy = require('passport-local').Strategy, | |
session = require('express-session'), | |
hdb_callout = require('./utility/harperDBCallout'), | |
hdbConnect = require('harperdb-connect'), | |
http = require('http'), | |
https = require('https'), | |
fs = require('fs'); | |
const config = require('./config/config.json'); | |
const DEFAULT_HTTP_PORT = 61183; | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ | |
extended: false | |
})); | |
app.use(express.static(path.join(__dirname, 'resources'))); | |
app.set('views', path.join(__dirname + '/pages')); | |
app.set('view engine', 'jade') | |
app.use(favicon("https://s3.amazonaws.com/hdb-marketing/harperdb__flavicon_4Cw_icon.ico")); | |
app.use(session({ | |
secret: 'keyboard cat', | |
resave: true, | |
saveUninitialized: true, | |
rolling: true, | |
cookie: { | |
maxAge: 60000 * 15, // 15 minutes | |
} | |
})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
// route | |
var main = require('./routes/main'), | |
login = require('./routes/login'), | |
security = require('./routes/security'), | |
explore = require('./routes/explore'), | |
logs = require('./routes/logs'), | |
logout = require('./routes/logout'), | |
user_detail = require('./routes/user_detail'), | |
schema = require('./routes/schema'), | |
health = require('./routes/health'), | |
help = require('./routes/help'), | |
live_link = require('./routes/live_link'); | |
app.use('/main', main); | |
app.use('/login', login); | |
app.use('/security', security); | |
app.use('/explore', explore); | |
app.use('/logs', logs); | |
app.use('/logout', logout); | |
app.use('/user_detail', user_detail); | |
app.use('/schema', schema); | |
app.use('/health', health); | |
app.use('/help', help); | |
app.use('/livelink', live_link); | |
app.use('/', main); | |
//authen | |
passport.use(new LocalStrategy({ | |
passReqToCallback: true | |
}, | |
function (req, username, password, done) { | |
var db = new hdbConnect.HarperDBConnect(username, password); | |
db.connect(req.body.endpoint_url + ':' + req.body.endpoint_port) | |
.then(() => { | |
console.log('Connected to db') | |
db.request({ | |
method: 'POST', | |
url: db.options.url, | |
headers: { | |
'content-type': 'application/json', | |
authorization: db.authorization, | |
'cache-control': 'no-cache' | |
}, | |
body: { | |
operation: 'user_info' | |
}, | |
json: true, | |
}, false).then(user => { | |
console.log('we good m8'); | |
if (user && user.active) { | |
user.password = password; | |
user.endpoint_url = req.body.endpoint_url; | |
user.endpoint_port = req.body.endpoint_port; | |
return done(null, user); | |
} else if (user) { | |
return done(null, false, { | |
message: JSON.stringify(user) | |
}); | |
} else { | |
return done(null, false, { | |
message: 'Invalid credentials' | |
}); | |
} | |
}).catch(err => { | |
return done(null, false, { | |
message: err | |
}); | |
}) | |
}) | |
.catch(err => console.error(err)) | |
} | |
)); | |
passport.serializeUser(function (user, done) { | |
done(null, user); | |
}); | |
passport.deserializeUser(function (user, done) { | |
done(null, user); | |
}); | |
runServer(); | |
function runServer() { | |
if (true || process.version >= 'v8.11.0') { | |
let http_port = config.http_port; | |
if (!http_port && !config.https_port) { | |
http_port = DEFAULT_HTTP_PORT; | |
} | |
if (http_port) { | |
http.createServer(app).listen(http_port, () => { | |
console.log('HarperDB Studio running on port ' + http_port); | |
}); | |
} | |
if (config.https_port && config.https_key_path && config.https_cert_path) { | |
let credentials = { | |
key: fs.readFileSync(config.https_key_path), | |
cert: fs.readFileSync(config.https_cert_path) | |
}; | |
https.createServer(credentials, app) | |
.listen(config.https_port, function () { | |
console.log('HarperDB Studio running on port ' + config.https_port); | |
}); | |
} | |
} | |
else | |
console.log(" HarperDB Studio requires Node.js version 8.11 or higher"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment