Skip to content

Commit

Permalink
add support for cors
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Jul 20, 2016
1 parent 9eaf052 commit f6ccc9e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
35 changes: 18 additions & 17 deletions bin/slackin
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

var pkg = require('./../package');
var args = require('args');
var slackin = require('./../dist').default;
var pkg = require('./../package')
var args = require('args')
var slackin = require('./../dist').default

args
.option(['p', 'port'], 'Port to listen on [$PORT or 3000]', require('hostenv').PORT || process.env.PORT || 3000)
Expand All @@ -11,33 +11,34 @@ args
.option(['i', 'interval'], 'How frequently (ms) to poll Slack [$SLACK_INTERVAL or 5000]', process.env.SLACK_INTERVAL || 5000)
.option(['P', 'path'], 'Path to serve slackin under', '/')
.option(['s', 'silent'], 'Do not print out warns or errors')
.option(['x', 'cors'], 'Enable CORS for all routes')
.option(['C', 'coc'], 'Full URL to a CoC that needs to be agreed to')
.option(['S', 'css'], 'Full URL to a custom CSS file to use on the main page')
.option(['?', 'help'], 'Show the usage information');
.option(['?', 'help'], 'Show the usage information')

var flags = args.parse(process.argv, {
value: '<team-id> <api-token>',
help: false
});
})

var org = args.sub[0] || process.env.SLACK_SUBDOMAIN;
var token = args.sub[1] || process.env.SLACK_API_TOKEN;
var org = args.sub[0] || process.env.SLACK_SUBDOMAIN
var token = args.sub[1] || process.env.SLACK_API_TOKEN

if (flags.help) {
args.showHelp();
args.showHelp()
}

if (!org || !token) {
args.showHelp();
args.showHelp()
} else {
flags.org = org;
flags.token = token;
flags.org = org
flags.token = token
}

var port = flags.port;
var hostname = flags.hostname;
var port = flags.port
var hostname = flags.hostname

slackin(flags).listen(port, hostname, function(err){
if (err) throw err;
if (!flags.silent) console.log('%s – listening on %s:%d', new Date, hostname, port);
});
slackin(flags).listen(port, hostname, function (err) {
if (err) throw err
if (!flags.silent) console.log('%s – listening on %s:%d', new Date, hostname, port)
})
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { json } from 'body-parser'
import { Server as http } from 'http'
import remail from 'email-regex'
import dom from 'vd'
import cors from 'cors'

// our code
import Slack from './slack'
Expand All @@ -23,6 +24,7 @@ export default function slackin ({
org,
css,
coc,
cors: useCors = false,
path='/',
channels,
silent = false // jshint ignore:line,
Expand All @@ -43,7 +45,7 @@ export default function slackin ({
// setup app
let app = express()
let srv = http(app)
srv.app = app;
srv.app = app

let assets = __dirname + '/assets'

Expand All @@ -61,6 +63,10 @@ export default function slackin ({
slack.once('ready', next)
})

if (useCors) {
app.use(cors())
}

// splash page
app.get('/', (req, res) => {
let { name, logo } = slack.org
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"description": "Public Slack organizations made easy",
"repository": "rauchg/slackin",
"main": "dist/index",
"files": ["dist", "bin"],
"files": [
"dist",
"bin"
],
"babel": {
"presets": [
"es2015"
Expand All @@ -14,6 +17,7 @@
"args": "1.3.0",
"babel-polyfill": "6.3.14",
"body-parser": "1.10.2",
"cors": "2.7.1",
"debug": "2.1.1",
"email-regex": "1.0.0",
"express": "4.11.0",
Expand Down

0 comments on commit f6ccc9e

Please sign in to comment.