Load Web Assets on WeilChain
This article serves as a guide on how to store static assets on the WeilChain and their rendering, i.e. how to use WeilChain as your Content Delivery Network (CDN) for your website assets.
The Webserver Contract
The Webserver contract lays the foundation of storing and rendering these assets. One can either use the smart contract provided by us or they can implement a contract which follows this WIDL interface.
The Webserver contract has the following WIDL interface :
interface WebServer {
mutate func start_file_upload(path: string, total_chunks: u32);
mutate func add_path_content(path: string, chunk: list<u8>, index: u32) -> result<(), string>;
mutate func finish_upload(path: string, size_bytes: u32) -> result<(), string>;
query func total_chunks(path: string) -> result<u32, string>;
query func http_content(path: string, index: u32, method: string) -> tuple<u16, dict<string, string>, list<u8>>;
query func size_bytes(path: string) -> result<u32, string>;
query func get_chunk_size() -> u32;
mutate func set_chunk_size(size: u32)
}
A description of what each method does :
- start_file_upload: Initializes storage for a new file by allocating memory for chunks.
- add_path_content: Stores a single chunk of file data at specified index during upload.
- finish_upload: Marks file upload as complete and stores final file size.
- total_chunks: Returns the total number of chunks for a given file.
- http_content: Serves file content with appropriate status codes and headers.
- size_bytes: Returns the total size in bytes of a stored file.
- get_chunk_size: Returns current chunk size setting with unit as bytes.
- set_chunk_size: Updates the chunk size setting used for new uploads.
CLI usage
The CLI command load_assets
uses the webserver contract interface methods to load these assets on the WeilChain. Instructions on using this command :
- Deploy your Webserver contract.
- Run :
load_assets -a <webserver_contract_address> -d <directory_path>
, where the directory path here is to the one which contains your static assets, and it should be the absolute path Your assets are now deployed on the chain and can be retrieved by hitting the sentinel server as discussed in the following section.
Rendering of Assets
Hitting the endpoint : <sentinel-address>/<contract_address>/static_assets/<path-to-static-asset>
will return a response with octet stream (i.e. a byte array) for a valid asset path.
Note : path should be relative to the directory. For instance , for a file with root path : ..../asset_dir/index.html
, the path provided in the api should be index.html
And there you go! You can now use WeilChain as the CDN for your website with all the advantages of a blockchain.