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
GGCE
GGCE Web
Commits
87686331
Commit
87686331
authored
Sep 24, 2021
by
Matija Obreza
Browse files
Server: Require HTTP cookies for accessing GG-CE Web
parent
e50fc675
Changes
1
Hide whitespace changes
Inline
Side-by-side
workspaces/ui-express/server/server.ts
View file @
87686331
import
'
core-js/stable
'
;
import
'
regenerator-runtime/runtime
'
;
import
languages
from
'
@gringlobal-ce/i18n/data/Languages
'
;
/**
* Express HTTP server configuration
...
...
@@ -54,7 +55,6 @@ app.get('/en/*', (req, res) => {
res
.
redirect
(
301
,
`
${
config
.
frontendPath
}${
redirectPath
}
`
);
});
// Handle sitemap.xml and references files
app
.
get
(
'
/sitemap*.xml
'
,
sitemap
);
// Enable compression
...
...
@@ -107,14 +107,74 @@ app.use(express.static(path.join('../assets'), {
immutable
:
true
,
}));
// Parse cookies
app
.
use
(
cookieParser
());
const
HELLO_COOKIE
=
`GGCEid`
;
const
addHelloCookie
=
(
req
,
res
)
=>
{
console
.
log
(
`You get a cookie,
${
req
.
ip
}
gets a cookie, everyone gets a cookie!`
);
res
.
cookie
(
HELLO_COOKIE
,
1
,
{
path
:
config
?.
frontendPath
?.
length
?
config
.
frontendPath
:
undefined
,
secure
:
req
.
secure
||
undefined
,
httpOnly
:
true
,
sameSite
:
req
.
secure
?
'
Strict
'
as
const
:
undefined
,
});
}
// Fine, look at Accept-Language header
app
.
use
((
req
,
res
,
next
)
=>
{
// If user hits the server without cookies
if
(
!
req
.
cookies
[
HELLO_COOKIE
])
{
// Bring them to the dark side with our cookie
addHelloCookie
(
req
,
res
);
// Send them to their preferred and GG-CE supported localized version
if
(
req
.
url
===
'
/
'
)
{
console
.
log
(
'
Inspecting l10n request with no cookies, url:
'
,
req
.
url
,
req
.
cookies
);
const
acceptedLangs
=
req
.
header
(
'
Accept-Language
'
)?.
split
(
'
,
'
)
||
null
;
if
(
acceptedLangs
?.
length
)
{
const
ggceLang
=
acceptedLangs
// toLowerCase: zh-tw
.
map
((
preferredLang
)
=>
preferredLang
.
toLowerCase
())
// map to GG-CE supported lang
.
map
((
preferredLang
)
=>
languages
.
find
((
a
)
=>
a
.
short
.
toLowerCase
()
===
preferredLang
))
// filter out unsupported GG-CE langs
.
filter
((
a
)
=>
a
!==
null
)[
0
]
||
languages
[
0
];
console
.
log
(
`User defaults to
${
acceptedLangs
[
0
]}
, supported locale is
${
ggceLang
.
short
}
`
);
if
(
ggceLang
?.
short
!==
'
en
'
)
{
// Redirect to /lang/
console
.
log
(
`Redirecting to
${
config
.
frontendPath
}
/
${
ggceLang
.
short
}
/`
);
res
.
redirect
(
307
,
`
${
config
.
frontendPath
}
/
${
ggceLang
.
short
}
/`
);
return
;
// done
}
else
{
// 'en' redirects to /
console
.
log
(
`Redirecting to
${
config
.
frontendPath
}
/`
);
res
.
redirect
(
307
,
`
${
config
.
frontendPath
}
/`
);
return
;
// done
}
}
else
{
// Weird, no Accepted-Lang header :-?
console
.
log
(
`Hey,
${
req
.
ip
}
come again to
${
req
.
path
}
, but say what language you want!`
);
res
.
status
(
307
).
location
(
req
.
path
).
send
(
`Hey human @
${
req
.
ip
}
, visit us! But say what language you want!`
);
return
;
// done
}
}
else
{
console
.
log
(
`Hey,
${
req
.
ip
}
come again to
${
req
.
path
}
`
);
res
.
status
(
307
).
location
(
req
.
path
).
send
(
`Hey human @
${
req
.
ip
}
, visit us!`
);
return
;
}
}
// They are on the dark cookie side!
next
();
});
const
localeModules
=
[
'
express
'
,
'
client
'
,
'
common
'
];
const
i18nS
=
i18nServer
(
localeModules
);
// Register i18n
app
.
use
(
i18nextMiddleware
.
handle
(
i18nS
));
// Parse cookies
app
.
use
(
cookieParser
());
// Relay requests to React
app
.
use
(
prerenderer
(
html
,
errHtml
));
...
...
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