Skip to content

Repository files navigation

CIcrates.iodocs.rs

A simple http server library.

Future

This crate will be deprecated and you should migrate to axum. There is a guide in migrate-to-axum.md. It is written by AI based on an active project migration. Doing a first migration draft with AI should be possible.

Axum has almost all features and more, supports tower Service (albeit at first maybe a bit more complicated) but makes reuability in the ecosystem way easier.

Basic get request

use chuchi::{get,Res};structGlobalName(String);// handle a simple get request#[get("/")]fnroot(global_name:Res<GlobalName>) -> String{format!("Hi, this is {}", global_name.0)}#[tokio::main]asyncfnmain(){letmut server = chuchi::build("0.0.0.0:3000").await.expect("Failed to parse address");
server.add_resource(GlobalName("chuchi".into()));
server.add_route(root);
server.run().await.unwrap();}

For more examples look in the examples directory and the test directory.

Features

  • json
  • fs
  • http2 (enables http 2 support)
  • ws (adds websocket support)
  • trace

Api Example

use std::fmt;use std::sync::{Arc,Mutex};use chuchi::api::{Request,Method};use chuchi::api::error::{self,ErrorasApiError,StatusCode};use chuchi::{api, impl_res_extractor,Resource};use serde::{Serialize,Deserialize};// -- Type definitions#[derive(Debug,Clone,Serialize)]pubenumError{Internal(String),Request(String)}impl error::ApiErrorforError{fnfrom_error(e:ApiError) -> Self{match e {ApiError::HeadersMissing(_) |
ApiError::Deserialize(_) => {Self::Request(e.to_string())}
e => Self::Internal(e.to_string()),}}fnstatus_code(&self) -> StatusCode{matchself{Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,Self::Request(_) => StatusCode::BAD_REQUEST}}}impl fmt::DisplayforError{fnfmt(&self,f:&mut fmt::Formatter) -> fmt::Result{
fmt::Debug::fmt(self, f)}}#[derive(Debug,Clone,Serialize,Deserialize)]structNameReq;#[derive(Debug,Clone,Serialize,Deserialize)]structName{firstname:String,lastname:String}implRequestforNameReq{typeResponse = Name;typeError = Error;constPATH:&'staticstr = "/name";constMETHOD:Method = Method::GET;}#[derive(Debug,Clone,Serialize,Deserialize)]structSetNameReq{name:Name}implRequestforSetNameReq{typeResponse = ();typeError = Error;constPATH:&'staticstr = "/name";constMETHOD:Method = Method::PUT;}// -- implementations#[derive(Resource)]structSharedName(Mutex<Name>);#[api(NameReq)]fnget_name(req:NameReq,name:&SharedName) -> Result<Name,Error>{let lock = name.0.lock().unwrap();Ok(lock.clone())}#[api(SetNameReq)]fnset_name(req:SetNameReq,name:&SharedName) -> Result<(),Error>{letmut lock = name.0.lock().unwrap();*lock = req.name;Ok(())}#[tokio::main]asyncfnmain(){let name = SharedName(Mutex::new(Name{firstname:"Albert".into(),lastname:"Einstein".into()}));letmut server = chuchi::build("0.0.0.0:3000").await.expect("Failed to parse address");
server.add_resource(name);
server.add_route(get_name);
server.add_route(set_name);
server.run().await.unwrap();}}

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages