Merge pull request #12 from smolgrrr/rust-server

basic server
This commit is contained in:
smolgrrr 2023-11-18 15:46:27 +11:00 committed by GitHub
commit 89ac48e37c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1925 additions and 0 deletions

2
pow_server/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# Ignore all build artifacts
target/

1863
pow_server/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
pow_server/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "pow_server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4.0"
actix-rt = "2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
nostr = "0.25"

47
pow_server/src/main.rs Normal file
View File

@ -0,0 +1,47 @@
use actix_web::{web, App, HttpResponse, HttpServer, Responder, post};
use serde::Deserialize;
use std::str::FromStr;
use nostr::prelude::*;
#[derive(Deserialize)]
struct EventContent {
// tags: Vec<Vec<String>>,
content: String,
pubkey: String,
}
#[derive(Deserialize)]
struct PowRequest {
req_event: EventContent,
difficulty: String,
}
#[post("/powgen")]
async fn pow_handler(pow_request: web::Json<PowRequest>) -> impl Responder {
let pubkey = match XOnlyPublicKey::from_str(&pow_request.req_event.pubkey) {
Ok(pubkey) => pubkey,
Err(_) => return HttpResponse::BadRequest().finish(),
};
let difficulty = match u8::from_str(&pow_request.difficulty) {
Ok(difficulty) => difficulty,
Err(_) => return HttpResponse::BadRequest().finish(),
};
let builder = EventBuilder::new_text_note(&pow_request.req_event.content, &[]);
let event = builder.to_unsigned_pow_event(pubkey, difficulty);
HttpResponse::Ok().json(event)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(pow_handler)
})
.bind("127.0.0.1:8080")?
.run()
.await
}