-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.coffee
100 lines (74 loc) · 2.64 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
express = require "express"
stylus = require "stylus"
nib = require "nib"
passport = require "passport"
GitHubStrategy = require('passport-github').Strategy
GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID || "041e33b4c176a007f627"
GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET || "f70b6f1468f4ef87ba18d5587a2be6d7c6c4ae98";
app = module.exports = express.createServer()
passport.serializeUser (user, done) ->
done(null, user);
passport.deserializeUser (obj, done) ->
done(null, obj);
callbackURL = process.env.GITHUB_CALLBACK_URL || "http://localhost:8085/auth/callback"
passport.use new GitHubStrategy {
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
callbackURL: callbackURL
},
(accessToken, refreshToken, profile, done) ->
process.nextTick ->
profile.accessToken = accessToken
profile.avatar = profile._json.avatar_url
return done(null, profile);
# stylus compile function
compile = (str, path) ->
return stylus(str)
.define("url", stylus.url({ paths: [__dirname + "/public"] }))
.set("filename", path)
.set("warn", true)
.set("compress", false)
.use(nib())
app.configure ->
# stylus middleware
app.use stylus.middleware
src : __dirname + "/styls" # styl files should be placed inside this folder
dest : __dirname + "/public" # CSS files will be complied to public directory
compile: compile # compile function
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {layout: false});
app.use(express.favicon(__dirname + "/public/favicon.ico"));
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.get '/auth',
passport.authenticate('github', scope: 'public_repo'),
(req, res) ->
app.get "/", (req, res) ->
user = req.user
if not user
user = {username:'guest'}
console.log "user", user
res.render "index", {user: user}
app.get "/:username/:repoName/issues/:number", (req, res) ->
user = req.user
if not user
user = {username:'guest'}
res.render "index", {user: user}
app.get "/logout", (req, res) ->
req.logout()
res.redirect "/"
app.get '/auth/callback',
passport.authenticate('github', { failureRedirect: '/login' }),
(req, res) ->
res.redirect('/');
port = process.env.PORT || 8085
app.listen port
console.log "server started on port 8085. Open http://localhost:8085 in your browser"