Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Genesys PGR
Genesys Website
Commits
556a73e9
Commit
556a73e9
authored
Apr 02, 2019
by
Viacheslav Pavlov
Committed by
Matija Obreza
Apr 09, 2019
Browse files
Ensure that all serverStorage data loaded before app startup
parent
cd279a7b
Changes
4
Hide whitespace changes
Inline
Side-by-side
server/index.ts
View file @
556a73e9
...
...
@@ -15,24 +15,25 @@ const PORT = process.env.PORT || 3000;
configureBackendApi
({
apiUrl
:
`http://localhost:
${
PORT
}
/proxy`
});
const
refreshAppToken
=
()
=>
{
LoginService
.
loginApp
(
config
.
clientId
).
then
((
token
)
=>
{
return
LoginService
.
loginApp
(
config
.
clientId
,
config
.
clientSecret
,
config
.
apiUrl
).
then
(
async
(
token
)
=>
{
console
.
log
(
'
Received token:
\n
'
,
token
);
configureBackendApi
({
accessToken
:
token
.
access_token
});
const
refreshIn
=
(
token
.
expires_in
-
30
)
*
1000
;
console
.
log
(
`Scheduling app token refresh in
${
refreshIn
}
s`
);
config
.
access_token
=
token
;
setTimeout
(
refreshAppToken
,
refreshIn
);
refreshServerStorage
();
await
refreshServerStorage
();
}).
catch
((
axiosError
)
=>
{
console
.
log
(
`Error refreshing app token:
${
axiosError
.
response
.
statusText
}
`
,
axiosError
.
response
);
});
};
refreshAppToken
();
// start server on PORT
Loadable
.
preloadAll
().
then
(()
=>
{
server
.
listen
(
PORT
,
()
=>
{
console
.
log
(
'
HTTP server listening on:
'
+
PORT
);
});
});
refreshAppToken
()
.
then
(()
=>
{
// start server on PORT
Loadable
.
preloadAll
().
then
(()
=>
{
server
.
listen
(
PORT
,
()
=>
{
console
.
log
(
'
HTTP server listening on:
'
+
PORT
);
});
});
});
server/middleware/serverStorage.ts
View file @
556a73e9
// constants
import
'
babel-polyfill
'
;
import
{
RECEIVE_CROPS
}
from
'
crop/constants
'
;
import
{
GET_SERVER_INFO
}
from
'
constants/serverInfo
'
;
// model
...
...
@@ -7,6 +8,9 @@ import ApiCall from 'model/ApiCall';
import
{
CropService
}
from
'
service/CropService
'
;
import
ApiInfoService
from
'
service/genesys/ApiInfoService
'
;
// server
import
config
from
'
../config
'
;
const
REFRESH_TIME
=
15
*
60
*
1000
;
export
const
serverStorage
=
{
...
...
@@ -20,19 +24,31 @@ export const serverStorage = {
},
};
const
directXhrConfig
=
{
baseURL
:
config
.
apiUrl
,
};
const
refreshCrops
=
()
=>
{
CropService
.
listCrops
()
.
then
((
crops
)
=>
serverStorage
.
crops
.
data
=
{
apiCall
:
ApiCall
.
success
(
crops
)});
const
start
=
Date
.
now
();
return
CropService
.
listCrops
(
directXhrConfig
)
.
then
((
crops
)
=>
{
console
.
log
(
`Crops fetched in
${
Date
.
now
()
-
start
}
ms`
);
serverStorage
.
crops
.
data
=
{
apiCall
:
ApiCall
.
success
(
crops
)};
});
};
const
refreshServerInfo
=
()
=>
{
ApiInfoService
.
apiInfo
()
.
then
((
serverInfo
)
=>
serverStorage
.
serverInfo
.
data
=
{
info
:
serverInfo
});
const
start
=
Date
.
now
();
return
ApiInfoService
.
apiInfo
(
directXhrConfig
)
.
then
((
serverInfo
)
=>
{
console
.
log
(
`Server info fetched in
${
Date
.
now
()
-
start
}
ms`
);
serverStorage
.
serverInfo
.
data
=
{
info
:
serverInfo
};
});
};
export
const
refreshServerStorage
=
()
=>
{
refreshCrops
();
refreshServerInfo
();
export
const
refreshServerStorage
=
async
()
=>
{
await
refreshCrops
();
await
refreshServerInfo
();
console
.
log
(
'
Server storage refresh done
'
);
setTimeout
(
refreshServerStorage
,
REFRESH_TIME
);
};
src/actions/ApiCall.ts
View file @
556a73e9
...
...
@@ -39,7 +39,7 @@ export const createApiCaller = (method, payloadType: string, config: {direct?: b
xhrConfig
.
baseURL
=
getState
().
applicationConfig
.
apiUrl
;
}
if
(
config
.
direc
t
)
{
if
(
config
.
withoutTimeou
t
)
{
xhrConfig
.
timeout
=
0
;
}
...
...
@@ -95,7 +95,7 @@ export const createPureApiCaller = (method, config: {direct?: boolean, withoutTi
xhrConfig
.
baseURL
=
getState
().
applicationConfig
.
apiUrl
;
}
if
(
config
.
direc
t
)
{
if
(
config
.
withoutTimeou
t
)
{
xhrConfig
.
timeout
=
0
;
}
...
...
src/service/LoginService.ts
View file @
556a73e9
...
...
@@ -14,9 +14,9 @@ export const VERIFY_GOOGLE_TOKEN_URL = `/api/google/verify-token`;
export
class
LoginService
{
public
static
loginApp
(
clientId
:
string
,
clientSecret
?:
string
)
{
public
static
loginApp
(
clientId
:
string
,
clientSecret
?:
string
,
baseURL
?:
string
)
{
return
axiosBackend
.
post
(
LOGIN_URL
,
null
,
{
//
baseURL: '/proxy',
baseURL
:
baseURL
||
'
/proxy
'
,
params
:
{
grant_type
:
'
client_credentials
'
,
client_id
:
clientId
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment