-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·110 lines (102 loc) · 3.95 KB
/
server.js
File metadata and controls
executable file
·110 lines (102 loc) · 3.95 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
import express from 'express'
import opener from 'opener'
import serialize from 'serialize-javascript'
import React from 'react'
import { renderToString } from 'react-dom/server'
import { createStore, applyMiddleware, combineReducers, compose } from 'redux'
import thunkMiddleware from 'redux-thunk'
import createSocketIoMiddleware from 'redux-socket.io'
import io from 'socket.io-client'
import { Provider } from 'react-redux'
import { createMemoryHistory, match, RouterContext } from 'react-router'
import { syncHistoryWithStore, routerReducer, routerMiddleware } from 'react-router-redux'
import routes from './src/routes'
import reducer from './src/reducers'
var cors = require('cors');
var proxy = require('http-proxy-middleware');
/* SocketIO connection */
let socket = io('http://172.24.10.3:8080/');
let socketIoMiddleware = createSocketIoMiddleware(socket, 'server/');
/* Middlewares Concat */
let middlewares = [thunkMiddleware,socketIoMiddleware];
/* Express server creation */
const app = express()
app.use('/public', express.static(__dirname + '/public'))
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
const crosAPIUrl = 'http://172.24.10.3:8080'
var options = {
target: crosAPIUrl, // target host
changeOrigin: true, // needed for virtual hosted sites
};
var newProxy = proxy(options);
app.use('/lastcalls', newProxy);
app.use('/phonedirectory', newProxy);
app.use('/externalphonedirectory', newProxy);
app.use('/call', newProxy);
app.use('/calls', newProxy);
app.use('/scpost', newProxy);
app.use('/updatephone', newProxy);
app.use('/makeexternalphone', newProxy);
app.use('/searchexternalcall', newProxy);
app.use('/mcphone', newProxy);
app.use('/topdurationcalls', newProxy);
app.use('/authuser', newProxy);
/* configure store */
function configureStore(memoryHistory, initialState){
let store = createStore(
reducer,
initialState,
compose(
applyMiddleware(...middlewares, routerMiddleware(memoryHistory))
)
)
return store
}
const HTML = ({ content, store }) => (
<html>
<head>
<title>Hotel Plaza Meru | Phone Register Client</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.3/toastr.min.css"/>
<link rel='stylesheet' type='text/css' href='public/style.css' />
</head>
<body>
<div id='app' dangerouslySetInnerHTML={{ __html: content }}/>
<script dangerouslySetInnerHTML={{ __html: `window.__initialState__=${serialize(store.getState())};` }}/>
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src='public/vendor.js' />
<script src='public/bundle.js' />
</body>
</html>
)
/* react router */
app.use(function (req, res) {
const memoryHistory = createMemoryHistory(req.path)
let store = configureStore(memoryHistory)
const history = syncHistoryWithStore(memoryHistory, store)
/* react router match history */
match({ history, routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message)
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
const content = renderToString(
<Provider store={store}>
<RouterContext {...renderProps}/>
</Provider>
)
res.send('<!doctype html>\n' + renderToString(<HTML content={content} store={store}/>))
}
})
})
app.listen(8081, function (err) {
if (err) { console.log(err); return; }
console.log('Server listening on http://localhost:8081, Ctrl+C to stop')
})
opener("http://localhost:8081")