-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
browser assets to be served via the cdn (#3683)
more stuff offloaded from byond's slowness the better thank you mso tgstation/tgstation#52681 🆑 server: server's can now support using a cdn for web assets /🆑
- Loading branch information
Showing
9 changed files
with
195 additions
and
5 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
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
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,87 @@ | ||
/// CDN Webroot asset transport. | ||
/datum/asset_transport/webroot | ||
name = "CDN Webroot asset transport" | ||
|
||
/datum/asset_transport/webroot/Load() | ||
if (validate_config(log = FALSE)) | ||
load_existing_assets() | ||
|
||
/// Processes thru any assets that were registered before we were loaded as a transport. | ||
/datum/asset_transport/webroot/proc/load_existing_assets() | ||
for (var/asset_name in SSassets.cache) | ||
var/datum/asset_cache_item/ACI = SSassets.cache[asset_name] | ||
save_asset_to_webroot(ACI) | ||
|
||
/// Register a browser asset with the asset cache system | ||
/// We also save it to the CDN webroot at this step instead of waiting for send_assets() | ||
/// asset_name - the identifier of the asset | ||
/// asset - the actual asset file or an asset_cache_item datum. | ||
/datum/asset_transport/webroot/register_asset(asset_name, asset) | ||
. = ..() | ||
var/datum/asset_cache_item/ACI = . | ||
|
||
if (istype(ACI) && ACI.hash) | ||
save_asset_to_webroot(ACI) | ||
|
||
/// Saves the asset to the webroot taking into account namespaces and hashes. | ||
/datum/asset_transport/webroot/proc/save_asset_to_webroot(datum/asset_cache_item/ACI) | ||
var/webroot = CONFIG_GET(string/asset_cdn_webroot) | ||
var/newpath = "[webroot][get_asset_suffex(ACI)]" | ||
if (fexists(newpath)) | ||
return | ||
if (fexists("[newpath].gz")) //its a common pattern in webhosting to save gzip'ed versions of text files and let the webserver serve them up as gzip compressed normal files, sometimes without keeping the original version. | ||
return | ||
return fcopy(ACI.resource, newpath) | ||
|
||
/// Returns a url for a given asset. | ||
/// asset_name - Name of the asset. | ||
/// asset_cache_item - asset cache item datum for the asset, optional, overrides asset_name | ||
/datum/asset_transport/webroot/get_asset_url(asset_name, datum/asset_cache_item/asset_cache_item) | ||
if (!istype(asset_cache_item)) | ||
asset_cache_item = SSassets.cache[asset_name] | ||
var/url = CONFIG_GET(string/asset_cdn_url) //config loading will handle making sure this ends in a / | ||
return "[url][get_asset_suffex(asset_cache_item)]" | ||
|
||
/datum/asset_transport/webroot/proc/get_asset_suffex(datum/asset_cache_item/asset_cache_item) | ||
var/base = "[copytext(asset_cache_item.hash, 1, 3)]/" | ||
var/filename = "asset.[asset_cache_item.hash][asset_cache_item.ext]" | ||
if (length(asset_cache_item.namespace)) | ||
base = "namespaces/[copytext(asset_cache_item.namespace, 1, 3)]/[asset_cache_item.namespace]/" | ||
if (!asset_cache_item.namespace_parent) | ||
filename = "[asset_cache_item.name]" | ||
return base + filename | ||
|
||
|
||
/// webroot asset sending - does nothing unless passed legacy assets | ||
/datum/asset_transport/webroot/send_assets(client/client, list/asset_list) | ||
. = FALSE | ||
var/list/legacy_assets = list() | ||
if (!islist(asset_list)) | ||
asset_list = list(asset_list) | ||
for (var/asset_name in asset_list) | ||
var/datum/asset_cache_item/ACI = asset_list[asset_name] | ||
if (!istype(ACI)) | ||
ACI = SSassets.cache[asset_name] | ||
if (!ACI) | ||
legacy_assets += asset_name //pass it on to base send_assets so it can output an error | ||
continue | ||
if (ACI.legacy) | ||
legacy_assets[asset_name] = ACI | ||
if (length(legacy_assets)) | ||
. = ..(client, legacy_assets) | ||
|
||
|
||
/// webroot slow asset sending - does nothing. | ||
/datum/asset_transport/webroot/send_assets_slow(client/client, list/files, filerate) | ||
return FALSE | ||
|
||
/datum/asset_transport/webroot/validate_config(log = TRUE) | ||
if (!CONFIG_GET(string/asset_cdn_url)) | ||
if (log) | ||
log_asset("ERROR: [type]: Invalid Config: ASSET_CDN_URL") | ||
return FALSE | ||
if (!CONFIG_GET(string/asset_cdn_webroot)) | ||
if (log) | ||
log_asset("ERROR: [type]: Invalid Config: ASSET_CDN_WEBROOT") | ||
return FALSE | ||
return TRUE |
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,40 @@ | ||
# External resources | ||
# Set this to the location of a .zip with the server's .rsc inside of it. | ||
# If you set this mutiple times, the server will rotate between the links. | ||
# To use this, the compile option PRELOAD_RSC must be set to 0 to keep byond from preloading resources | ||
# Resource urls can not be encrypted (https://), as they are downloaded by byond, not IE, and byond can't into encryption | ||
|
||
EXTERNAL_RSC_URLS http://rsc.cm-ss13.com/ | ||
|
||
|
||
######################## | ||
# Browser Asset Config # | ||
######################## | ||
# Browser assets are any file included in interfaces. css, images, javascript, etc. | ||
# This handles configuring how we get these to the player so interfaces can access them. | ||
|
||
# Asset Transport | ||
# The normal way of getting assets to clients is to use the internal byond system. This can be slow and delay the opening of interface windows. It also doesn't allow the internal IE windows byond uses to cache anything. | ||
# You can instead have the server save them to a website via a folder within the game server that the web server can read. This could be a simple webserver or something backed by a CDN. | ||
# Valid values: simple, webroot. Simple is the default. | ||
#ASSET_TRANSPORT webroot | ||
|
||
|
||
# Simple asset transport configurable values. | ||
|
||
# Uncomment this to have the server passively send all browser assets to each client in the background. (instead of waiting for them to be needed) | ||
# This should be uncommented in production and commented in development | ||
#ASSET_SIMPLE_PRELOAD | ||
|
||
|
||
# Webroot asset transport configurable values. | ||
|
||
# Local folder to save assets to. | ||
# Assets will be saved in the format of asset.MD5HASH.EXT or in namespaces/hash/ as ASSET_FILE_NAME or asset.MD5HASH.EXT | ||
#ASSET_CDN_WEBROOT data/asset-store/ | ||
|
||
# URL the folder from above can be accessed from. | ||
# for best results the webserver powering this should return a long cache validity time, as all assets sent via this transport use hash based urls | ||
# Encryption (https) is supported here, but linux clients will have issues if you require higher then tls 1.0. Windows clients down to windows 7 can handle tls 1.2 no issue. | ||
# if you want to test this locally, you simpily run the `localhost-asset-webroot-server.py` python3 script to host assets stored in `data/asset-store/` via http://localhost:58715/ | ||
#ASSET_CDN_URL http://localhost:58715/ |
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,15 @@ | ||
#!/usr/bin/env python3 | ||
from http.server import HTTPServer, SimpleHTTPRequestHandler | ||
import os | ||
|
||
class CORSRequestHandler(SimpleHTTPRequestHandler): | ||
def end_headers(self): | ||
self.send_header('Access-Control-Allow-Origin', '*') | ||
self.send_header('Access-Control-Allow-Methods', 'GET') | ||
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate') | ||
return super(CORSRequestHandler, self).end_headers() | ||
|
||
os.makedirs('../data/asset-store/', exist_ok=True) | ||
os.chdir('../data/asset-store/') | ||
httpd = HTTPServer(('localhost', 58715), CORSRequestHandler) | ||
httpd.serve_forever() |