Skip to content

Commit

Permalink
Introduce pathprefix
Browse files Browse the repository at this point in the history
  • Loading branch information
tjwelde committed Dec 12, 2017
1 parent 5f7f4f5 commit 3ba06f5
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ passport.use(new BasicStrategy(function (userid, password, done) {
passport.use(new GitHubStrategy({
clientID: settings.clientID,
clientSecret: settings.clientSecret,
callbackURL: "/admin/auth/callback",
callbackURL: settings.pathPrefix + "/admin/auth/callback",
scope: ['repo', 'write:repo_hook']
}, function(accessToken, refreshToken, profile, cb) {
var user = Users.find({ id: profile.id})
Expand Down
5 changes: 3 additions & 2 deletions example.settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"clientID": "<insert github clientID>",
"clientSecret": "insert github clientSecret"
"clientID": "<github clientID>",
"clientSecret": "<github clientSecret>",
"pathPrefix": "/githubmirror"
}
15 changes: 8 additions & 7 deletions routes/admin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var express = require('express')
var router = express.Router()
var passport = require('passport')
var settings = require('../settings.json')

var GitHubApi = require('github')
var githubSettings = {}
Expand All @@ -16,15 +17,15 @@ router.get('/login', function(req, res) {

router.get('/logout', function(req, res) {
req.logout()
res.redirect('/admin/login')
res.redirect(settings.pathPrefix + '/admin/login')
})

router.get('/auth/github', passport.authenticate('github'))

router.get('/auth/callback',
passport.authenticate('github', { failureRedirect: '/admin/login', failureFlash: true }),
passport.authenticate('github', { failureRedirect: settings.pathPrefix + '/admin/login', failureFlash: true }),
function(req, res) {
res.redirect("/admin")
res.redirect(settings.pathPrefix + '/admin')
}
)

Expand Down Expand Up @@ -52,7 +53,7 @@ router.get('/', isAuthenticated, function(req, res) {

router.post('/', isAuthenticated, function(req, res) {
if(req.body.repo && req.body.mirror && req.body.branch) {
var url = 'http://' + req.get('host') + '/mirror/' + req.body.repo
var url = 'http://' + req.get('host') + settings.pathPrefix + '/mirror/' + req.body.repo
var github = new GitHubApi(githubSettings)
github.authenticate({
type: "token",
Expand Down Expand Up @@ -81,7 +82,7 @@ router.post('/', isAuthenticated, function(req, res) {
branch: req.body.branch,
path: './repositories/' + req.body.repo
}}).value()
res.redirect('/admin')
res.redirect(settings.pathPrefix + '/admin')
}).catch(function (err) {
res.status(500).send(err)
})
Expand All @@ -106,7 +107,7 @@ router.get('/remove/:owner/:repo', isAuthenticated, function (req, res) {
}).then(function(data) {
var success = Repositories.unset(setting).value()
if(success) {
res.redirect("/admin")
res.redirect(settings.pathPrefix + '/admin')
}
else {
res.sendStatus(500)
Expand All @@ -122,5 +123,5 @@ module.exports = router

function isAuthenticated(req, res, next) {
if (req.isAuthenticated()) return next()
res.redirect('/admin/login')
res.redirect(settings.pathPrefix + '/admin/login')
}
7 changes: 4 additions & 3 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ var express = require('express')
var router = express.Router()
var passport = require('passport')
var Users = require('../config/db').get('users')
var settings = require('../settings.json')

var mirror = require('./mirror')
var admin = require('./admin')

router.get('/', function(req, res) {
res.redirect('/admin')
res.redirect(settings.pathPrefix + '/admin')
})

router.use('/mirror', mirror)
Expand All @@ -24,7 +25,7 @@ router.post('/newuser', hasNoBasicUser, function(req, res) {
password: req.body.password
}).value()
if(user){
return res.redirect('/admin')
return res.redirect(settings.pathPrefix + '/admin')
}
else {
return res.sendStatus(400)
Expand All @@ -42,7 +43,7 @@ function hasBasicUser(req, res, next) {
return next()
}
else {
res.redirect('/newuser')
res.redirect(settings.pathPrefix + '/newuser')
}
}
function basicAuthentication(req, res, next) {
Expand Down
6 changes: 4 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ if(!settings['clientID'] || !settings['clientSecret']) {

var app = express()

app.locals.pathprefix = settings.pathPrefix

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(express.static(path.resolve(__dirname, 'public')))
app.use(settings.pathPrefix, express.static(path.resolve(__dirname, 'public')))

app.use(morgan('dev'))
app.use(bodyParser.json())
Expand All @@ -37,7 +39,7 @@ app.use(flash());
app.use(passport.initialize())
app.use(passport.session())

app.use('/', routes)
app.use(settings.pathPrefix, routes)

var listener = app.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
var addr = listener.address()
Expand Down
2 changes: 1 addition & 1 deletion views/admin-login.jade
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ extends layout

block content
h1 Login
a(href="/admin/auth/github") Login with GitHub
a(href=pathprefix + "/admin/auth/github") Login with GitHub
2 changes: 1 addition & 1 deletion views/admin.jade
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ block content
.panel.panel-default
.panel-heading
=key
a(href='/admin/remove/' + key).glyphicon.glyphicon-remove.pull-right.text-muted
a(href=pathprefix + '/admin/remove/' + key).glyphicon.glyphicon-remove.pull-right.text-muted
.panel-body
.row
.col-md-2 #[strong GitHub:]
Expand Down
4 changes: 2 additions & 2 deletions views/layout.jade
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ doctype html
html
head
title= title
link(rel='stylesheet', href='/css/style.css')
link(rel='stylesheet', href=pathprefix + '/css/style.css')
link(rel='stylesheet', href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css')
script(src='https://code.jquery.com/jquery-3.1.1.min.js')
script(src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js')
!= LRScript
body
.container
block content
script(src='/js/admin.js')
script(src=pathprefix + '/js/admin.js')

0 comments on commit 3ba06f5

Please sign in to comment.