Skip to content

Commit

Permalink
Added index-html-server lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
rimutaka committed Jul 8, 2024
1 parent 5b063b2 commit 7273995
Show file tree
Hide file tree
Showing 22 changed files with 622 additions and 95 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"

[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-gnu-gcc"
5 changes: 5 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
edition = "2021"
# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#merge_imports
# imports_granularity = "Crate"
# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#max_width
max_width = 120
24 changes: 24 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[workspace]
members = ["isbn_wasm_mod", "lambdas/index-html-server"]
resolver = "2"

[workspace.dependencies]
lambda_runtime = { version = "0.12", default-features = false }
tokio = { version = "1.16", features = [
"macros",
"io-util",
"sync",
"rt-multi-thread",
] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
aws-config = { version = "1.1.7", features = ["behavior-version-latest"] }
aws_lambda_events = "0.15.1"
aws-sdk-s3 = "1.40.0"
regex = "1.10.5"

[profile.release]
lto = true
codegen-units = 1
8 changes: 4 additions & 4 deletions build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"files": {
"main.css": "./static/css/main.00b9f5c0.css",
"main.js": "./static/js/main.3ab7bc97.js",
"static/media/isbn_mod_bg.wasm": "./static/media/isbn_mod_bg.c9c44fb02c839a858597.wasm",
"main.js": "./static/js/main.8af119ea.js",
"static/media/isbn_mod_bg.wasm": "./static/media/isbn_mod_bg.7eb38663a2358fff1703.wasm",
"static/media/welcome-bg.jpg": "./static/media/welcome-bg.b44b41f244b5885c2660.jpg",
"index.html": "./index.html",
"main.00b9f5c0.css.map": "./static/css/main.00b9f5c0.css.map",
"main.3ab7bc97.js.map": "./static/js/main.3ab7bc97.js.map"
"main.8af119ea.js.map": "./static/js/main.8af119ea.js.map"
},
"entrypoints": [
"static/css/main.00b9f5c0.css",
"static/js/main.3ab7bc97.js"
"static/js/main.8af119ea.js"
]
}
2 changes: 1 addition & 1 deletion build/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="description" content="Record and share books you come across"><meta name="referrer" content="no-referrer"/><title>📖📚📚</title><link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png"/><link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png"><meta name="theme-color" content="#ffffff"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta id="ogImage" property="og:image" content="/img/og-thumb.png"/><meta id="ogTitle" property="og:title" content="Scan ISBN to record or share a book"/><script defer="defer" src="/static/js/main.3ab7bc97.js"></script><link href="/static/css/main.00b9f5c0.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="app"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="description" content="Record and share books you come across"><meta name="referrer" content="no-referrer"/><title>📖📚📚</title><link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png"/><link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png"><meta name="theme-color" content="#ffffff"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta id="ogImage" property="og:image" content="/img/og-thumb.png"/><meta id="ogTitle" property="og:title" content="Scan ISBN to record or share a book"/><script defer="defer" src="/static/js/main.8af119ea.js"></script><link href="/static/css/main.00b9f5c0.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="app"></div></body></html>
1 change: 0 additions & 1 deletion build/static/js/main.3ab7bc97.js.map

This file was deleted.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions build/static/js/main.8af119ea.js.map

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion build/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if ("function" === typeof importScripts) {
self.addEventListener("install", event => {
self.skipWaiting();
});
workbox.precaching.precacheAndRoute([{"revision":"e4329aba85db159b3976353ca2e778a4","url":"asset-manifest.json"},{"revision":"4020571efe44dc33d271798e6a18e0c1","url":"favicon.ico"},{"revision":"f98923403f5c78d689d56b18f947c520","url":"img/apple-touch-icon.png"},{"revision":"a62aa63bb4d0a3dd08820787bd7e118f","url":"img/favicon-16x16.png"},{"revision":"a5229a03fcfe584a3031846fe3c19ccf","url":"img/favicon-32x32.png"},{"revision":"95cfc853673e32a2a56ffc8c2c45d19a","url":"img/og-thumb.png"},{"revision":"3a956cc59773f900299ffeb18f519a7f","url":"index.html"},{"revision":"dc1b0d9c8cbf227e335faa82ab5c5327","url":"static/css/main.00b9f5c0.css"},{"revision":"b6543bcefa150c012146212be0ae0643","url":"static/js/main.3ab7bc97.js"},{"revision":"13bdae9cd33075f496b86b0e244dfa1c","url":"static/media/isbn_mod_bg.c9c44fb02c839a858597.wasm"},{"revision":"24f2b115d3964c9f977462cdd38b066a","url":"wasm/koder.js"},{"revision":"6f11e7db4fe9aca82cac7150bfc33769","url":"wasm/zbar.js"},{"revision":"e8789bf03df9c2c85e9c59ab0a0cd0c6","url":"wasm/zbar.wasm"},{"revision":"bb1c649a95ffa80369254cc3e51b9a41","url":"wasmWorker.js"}]);
workbox.precaching.precacheAndRoute([{"revision":"1133096324be1792e95f83a1562b40e4","url":"asset-manifest.json"},{"revision":"4020571efe44dc33d271798e6a18e0c1","url":"favicon.ico"},{"revision":"f98923403f5c78d689d56b18f947c520","url":"img/apple-touch-icon.png"},{"revision":"a62aa63bb4d0a3dd08820787bd7e118f","url":"img/favicon-16x16.png"},{"revision":"a5229a03fcfe584a3031846fe3c19ccf","url":"img/favicon-32x32.png"},{"revision":"95cfc853673e32a2a56ffc8c2c45d19a","url":"img/og-thumb.png"},{"revision":"294d25d6d1c02316e41eb0c4e2da82f6","url":"index.html"},{"revision":"dc1b0d9c8cbf227e335faa82ab5c5327","url":"static/css/main.00b9f5c0.css"},{"revision":"da9344208bcf9419b43492b41d3e3702","url":"static/js/main.8af119ea.js"},{"revision":"b8e3f8d253137c82b6a17c1b92b49faf","url":"static/media/isbn_mod_bg.7eb38663a2358fff1703.wasm"},{"revision":"24f2b115d3964c9f977462cdd38b066a","url":"wasm/koder.js"},{"revision":"6f11e7db4fe9aca82cac7150bfc33769","url":"wasm/zbar.js"},{"revision":"e8789bf03df9c2c85e9c59ab0a0cd0c6","url":"wasm/zbar.wasm"},{"revision":"bb1c649a95ffa80369254cc3e51b9a41","url":"wasmWorker.js"}]);
workbox.routing.registerRoute(
new RegExp("https://fonts.(?:.googlepis|gstatic).com/(.*)"),
new workbox.strategies.CacheFirst({
Expand Down
4 changes: 2 additions & 2 deletions isbn_wasm_mod/src/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct ImageLinks {
/// Part of GoogleBooks API response
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub(crate) struct VolumeInfo {
pub struct VolumeInfo {
pub title: String,
#[serde(default = "Vec::new")]
pub authors: Vec<String>,
Expand Down Expand Up @@ -88,7 +88,7 @@ pub struct Volumes {
}

/// Fetches book data from Google Books API
pub async fn get_book_data(isbn: &str, runtime: &BrowserRuntime) -> super::Result<Volumes> {
pub(crate) async fn get_book_data(isbn: &str, runtime: &BrowserRuntime) -> super::Result<Volumes> {
log!("get_book_data for: {isbn}");

let url = format!("https://www.googleapis.com/books/v1/volumes?q=isbn:{isbn}");
Expand Down
6 changes: 5 additions & 1 deletion isbn_wasm_mod/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use utils::{WasmResponse, WasmResult};
use wasm_bindgen::prelude::*;
use web_sys::{Window, WorkerGlobalScope};

mod google;
pub mod google;
#[macro_use]
pub(crate) mod utils;

Expand Down Expand Up @@ -35,6 +35,10 @@ pub fn hello_wasm() {
log!("Hello from WASM!");
}

pub fn hello_lambda() {
log!("Hello from Lambda!");
}

/// A demo function to test if WASM is callable from background.js
#[wasm_bindgen]
pub fn log_isbn(isbn: String) {
Expand Down
21 changes: 21 additions & 0 deletions lambdas/index-html-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "index-html-server"
version = "0.2.0"
authors = ["rimutaka <[email protected]>"]
edition = "2021"
description = "Returns modified index.html from S3"
license = "AGPL-3.0"

[dependencies]
wasm_mod = { path = "../../isbn_wasm_mod" }
tokio = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
lambda_runtime = { workspace = true }
aws_lambda_events = { workspace = true }
aws-sdk-s3 = { workspace = true }
aws-config = { workspace = true }
regex = { workspace = true }
reqwest = { version = "0.12", features = ["json", "rustls-tls-native-roots"] }
114 changes: 114 additions & 0 deletions lambdas/index-html-server/apigw-req.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"body": null,
"headers": {
"Host": "mqd1wllexd.execute-api.us-east-1.amazonaws.com",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0",
"X-Amzn-Trace-Id": "Root=1-66872833-6f9600b6174d52360b458d06",
"X-Forwarded-For": "206.83.103.59",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "en-US,en;q=0.5",
"dnt": "1",
"priority": "u=1",
"referer": "https://us-east-1.console.aws.amazon.com/",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "cross-site",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1"
},
"httpMethod": "GET",
"isBase64Encoded": false,
"multiValueHeaders": {
"Host": [
"mqd1wllexd.execute-api.us-east-1.amazonaws.com"
],
"User-Agent": [
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0"
],
"X-Amzn-Trace-Id": [
"Root=1-66872833-6f9600b6174d52360b458d06"
],
"X-Forwarded-For": [
"206.83.103.59"
],
"X-Forwarded-Port": [
"443"
],
"X-Forwarded-Proto": [
"https"
],
"accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"
],
"accept-encoding": [
"gzip, deflate, br, zstd"
],
"accept-language": [
"en-US,en;q=0.5"
],
"dnt": [
"1"
],
"priority": [
"u=1"
],
"referer": [
"https://us-east-1.console.aws.amazon.com/"
],
"sec-fetch-dest": [
"document"
],
"sec-fetch-mode": [
"navigate"
],
"sec-fetch-site": [
"cross-site"
],
"sec-fetch-user": [
"?1"
],
"upgrade-insecure-requests": [
"1"
]
},
"multiValueQueryStringParameters": null,
"path": "/understanding-caricature-by-greg-houston/9781580935647/",
"pathParameters": null,
"queryStringParameters": null,
"requestContext": {
"accountId": "512295225992",
"apiId": "mqd1wllexd",
"deploymentId": "tafunn",
"domainName": "mqd1wllexd.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "mqd1wllexd",
"extendedRequestId": "aaM4FE9IoAMEWzA=",
"httpMethod": "GET",
"identity": {
"accessKey": null,
"accountId": null,
"caller": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"principalOrgId": null,
"sourceIp": "206.83.103.59",
"user": null,
"userAgent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0",
"userArn": null
},
"path": "/dev/understanding-caricature-by-greg-houston/9781580935647/",
"protocol": "HTTP/1.1",
"requestId": "f69e3e2a-c5d7-48aa-89eb-e56a05411534",
"requestTime": "04/Jul/2024:22:54:43 +0000",
"requestTimeEpoch": 1720133683448,
"resourceId": "p0yn8b2fu0",
"resourcePath": "/",
"stage": "dev"
},
"resource": "/",
"stageVariables": null
}
9 changes: 9 additions & 0 deletions lambdas/index-html-server/apigw-resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cookies": [],
"isBase64Encoded": false,
"statusCode": 200,
"headers": {
"content-type": "text/text"
},
"body": "Hello from Lambda!"
}
97 changes: 97 additions & 0 deletions lambdas/index-html-server/src/google.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/// Logic for fetching book data from Google Books API
///
/// Volume search by ISBN:
/// - https://www.googleapis.com/books/v1/volumes?q=isbn:9781761186769
///
/// Response: isbn_wasm_mod/data-samples/google-books-volume.json
///
///
///
//
use super::BrowserRuntime;
use crate::utils::{execute_http_request, log};

use serde::{Deserialize, Serialize};

// /// Part of GoogleBooks API response
// #[derive(Deserialize, Serialize, Debug)]
// #[serde(rename_all = "camelCase")]
// pub struct IndustryIdentifier {
// pub r#type: String,
// pub identifier: String,
// }

/// Part of GoogleBooks API response
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ImageLinks {
pub small_thumbnail: String,
pub thumbnail: String,
}

// /// Part of GoogleBooks API response
// #[derive(Deserialize, Serialize, Debug)]
// #[serde(rename_all = "camelCase")]
// pub struct ListPrice {
// #[serde(skip_serializing_if = "Option::is_none")]
// pub amount: Option<f64>,
// #[serde(skip_serializing_if = "Option::is_none")]
// pub currency_code: Option<String>,
// }

// /// Part of GoogleBooks API response
// #[derive(Deserialize, Serialize, Debug)]
// #[serde(rename_all = "camelCase")]
// pub struct SaleInfo {
// #[serde(skip_serializing_if = "Option::is_none")]
// pub list_price: Option<ListPrice>,
// }

/// Part of GoogleBooks API response
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct VolumeInfo {
pub title: String,
#[serde(default = "Vec::new")]
pub authors: Vec<String>,
// #[serde(skip_serializing_if = "Option::is_none")]
// pub description: Option<String>,
// #[serde(default = "Vec::new")]
// pub industry_identifiers: Vec<IndustryIdentifier>,
// #[serde(skip_serializing_if = "Option::is_none")]
// pub page_count: Option<i64>,
// #[serde(default = "Vec::new")]
// pub categories: Vec<String>,
// #[serde(skip_serializing_if = "Option::is_none")]
pub image_links: Option<ImageLinks>,
}

/// Part of GoogleBooks API response
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Volume {
pub id: String,
pub self_link: String,
pub volume_info: VolumeInfo,
// #[serde(skip_serializing_if = "Option::is_none")]
// pub sale_info: Option<SaleInfo>,
}

/// The root of GoogleBooks API response
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Volumes {
pub kind: String,
pub total_items: i64,
#[serde(default = "Vec::new")]
pub items: Vec<Volume>,
}

/// Fetches book data from Google Books API
pub(crate) async fn get_book_data(isbn: &str, runtime: &BrowserRuntime) -> super::Result<Volumes> {
log!("get_book_data for: {isbn}");

let url = format!("https://www.googleapis.com/books/v1/volumes?q=isbn:{isbn}");

execute_http_request::<Volumes, u8>(&url, None, runtime).await
}
Loading

0 comments on commit 7273995

Please sign in to comment.