1
- import { blockSpamReferers } from '@toba/block-spam-referer' ;
2
- import compress from 'compression' ;
3
- import Express from 'express' ;
4
- import { ExpressHandlebars } from '@toba/handlebars' ;
5
- import * as path from 'path' ;
6
- import { postProvider } from '@trailimage/flickr-provider' ;
7
- import { mapProvider } from '@trailimage/google-provider' ;
8
- import { config as modelConfig , blog } from '@trailimage/models' ;
9
- import { config } from './config' ;
1
+ import { blockSpamReferers } from '@toba/block-spam-referer'
2
+ import compress from 'compression'
3
+ import Express from 'express'
4
+ import { ExpressHandlebars } from '@toba/handlebars'
5
+ import * as path from 'path'
6
+ import { postProvider } from '@trailimage/flickr-provider'
7
+ import { mapProvider } from '@trailimage/google-provider'
8
+ import { config as modelConfig , blog } from '@trailimage/models'
9
+ import { config } from './config'
10
10
import {
11
11
Layout ,
12
12
addTemplateMethods ,
13
13
requireSSL ,
14
14
checkCacheReset ,
15
15
sortCategories
16
- } from './views/' ;
17
- import { route } from './routes' ;
16
+ } from './views/'
17
+ import { route } from './routes'
18
18
19
- const root = path . join ( __dirname , '..' ) ;
19
+ const root = path . join ( __dirname , '..' )
20
20
21
21
if ( process . argv . includes ( '--serve' ) ) {
22
- configureModels ( ) ;
23
- createWebService ( ) ;
22
+ configureModels ( )
23
+ createWebService ( )
24
24
}
25
25
26
26
export function configureModels ( ) {
27
- postProvider . configure ( config . providers . post ) ;
28
- mapProvider . configure ( config . providers . map ) ;
27
+ postProvider . configure ( config . providers . post )
28
+ mapProvider . configure ( config . providers . map )
29
29
30
- modelConfig . site = config . site ;
31
- modelConfig . owner = config . owner ;
32
- modelConfig . subtitleSeparator = config . posts . subtitleSeparator ;
33
- modelConfig . maxPhotoMarkersOnMap = config . providers . map . maxMarkers ;
34
- modelConfig . providers . post = postProvider ;
35
- modelConfig . providers . map = mapProvider ;
30
+ modelConfig . site = config . site
31
+ modelConfig . owner = config . owner
32
+ modelConfig . subtitleSeparator = config . posts . subtitleSeparator
33
+ modelConfig . maxPhotoMarkersOnMap = config . providers . map . maxMarkers
34
+ modelConfig . providers . post = postProvider
35
+ modelConfig . providers . map = mapProvider
36
36
modelConfig . artistsToNormalize = new RegExp (
37
37
config . posts . artistNames . join ( '|' )
38
- ) ;
38
+ )
39
39
}
40
40
41
41
async function createWebService ( ) {
42
- const app = Express ( ) ;
43
- const port = process . env [ 'PORT' ] || 3000 ;
42
+ const app = Express ( )
43
+ const port = process . env [ 'PORT' ] || 3000
44
44
45
45
console . info (
46
46
`Starting ${
47
47
config . isProduction ? 'production' : 'development'
48
48
} application`
49
- ) ;
49
+ )
50
50
51
- defineViews ( app ) ;
51
+ defineViews ( app )
52
52
53
- if ( false ) {
54
- //config.needsAuth) {
53
+ if (
54
+ false &&
55
+ ! ( postProvider . isAuthenticated && mapProvider . isAuthenticated )
56
+ ) {
55
57
// must authenticate before normal routes are available
56
- route . authentication ( app ) ;
57
- app . listen ( port ) ;
58
- console . info ( `Listening for authentication on port ${ port } ` ) ;
58
+ route . authentication ( app )
59
+ app . listen ( port )
60
+ console . info ( `Listening for authentication on port ${ port } ` )
59
61
} else {
60
- if ( config . requireSSL ) {
61
- app . use ( requireSSL ) ;
62
- }
63
- app . use ( blockSpamReferers ) ;
64
- app . use ( checkCacheReset ) ;
62
+ if ( config . requireSSL ) app . use ( requireSSL )
63
+
64
+ app . use ( blockSpamReferers )
65
+ app . use ( checkCacheReset )
65
66
// https://github.com/expressjs/compression/blob/master/README.md
66
- app . use ( compress ( ) ) ;
67
- app . use ( Express . static ( path . join ( root , 'public' ) ) ) ;
67
+ app . use ( compress ( ) )
68
+ app . use ( Express . static ( path . join ( root , 'public' ) ) )
68
69
69
- await blog . load ( ) ;
70
+ await blog . load ( )
70
71
71
72
if ( blog . loaded ) {
72
73
// blog must be loaded before routes are defined
73
- sortCategories ( blog ) ;
74
- route . standard ( app ) ;
75
- app . listen ( port ) ;
76
- console . info ( `Listening on port ${ port } ` ) ;
74
+ sortCategories ( blog )
75
+ route . standard ( app )
76
+ app . listen ( port )
77
+ console . info ( `Listening on port ${ port } ` )
77
78
} else {
78
- console . error ( 'Blog data failed to load. Stopping application.' ) ;
79
+ console . error ( 'Blog data failed to load. Stopping application.' )
79
80
}
80
81
}
81
82
}
@@ -85,15 +86,15 @@ async function createWebService() {
85
86
* @see http://mustache.github.com/mustache.5.html
86
87
*/
87
88
function defineViews ( app : Express . Application ) {
88
- const viewPath = path . join ( root , 'views' ) ;
89
+ const viewPath = path . join ( root , 'views' )
89
90
const ehb = new ExpressHandlebars ( {
90
91
defaultLayout : Layout . Main !
91
- } ) ;
92
+ } )
92
93
93
94
// http://expressjs.com/4x/api.html#app-settings
94
- app . set ( 'views' , viewPath ) ;
95
- app . set ( 'view engine' , ehb . fileExtension ) ;
96
- app . engine ( ehb . fileExtension , ehb . renderer ) ;
95
+ app . set ( 'views' , viewPath )
96
+ app . set ( 'view engine' , ehb . fileExtension )
97
+ app . engine ( ehb . fileExtension , ehb . renderer )
97
98
98
- addTemplateMethods ( ehb ) ;
99
+ addTemplateMethods ( ehb )
99
100
}
0 commit comments