1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29:02 +02:00

Cargo can't do nested workspaces.

This commit is contained in:
Cameron Dershem
2019-06-13 00:32:48 -04:00
parent e43f408bce
commit 315b131cdd
14 changed files with 17 additions and 21 deletions

View File

@ -0,0 +1,8 @@
[package]
name = "powerful-extractors"
version = "0.1.0"
edition = "2018"
[dependencies]
actix-web = "1.0"
serde = "1.0"

View File

@ -0,0 +1,44 @@
use actix_web::{web, App, HttpResponse, HttpServer};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct Event {
id: Option<i32>,
timestamp: f64,
kind: String,
tags: Vec<String>,
}
fn store_event_in_db(timestamp: f64, kind: String, tags: Vec<String>) -> Event {
// store item in db and get new_event
// use id to lookup item
Event {
id: Some(1),
timestamp: timestamp,
kind: kind,
tags: tags,
}
}
fn capture_event(evt: web::Json<Event>) -> actix_web::Result<HttpResponse> {
let new_event = store_event_in_db(evt.timestamp, evt.kind.clone(), evt.tags.clone());
Ok(HttpResponse::Ok().json(new_event))
}
fn index() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(include_str!("../static/form.html"))
}
fn main() {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/event", web::post().to(capture_event))
})
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
}

View File

@ -0,0 +1,43 @@
<!doctype htmtl>
<html>
<head>
<meta charset=utf-8>
<title>Forms</title>
</head>
<body>
<h3>Submit Json</h3>
<button onclick="submitJson()">Submit</button>
<script>
let payload = {
timestamp: 12345,
kind: "this is a kind",
tags: ['tag1', 'tag2', 'tag3'],
}
function submitJson() {
fetch('http://localhost:8088/event', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
})
.then(function (res) {
return res.json();
})
.then(function (data) {
let expected = {
...payload,
id: 1
};
console.log("expected: ", expected);
console.log("received: ", data);
});
}
</script>
</body>
</html>