use actix_web::{get, web, App, HttpResponse, HttpServer, ResponseError};
use thiserror::Error;
use askama::Template;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite::params;
struct TodoEntry {
id: u32,
text: String,
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
entries: Vec<TodoEntry>,
}
#[derive(Error, Debug)]
enum MyError {
#[error("Failed to render HTML")]
AskamaError(#[from] askama::Error),
#[error("Failed to get connection")]
ConnectionPoolError(#[from] r2d2::Error),
#[error("Failed SQL execution")]
SqliteError(#[from] rusqlite::Error),
}
impl ResponseError for MyError {}
#[get("/")]
async fn index(db: web::Data<Pool<SqliteConnectionManager>>) -> Result<HttpResponse, MyError> {
let conn = db.get()?;
let mut statement = conn.prepare("SELECT id, text FROM todo")?;
let rows = statement.query_map(params![], |row| {
let id = row.get(0)?;
let text = row.get(1)?;
Ok(TodoEntry { id, text })
})?;
let mut entries = Vec::new();
for row in rows {
entries.push(row?);
}
let html = IndexTemplate { entries };
let response_body = html.render()?;
Ok(HttpResponse::Ok()
.content_type("text/html")
.body(response_body))
}
#[actix_rt::main]
async fn main() -> Result<(), actix_web::Error> {
let manager = SqliteConnectionManager::file("todo.db");
let pool = Pool::new(manager).expect("Failed to inithialize the connection pool.");
let conn = pool.get().expect("Failed to get the connection from the pool.");
conn.execute(
"CREATE TABLE IF NOT EXISTS todo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL
)",
params![],
)
.expect("Failed to create a table 'todo'.");
HttpServer::new(move || App::new().service(index).app_data(pool.clone()))
.bind("0.0.0.0:8080")?
.run()
.await?;
Ok(())
}
import matplotlib.pyplot as plt
def func1(x):
return 3 * x - 24
x = list(range(1,11))
y = []
for i in range(10):
y.append(func1(x[i]))
plt.plot(x, y)
plt.grid(color='0.8')
plt.show()
[package]
name = "todo"
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 = { version = "4.1" }
actix-rt = { version = "2.7" }
thiserror = { version = "1.0" }
askama = { version = "0.11" }
import sys
import re
args = sys.argv
def aby2dec(target, m):
n = len(target) - 1
sum = 0
for i in range(len(target)):
alpha_search_result = re.search('[A-F]', target[i])
if alpha_search_result:
num = ord(alpha_search_result.group()) - 55
else:
num = int(target[i])
sum += (m ** n) * num
n -= 1
return sum
print(aby2dec(args[1], int(args[2])))
[package]
name = "todo"
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 = { version = "4.1" }
actix-rt = { version = "2.7" }
thiserror = { version = "1.0" }