extern crate tela;
use serde::{Deserialize, Serialize};
use tela::{
prelude::*,
response::{File, HTML, JSON},
Server,
};
#[get("/html-file")]
pub fn html_file() -> File<&'static str> {
File("examples/assets/index.html")
}
#[get("/text-file")]
pub fn text_file() -> File<&'static str> {
File("examples/assets/index.txt")
}
#[get("/text-to-html-file")]
pub fn text_to_html_file() -> HTML<File<&'static str>> {
HTML(File("examples/assets/index.txt"))
}
#[get("/json-file")]
pub fn json_file() -> File<&'static str> {
File("examples/assets/sample.json")
}
#[derive(Deserialize, Serialize)]
pub struct User {
name: String,
age: u16,
description: String,
}
#[get("/text-to-json-file")]
pub fn text_to_json_file() -> Result<JSON<User>> {
JSON::from_file(File("examples/assets/sample.txt"))
}
#[tela::main]
async fn main() {
Server::new()
.routes(group![
html_file,
text_file,
text_to_html_file,
json_file,
text_to_json_file
])
.serve(3000)
.await
}