-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from ssysm/update
Update
- Loading branch information
Showing
46 changed files
with
2,105 additions
and
596 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,96 @@ | ||
const express = require('express'); | ||
const axios = require('axios'); | ||
|
||
const fetch = require('node-fetch'); | ||
const url = require('url'); | ||
const appUri = "https://starrysea.org/"; | ||
const renderUri = "http://localhost:8080/render"; | ||
const renderUri = "https://rendertron.tes-project.xyz/render"; | ||
|
||
const app = express(); | ||
|
||
app.get('*',(req,res)=>{ | ||
|
||
axios.get(`${renderUri}/${appUri + req.originalUrl}`) | ||
.then((data)=>{ | ||
res.set('Cache-Control','public, max-age=300, s-maxage=600'); | ||
res.send(data.data.toString()); | ||
}) | ||
// Generates the URL | ||
function generateUrl(request) { | ||
return url.format({ | ||
protocol: request.protocol, | ||
host: appUrl, | ||
pathname: request.originalUrl | ||
}); | ||
} | ||
|
||
}); | ||
function detectBot(userAgent) { | ||
// List of bots to target, add more if you'd like | ||
|
||
const bots = [ | ||
// crawler bots | ||
'googlebot', | ||
'bingbot', | ||
'yandexbot', | ||
'duckduckbot', | ||
'slurp', | ||
// link bots | ||
'twitterbot', | ||
'facebookexternalhit', | ||
'linkedinbot', | ||
'embedly', | ||
'baiduspider', | ||
'pinterest', | ||
'slackbot', | ||
'vkShare', | ||
'facebot', | ||
'outbrain', | ||
'W3C_Validator' | ||
] | ||
|
||
const agent = userAgent.toLowerCase(); | ||
|
||
for (const bot of bots) { | ||
if (agent.indexOf(bot) > -1) { | ||
console.log('bot detected', bot, agent) | ||
return true | ||
} | ||
} | ||
|
||
console.log('no bots found') | ||
return false | ||
|
||
} | ||
|
||
|
||
app.get('*', (req, res) => { | ||
|
||
|
||
const isBot = detectBot(req.headers['user-agent']); | ||
|
||
|
||
app.listen(4300); | ||
if (isBot) { | ||
|
||
const botUrl = generateUrl(req); | ||
// If Bot, fetch url via rendertron | ||
|
||
fetch(`${renderUri}/${botUrl}`) | ||
.then(res => res.text() ) | ||
.then(body => { | ||
|
||
// Set the Vary header to cache the user agent, based on code from: | ||
// https://github.com/justinribeiro/pwa-firebase-functions-botrender | ||
res.set('Cache-Control', 'public, max-age=300, s-maxage=600'); | ||
res.set('Vary', 'User-Agent'); | ||
|
||
res.send(body.toString()) | ||
|
||
}); | ||
|
||
} else { | ||
|
||
|
||
// Not a bot, fetch the regular Angular app | ||
// Possibly faster to serve directly from from the functions directory? | ||
fetch(`${appUri}`) | ||
.then(res => res.text()) | ||
.then(body => { | ||
res.send(body.toString()); | ||
}) | ||
} | ||
|
||
}); | ||
|
||
app.listen(4400); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const locale = [ | ||
"en-US", | ||
"ja-JP", | ||
"zh-CN" | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Injectable } from '@angular/core'; | ||
import {environment} from "../../environments/environment"; | ||
|
||
@Injectable() | ||
export class CookieService { | ||
|
||
constructor() { } | ||
|
||
setCookie(cookieName:string,cookieValue:string,expireDays:number) :string { | ||
var d = new Date(); | ||
d.setTime(d.getTime() + (expireDays*24*60*60*1000)); | ||
var expires = "expires=" + d.toUTCString(); | ||
return document.cookie = cookieName + "=" + cookieValue + ";" + expires + ";path=/;domain="+environment.cookieDomain+';'; | ||
} | ||
|
||
|
||
getCookie(cookieName:string): string { | ||
var name = cookieName + "="; | ||
var decodedCookie = decodeURIComponent(document.cookie); | ||
var ca = decodedCookie.split(';'); | ||
for(var i = 0; i < ca.length; i++) { | ||
var c = ca[i]; | ||
while (c.charAt(0) == ' ') { | ||
c = c.substring(1); | ||
} | ||
if (c.indexOf(name) == 0) { | ||
return c.substring(name.length, c.length); | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
deleteCookie(): void{ | ||
var cookies = document.cookie.split(";"); | ||
|
||
for (var i = 0; i < cookies.length; i++) { | ||
var cookie = cookies[i]; | ||
var eqPos = cookie.indexOf("="); | ||
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; | ||
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain="+environment.cookieDomain+';'; | ||
} | ||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.