-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove tagcloud which is no longer neeed
- Loading branch information
1 parent
f8208c2
commit 21065e0
Showing
5 changed files
with
147 additions
and
10 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
step-tools/src/main/resources/cloudflare_caching/createhomepagecache
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,103 @@ | ||
addEventListener('fetch', event => { | ||
event.respondWith(handleRequest(event.request)) | ||
}) | ||
|
||
const url = "https://www.stepbible.org/?" | ||
async function gatherResponse(response) { | ||
const { headers } = response | ||
const contentType = headers.get("content-type") || "" | ||
if (contentType.includes("application/json")) { | ||
console.log("json") | ||
return JSON.stringify(await response.json()) | ||
} | ||
else if (contentType.includes("application/text")) { | ||
console.log("application") | ||
return response.text() | ||
} | ||
else if (contentType.includes("text/html")) { | ||
return response.text() | ||
} | ||
else { | ||
console.log("other") | ||
return response.text() | ||
} | ||
} | ||
|
||
async function handleRequest(request) { | ||
const userurl = request.url | ||
|
||
// Function to parse query strings | ||
function getParameterByName(name) { | ||
name = name.replace(/[\[\]]/g, '\\$&') | ||
name = name.replace(/\//g, '') | ||
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), | ||
results = regex.exec(userurl) | ||
|
||
if (!results) return null | ||
else if (!results[2]) return '' | ||
else if (results[2]) { | ||
results[2] = results[2].replace(/\//g, '') | ||
} | ||
|
||
return decodeURIComponent(results[2].replace(/\+/g, ' ')); | ||
} | ||
|
||
const langs = ["en", "es", "zh", "zh-tw", "aa", "af", "ar", "az", "be", "bg", "bn", "cs", "cy", "de", "el", "et", "fa", "fi", "fr", "ga", "gl", "gu", "hi", "hr", "hu", "id", "in", "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "ml", "ms", "mt", "nl", "no", "pl", "pt", "ro", "ru", "ry", "sk", "sl", "sq", "sr", "sv", "sw", "ta", "te", "th", "tr", "uk", "ur", "uz", "vi"]; | ||
const respInit = { | ||
headers: { | ||
"content-type": "text/html;charset=UTF-8", | ||
"accept-language": "en" | ||
}, | ||
} | ||
const languageSelected = getParameterByName('lang') | ||
if ((typeof languageSelected === "string") && (langs.indexOf(languageSelected) > -1)) { | ||
var init = {} | ||
init = { | ||
headers: { | ||
"content-type": "text/html;charset=UTF-8", | ||
"accept-language": languageSelected | ||
}, | ||
} | ||
const response = await fetch(url, init) | ||
var results = await gatherResponse(response) | ||
var resultsLines = results.split("\n") | ||
var cfScriptRemoved = "" | ||
var newResults = "" | ||
var linesAdded = 0 | ||
for (var i = 0; i < resultsLines.length; i++ ) { | ||
var curLine = resultsLines[i].trim() | ||
if ((curLine.indexOf("<script>") == 0) && | ||
(curLine.indexOf("https://static.cloudflareinsights.com/beacon.min.js") > -1) && | ||
(curLine.endsWith("</script>"))) { | ||
cfScriptRemoved = ", Cloudflare script was removed" | ||
} | ||
else if (curLine !== "") { | ||
newResults += curLine + "\n" | ||
linesAdded ++ | ||
} | ||
} | ||
await wwwcache.put(languageSelected, newResults) | ||
return new Response("added " + linesAdded + " out of " + resultsLines.length + " lines for language " + languageSelected + " to Cloudflare KV " + cfScriptRemoved, respInit) | ||
} | ||
else { | ||
var deleteName = getParameterByName('deleteKV') | ||
if (typeof deleteName === "string") { | ||
var deleteNames = [] | ||
if (deleteName === "all") { | ||
deleteNames = langs | ||
} | ||
else | ||
deleteNames = [ deleteName ] | ||
for (var i = 0; i < deleteNames.length; i++) { | ||
await wwwcache.delete(deleteNames[i]) | ||
} | ||
return new Response("deleted " + deleteName + " from Cloudflare KV", respInit) | ||
} | ||
} | ||
return new Response("don't understand your request", respInit) | ||
|
||
} | ||
|
||
addEventListener("fetch", event => { | ||
return event.respondWith(handleRequest()) | ||
}) |
33 changes: 33 additions & 0 deletions
33
step-tools/src/main/resources/cloudflare_caching/sendcachedhomepage
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,33 @@ | ||
export default { | ||
async fetch(request, env) { | ||
var ul = request.headers.get("Accept-Language"); | ||
if (typeof ul !== "string") | ||
ul = "en" | ||
const ulToCheck = ul.toLowerCase() | ||
const ul2 = ulToCheck.substring(0,2) | ||
var foundLang = ""; | ||
if ((ulToCheck.length >= 5) && (ul2 === "zh")) { | ||
const ul5 = ulToCheck.substring(2,5) | ||
if ((ul5 === "_tw") || (ul5 === "-tw") || (ul5 === "_hk") || (ul5 === "-hk")) | ||
foundLang = 'zh-tw'; | ||
} | ||
const langs = ["en", "es", "zh", "zh-tw", "aa", "af", "ar", "az", "be", "bg", "bn", "cs", "cy", "de", "el", "et", "fa", "fi", "fr", "ga", "gl", "gu", "hi", "hr", "hu", "id", "in", "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "ml", "ms", "mt", "nl", "no", "pl", "pt", "ro", "ru", "ry", "sk", "sl", "sq", "sr", "sv", "sw", "ta", "te", "th", "tr", "uk", "ur", "uz", "vi"]; | ||
var redirectCode = 301 | ||
if ((foundLang === "") && (langs.indexOf(ul2) > -1)) | ||
foundLang = ul2 | ||
if (foundLang !== "") { | ||
var pageForUser = await env.wwwcache.get(foundLang, { cacheTtl: 4800 }) | ||
if ((typeof pageForUser === "string") && (pageForUser.length > 6000)) { | ||
const respInit = { | ||
headers: { | ||
"content-type": "text/html;charset=UTF-8", | ||
"cache-control": "no-cache", | ||
}, | ||
} | ||
return new Response(pageForUser, respInit) | ||
} | ||
else redirectCode = 302 // If redirect is needed, it is a temporary redirect because it is a language supported by STEP but does not have a KV for it. | ||
} | ||
return Response.redirect("https://www.stepbible.org/?q=reference=Gen.1", redirectCode); | ||
}, | ||
}; |
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