Init with Rocket, Diesel and Tera templates
This commit is contained in:
commit
a45120bec0
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
/*.db
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,22 @@
|
||||||
|
[package]
|
||||||
|
name = "services"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
||||||
|
[dependencies.diesel]
|
||||||
|
version = "2.1.4"
|
||||||
|
default-features = false
|
||||||
|
features = ["sqlite"]
|
||||||
|
|
||||||
|
[dependencies.rocket]
|
||||||
|
version = "0.5.0"
|
||||||
|
default-features = false
|
||||||
|
|
||||||
|
[dependencies.rocket_dyn_templates]
|
||||||
|
version = "0.1.0"
|
||||||
|
default-features = false
|
||||||
|
features = ["tera"]
|
|
@ -0,0 +1,2 @@
|
||||||
|
[default]
|
||||||
|
cli_colors = false
|
|
@ -0,0 +1,9 @@
|
||||||
|
# For documentation on how to configure this file,
|
||||||
|
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||||
|
|
||||||
|
[print_schema]
|
||||||
|
file = "src/schema.rs"
|
||||||
|
custom_type_derives = ["diesel::query_builder::QueryId"]
|
||||||
|
|
||||||
|
[migrations_directory]
|
||||||
|
dir = "migrations"
|
|
@ -0,0 +1,2 @@
|
||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
DROP TABLE services
|
|
@ -0,0 +1,6 @@
|
||||||
|
-- Your SQL goes here
|
||||||
|
CREATE TABLE IF NOT EXISTS "services" (
|
||||||
|
"url" TEXT NOT NULL UNIQUE,
|
||||||
|
"software" TEXT NOT NULL,
|
||||||
|
PRIMARY KEY("url")
|
||||||
|
);
|
|
@ -0,0 +1 @@
|
||||||
|
hard_tabs = true
|
|
@ -0,0 +1,11 @@
|
||||||
|
pub mod models;
|
||||||
|
pub mod schema;
|
||||||
|
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use diesel::sqlite::SqliteConnection;
|
||||||
|
|
||||||
|
pub fn establish_connection() -> SqliteConnection {
|
||||||
|
let database_url = "services.db";
|
||||||
|
SqliteConnection::establish(&database_url)
|
||||||
|
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
#[macro_use]
|
||||||
|
extern crate rocket;
|
||||||
|
|
||||||
|
#[launch]
|
||||||
|
fn rocket() -> _ {
|
||||||
|
rocket::build()
|
||||||
|
.attach(Template::fairing())
|
||||||
|
.mount("/", routes![show_services])
|
||||||
|
}
|
||||||
|
|
||||||
|
use ::services::establish_connection;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use services::models::*;
|
||||||
|
|
||||||
|
use rocket_dyn_templates::Template;
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
fn show_services() -> Template /*std::string::String*/ {
|
||||||
|
use ::services::schema::services::dsl::*;
|
||||||
|
|
||||||
|
let connection = &mut establish_connection();
|
||||||
|
let results = services
|
||||||
|
.limit(5)
|
||||||
|
.select(Services::as_select())
|
||||||
|
.load(connection)
|
||||||
|
.expect("Error loading services");
|
||||||
|
|
||||||
|
let mut service_urls = Vec::new();
|
||||||
|
for service in &results {
|
||||||
|
service_urls.push(&service.url);
|
||||||
|
}
|
||||||
|
|
||||||
|
let services_num = &results.len().to_string();
|
||||||
|
|
||||||
|
let mut btreemap = std::collections::BTreeMap::new();
|
||||||
|
btreemap.insert("services", service_urls);
|
||||||
|
btreemap.insert("services_num", vec![services_num]);
|
||||||
|
|
||||||
|
Template::render("hello", &btreemap)
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable)]
|
||||||
|
#[diesel(table_name = crate::schema::services)]
|
||||||
|
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||||
|
pub struct Services {
|
||||||
|
pub url: String,
|
||||||
|
pub software: String,
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
// @generated automatically by Diesel CLI.
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
services (url) {
|
||||||
|
url -> Text,
|
||||||
|
software -> Text,
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
<p>
|
||||||
|
Displaying {{ services_num[0] }} services :
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for service in services %}
|
||||||
|
<li>{{ service }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
Loading…
Reference in New Issue