-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
87 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,4 @@ | |
members = [ | ||
"word_puzzle_cloudflare", | ||
"word_puzzle_generator", | ||
"word_puzzle_spin", | ||
] |
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 @@ | ||
cfdev: | ||
cd word_puzzle_cloudflare; npm run dev | ||
|
||
spin: | ||
cd word_puzzle_spin; spin build; spin up |
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,59 +1,32 @@ | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
use word_puzzle_generator::place_words; | ||
use word_puzzle_generator::{place_words, GeneratorOptions}; | ||
use worker::*; | ||
|
||
mod utils; | ||
|
||
#[derive(Deserialize)] | ||
struct GeneratorOptions { | ||
size: usize, | ||
words: Vec<String>, | ||
} | ||
|
||
#[event(fetch)] | ||
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> { | ||
// Optionally, get more helpful error messages written to the console in the case of a panic. | ||
utils::set_panic_hook(); | ||
|
||
// Optionally, use the Router to handle matching endpoints, use ":name" placeholders, or "*name" | ||
// catch-alls to match on specific patterns. Alternatively, use `Router::with_data(D)` to | ||
// provide arbitrary data that will be accessible in each route via the `ctx.data()` method. | ||
let router = Router::new(); | ||
|
||
// Add as many routes as your Worker needs! Each route will get a `Request` for handling HTTP | ||
// functionality and a `RouteContext` which you can use to and get route parameters and | ||
// Environment bindings like KV Stores, Durable Objects, Secrets, and Variables. | ||
router | ||
.get("/", |_, _| Response::ok("Hello from Workers!")) | ||
.get("/worker-version", |_, ctx| { | ||
let version = ctx.var("WORKERS_RS_VERSION")?.to_string(); | ||
Response::ok(version) | ||
}) | ||
.post_async("/generate", |mut req, _ctx| async move { | ||
match req.json().await { | ||
Err(_) => Response::error("Bad Request", 400), | ||
Ok(options) => { | ||
let options: GeneratorOptions = options; | ||
if options.size > 20 { | ||
return Response::error("Bad Request", 400); | ||
} | ||
|
||
match place_words(&options.words, options.size) { | ||
Err(unplaced_words) => Ok(Response::from_json(&json!({ | ||
"error": "Unable to place all words", | ||
"unplaced_words": unplaced_words | ||
})) | ||
.unwrap() | ||
.with_status(500)), | ||
Ok(grid) => Response::from_json(&json!(grid | ||
.iter() | ||
.map(|row| row.iter().collect::<String>()) | ||
.collect::<Vec<String>>())), | ||
} | ||
} | ||
} | ||
}) | ||
.post_async("/generate", generate_puzzle) | ||
.run(req, env) | ||
.await | ||
} | ||
|
||
async fn generate_puzzle(mut req: Request, _ctx: RouteContext<()>) -> Result<Response> { | ||
match req.json().await { | ||
Err(_) => Response::error("Bad Request", 400), | ||
Ok(options) => { | ||
let options: GeneratorOptions = options; | ||
if options.size > 20 { | ||
return Response::error("Bad Request", 400); | ||
} | ||
|
||
let puzzle = place_words(options); | ||
Response::from_json(&puzzle) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,42 +1,40 @@ | ||
use anyhow::Result; | ||
use routefinder::Captures; | ||
use spin_sdk::{ | ||
http::{Request, Response, Router}, | ||
http_component, | ||
}; | ||
use serde::Deserialize; | ||
use word_puzzle_generator::place_words; | ||
use word_puzzle_generator::{place_words, GeneratorOptions}; | ||
|
||
#[derive(Deserialize)] | ||
struct GeneratorOptions { | ||
size: usize, | ||
words: Vec<String>, | ||
} | ||
|
||
/// A simple Spin HTTP component. | ||
#[http_component] | ||
fn handle_word_puzzle_spin(req: Request) -> Result<Response> { | ||
let mut router = Router::new(); | ||
let mut router = Router::new(); | ||
|
||
router.get("/", |_req, _params| { | ||
Ok(http::Response::builder() | ||
.status(http::StatusCode::OK) | ||
.body(Some("Hello from spin!".into()))?) | ||
}); | ||
|
||
router.get("/", |_req, _params| Ok(http::Response::builder() | ||
.status(http::StatusCode::OK) | ||
.body(Some("Hello from spin!".into()))?)); | ||
router.post("/generate", |req, _params| { | ||
let body = req.body().as_ref().unwrap(); | ||
let options: GeneratorOptions = serde_json::from_str(std::str::from_utf8(body.as_ref()).unwrap()).unwrap(); | ||
router.post("/generate", generate_puzzle); | ||
|
||
router.handle(req) | ||
} | ||
|
||
if options.size > 20 { | ||
return Ok(http::Response::builder().status(http::StatusCode::BAD_REQUEST).body(None)?); | ||
} | ||
fn generate_puzzle(req: Request, _params: Captures) -> Result<Response> { | ||
let body = req.body().as_ref().unwrap(); | ||
let options: GeneratorOptions = serde_json::from_str(std::str::from_utf8(body.as_ref())?)?; | ||
|
||
let grid = place_words(&options.words, options.size).unwrap(); | ||
let response = serde_json::to_string_pretty(&grid | ||
.iter() | ||
.map(|row| row.iter().collect::<String>()) | ||
.collect::<Vec<String>>())?.as_bytes().to_vec(); | ||
return Ok(http::Response::builder() | ||
.status(http::StatusCode::OK) | ||
.body(Some(response.into()))?) | ||
}); | ||
if options.size > 20 { | ||
return Ok(http::Response::builder() | ||
.status(http::StatusCode::BAD_REQUEST) | ||
.body(None)?); | ||
} | ||
|
||
router.handle(req) | ||
let puzzle = place_words(options); | ||
let response = serde_json::to_string_pretty(&puzzle)?.as_bytes().to_vec(); | ||
Ok(http::Response::builder() | ||
.status(http::StatusCode::OK) | ||
.header("Content-Type", "application/json") | ||
.body(Some(response.into()))?) | ||
} |