🎉 Initial commit

This commit is contained in:
2025-10-05 17:58:56 +02:00
commit 0d801609a3
12 changed files with 2122 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
/target
/.idea
.env

1981
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "setra"
version = "0.1.0"
edition = "2024"
[dependencies]
rocket = { version = "0.5.1", features = ["json"] }
serde = { version = "1.0.228", features = ["derive"] }
dotenv = "0.15.0"
uuid = { version = "1.18.1", features = ["v4", "serde"] }
diesel = { version = "2.3.2", features = ["postgres", "uuid"] }
chrono = "0.4.42"

9
diesel.toml Normal file
View File

@@ -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", "Clone"]
[migrations_directory]
dir = "/mnt/Projects/setra/migrations"

0
migrations/.diesel_lock Normal file
View File

0
migrations/.keep Normal file
View File

View File

@@ -0,0 +1,4 @@
-- This file should undo anything in `up.sql`
DROP TABLE "trackers";
DROP TABLE "hits";

View File

@@ -0,0 +1,16 @@
-- Your SQL goes here
CREATE TABLE "trackers"
(
"id" UUID PRIMARY KEY NOT NULL UNIQUE,
"created_at" TIMESTAMP NOT NULL
);
CREATE TABLE "hits"
(
"id" UUID PRIMARY KEY NOT NULL UNIQUE,
"tracker_id" UUID REFERENCES trackers (id) NOT NULL,
"ip" TEXT NOT NULL,
"agent" TEXT,
"language" TEXT,
"created_at" TIMESTAMP NOT NULL
);

36
src/main.rs Normal file
View File

@@ -0,0 +1,36 @@
mod models;
mod schema;
mod tracker;
use crate::models::Hit;
use crate::schema::hits::dsl::*;
use diesel::{Connection, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper};
use std::env;
#[macro_use]
extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello world!"
}
#[launch]
fn rocket() -> _ {
dotenv::dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let mut db = PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url));
let results = hits
.limit(5)
.select(Hit::as_select())
.load(&mut db)
.expect("Error loading hits");
println!("results: {}", results.len());
rocket::build()
.mount("/", routes![index])
.mount("/tracker", routes![tracker::index])
}

23
src/models.rs Normal file
View File

@@ -0,0 +1,23 @@
use diesel::{Insertable, Queryable, Selectable};
use diesel::data_types::PgTimestamp;
use uuid::Uuid;
#[derive(Queryable, Selectable, Insertable, Clone)]
#[diesel(table_name = crate::schema::trackers)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Tracker {
pub id: Uuid,
pub created_at: PgTimestamp,
}
#[derive(Queryable, Selectable, Insertable, Clone)]
#[diesel(table_name = crate::schema::hits)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Hit {
pub id: Uuid,
pub tracker_id: Uuid,
pub ip: String,
pub agent: Option<String>,
pub language: Option<String>,
pub created_at: PgTimestamp,
}

23
src/schema.rs Normal file
View File

@@ -0,0 +1,23 @@
// @generated automatically by Diesel CLI.
diesel::table! {
hits (id) {
id -> Uuid,
tracker_id -> Uuid,
ip -> Text,
agent -> Nullable<Text>,
language -> Nullable<Text>,
created_at -> Timestamp,
}
}
diesel::table! {
trackers (id) {
id -> Uuid,
created_at -> Timestamp,
}
}
diesel::joinable!(hits -> trackers (tracker_id));
diesel::allow_tables_to_appear_in_same_query!(hits, trackers,);

13
src/tracker.rs Normal file
View File

@@ -0,0 +1,13 @@
use serde::Serialize;
use uuid::Uuid;
#[derive(Serialize)]
pub struct Tracker {
pub id: Uuid,
pub created_at: String,
}
#[get("/")]
pub fn index() -> &'static str {
"Tracker"
}