-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.coffee
More file actions
127 lines (101 loc) · 4.31 KB
/
server.coffee
File metadata and controls
127 lines (101 loc) · 4.31 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# defaults to production
environment = process.env.NODE_ENV
if environment is "production"
require 'newrelic'
# Module dependencies.
express = require "express"
path = require "path"
app = express()
# db
models = require './lib/models'
config = require './lib/config'
db = require('./lib/data/db')
console.log JSON.stringify(process.env)
basicAuth = require('basic-auth')
auth = (req, res, next) ->
unauthorized = (res) ->
res.set 'WWW-Authenticate', 'Basic realm=Authorization Required'
res.sendStatus 401
user = basicAuth(req)
if !user or !user.name or !user.pass
return unauthorized(res)
if user.name == 'iguana' and user.pass == process.env.ADMIN_PASSWORD
next()
else
unauthorized res
models.sync(force: false).
then( ->
console.log 'synced'
db.seq.query("SELECT @@sql_mode;").spread((results, metadata) ->
mode = results[0]['@@sql_mode'].split(',');
console.log mode
idx = mode.indexOf('ONLY_FULL_GROUP_BY');
if idx != -1
mode.splice idx, 1
mode = mode.join(',')
console.log mode
db.seq.query("SET sql_mode = '#{mode}'").spread((results, metadata) ->
console.log metadata
)
else
console.log 'No ONLY_FULL_GROUP_BY, no need to update mode'
)
).
catch((err) ->
console.log err
throw err
)
# Controllers
api = require "./lib/controllers/api"
importer = require "./lib/controllers/importer"
# Express Configuration
app.use require('morgan')("dev")
# Allow access control origin
app.use (req, res, next) ->
res.set
'Access-Control-Allow-Origin': '*'
'Access-Control-Allow-Methods': 'POST, GET'
'Access-Control-Allow-Headers': req.get('Access-Control-Request-Headers')
next()
bodyParser = require 'body-parser'
app.use bodyParser.urlencoded({ extended: false })
app.use bodyParser.json()
app.set 'trust proxy', true
app.use require('errorhandler')()
# Routes
app.get "/importer/:artist/rebuild_index", auth, importer.rebuild_index
#app.get "/importer/:artist/:archive_id/rebuild_index", importer.rebuild_show
#app.get "/importer/:artist/rebuild_setlists", importer.rebuild_setlists
app.get "/importer/rebuild-all", auth, importer.rebuild_all
#app.get "/importer/reslug", importer.reslug
app.get "/importer/rebuild-weighted-avg", auth, importer.reweigh
# app.get "/importer/search_data", api.search_data
#app.get '/views/:name.html', (req, res) -> res.renderView req.param('name')
app.get '/api/status', api.status
app.get '/api/artists', api.artists
app.get '/api/artists/:artist_slug', api.fix_artist_slug, api.single_artist
app.get '/api/artists/:artist_slug/years', api.fix_artist_slug, api.artist_years
app.get '/api/artists/:artist_slug/years/:year', api.fix_artist_slug, api.artist_year_shows
app.get '/api/artists/:artist_slug/years/:year/shows/:show_date', api.fix_artist_slug, api.artist_show_by_date
app.get '/api/artists/:artist_slug/top_shows', api.fix_artist_slug, api.top_shows
app.get '/api/artists/:artist_slug/random_show', api.fix_artist_slug, api.random_show
app.get '/api/artists/:artist_slug/random_date', api.fix_artist_slug, api.random_date
app.get '/api/artists/:artist_slug/shows', api.fix_artist_slug, api.artist_shows
app.get '/api/artists/:artist_slug/shows/:show_id', api.fix_artist_slug, api.single_show
app.get '/api/artists/:artist_slug/mp3/:track_id', api.fix_artist_slug, api.artist_mp3
app.get '/api/artists/:artist_slug/venues', api.fix_artist_slug, api.artist_venues
app.get '/api/artists/:artist_slug/venues/:venue_id', api.fix_artist_slug, api.single_venue
app.get '/api/artists/:artist_slug/search', api.fix_artist_slug, api.search
# app.get '/api/artists/:artist_slug/setlists', api.setlist.setlist
# app.get '/api/artists/:artist_slug/setlists/:setlist_id', api.setlist.show_id
# app.get '/api/artists/:artist_slug/setlists/on-date/:show_date', api.setlist.on_date
# app.get '/api/artists/:artist_slug/song_stats', api.setlist.song_stats
app.get '/api/latest-tapes', api.latest
app.get '/api/today', api.today
app.get '/api/poll', api.poll
app.post '/api/play', api.live
# Start server
console.log "Attempting to listen on port %d", (process.env.PORT or 9000)
port = process.env.PORT or 9000
app.listen port, ->
console.log "Express server listening on port %d in %s mode", port, app.get("env")