Compare commits
22 Commits
48d59b6f90
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 3ac59f65f2 | |||
| 4a25c377c4 | |||
| 8c64c5dbe3 | |||
| 88abbe8c14 | |||
| c5b85bc359 | |||
| 8a27a74e9f | |||
| 425bac6776 | |||
| 5c456fd5cd | |||
| 08b5932cf8 | |||
| c7377e06e3 | |||
| 4e53f9ed1f | |||
| d692bf1cdd | |||
| ec126bbdc4 | |||
| 0fe13b6ce7 | |||
| 9c79881c4b | |||
| f6b6b0a80b | |||
| 146509efdf | |||
| a908bf0e8a | |||
| aeff2b1759 | |||
| b1cb7dff27 | |||
| d50a6a3d76 | |||
| f9726f7b35 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
|||||||
.idea
|
.idea
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
.env
|
.env
|
||||||
|
config.toml
|
||||||
|
|||||||
@@ -8,5 +8,6 @@ actix-web = {version = "4.11.0"}
|
|||||||
serde = "1.0.228"
|
serde = "1.0.228"
|
||||||
serde_json = "1.0.145"
|
serde_json = "1.0.145"
|
||||||
toml = "0.9.7"
|
toml = "0.9.7"
|
||||||
sqlx = {version = "0.8.6", features = ["postgres"]}
|
sqlx = {version = "0.8.6", features = ["postgres", "runtime-tokio"]}
|
||||||
futures-util = "0.3.31"
|
futures-util = "0.3.31"
|
||||||
|
clap = { version = "4.5.50", features = ["derive"] }
|
||||||
@@ -9,6 +9,8 @@ RestartSec=1
|
|||||||
# Change with proper user to run the service
|
# Change with proper user to run the service
|
||||||
User=web
|
User=web
|
||||||
# Change with the installation location of the executable
|
# Change with the installation location of the executable
|
||||||
|
# It is possible to use a custom config file location
|
||||||
|
# by adding '--config /path/to/config.toml' to the command bellow
|
||||||
ExecStart=/usr/share/bin/aindustries-be
|
ExecStart=/usr/share/bin/aindustries-be
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
|
|||||||
13
assets/css/ip.css
Normal file
13
assets/css/ip.css
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
.ip {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ip h1 {
|
||||||
|
font-family: 'Indie Flower', cursive;
|
||||||
|
font-size: 60px;
|
||||||
|
text-align: center;
|
||||||
|
color: var(--clr-primary-a60);
|
||||||
|
}
|
||||||
58
assets/css/music.css
Normal file
58
assets/css/music.css
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
.featured {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.featured h1 {
|
||||||
|
font-size: 48px;
|
||||||
|
text-align: center;
|
||||||
|
font-family: 'Indie Flower', cursive;
|
||||||
|
color: var(--clr-primary-a60);
|
||||||
|
max-width: 75vw;
|
||||||
|
}
|
||||||
|
|
||||||
|
.featured iframe {
|
||||||
|
width: 55vw;
|
||||||
|
height: 100%;
|
||||||
|
aspect-ratio: 16 / 9;
|
||||||
|
border: none;
|
||||||
|
filter: drop-shadow(0px 0px 12px rgba(0, 0, 0, 0.75));
|
||||||
|
}
|
||||||
|
|
||||||
|
.playlist {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.playlist h1 {
|
||||||
|
font-size: 40px;
|
||||||
|
text-align: center;
|
||||||
|
font-family: 'Indie Flower', cursive;
|
||||||
|
color: var(--clr-primary-a60);
|
||||||
|
max-width: 75vw;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
.playlist iframe {
|
||||||
|
width: 55vw;
|
||||||
|
height: 100%;
|
||||||
|
aspect-ratio: 16 / 9;
|
||||||
|
border: none;
|
||||||
|
filter: drop-shadow(0px 0px 12px rgba(0, 0, 0, 0.75));
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (width <= 1250px) {
|
||||||
|
.featured iframe {
|
||||||
|
width: 90vw;
|
||||||
|
}
|
||||||
|
|
||||||
|
.playlist iframe {
|
||||||
|
width: 90vw;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
[application]
|
|
||||||
# Change with actual location of assets
|
|
||||||
assets=""
|
|
||||||
# Set bind address
|
|
||||||
bind="127.0.0.1:8080"
|
|
||||||
11
config.toml.example
Normal file
11
config.toml.example
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[server]
|
||||||
|
# Change with actual location of assets
|
||||||
|
assets="./assets"
|
||||||
|
# Set bind address
|
||||||
|
bind="127.0.0.1:8080"
|
||||||
|
|
||||||
|
[database]
|
||||||
|
address="127.0.0.1:5432"
|
||||||
|
user="web"
|
||||||
|
password="apassword"
|
||||||
|
database_name="website"
|
||||||
13
migrations/20251027081447_projects.sql
Normal file
13
migrations/20251027081447_projects.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS Projects (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"title" TEXT NOT NULL,
|
||||||
|
"description" TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS Project_Detail (
|
||||||
|
"id" TEXT PRIMARY KEY NOT NULL,
|
||||||
|
"title" TEXT NOT NULL,
|
||||||
|
"git_url" TEXT NOT NULL,
|
||||||
|
"git_title" TEXT NOT NULL,
|
||||||
|
"description" TEXT NOT NULL
|
||||||
|
)
|
||||||
13
migrations/20251027102341_website_project.sql
Normal file
13
migrations/20251027102341_website_project.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
INSERT INTO Projects ("id", "title", "description") VALUES (
|
||||||
|
'website',
|
||||||
|
'website',
|
||||||
|
'This project is the website you currently are on.'
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO Project_Detail ("id", "title", "git_url", "git_title", "description") VALUES (
|
||||||
|
'website',
|
||||||
|
'website',
|
||||||
|
'https://git.aindustries.be/AINDUSTRIES/aindustries.be',
|
||||||
|
'aindustries-be',
|
||||||
|
'This project, the website you are on, is made in Rust such that all the pages are generated by code. <br> That is that each html element is represented by a struct which implements the Render trait (as in render the element to html). <br> As it is right now the system is not that impressive but I believe it can do amazing things in the future.'
|
||||||
|
)
|
||||||
@@ -1,115 +1,160 @@
|
|||||||
use crate::html::Render;
|
use crate::html::Render;
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
pub(crate) struct Heading {
|
||||||
pub(crate) struct h1 {
|
level: u8,
|
||||||
|
id: String,
|
||||||
text: String,
|
text: String,
|
||||||
|
classes: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for h1 {
|
impl Render for Heading {
|
||||||
fn render(&self) -> String {
|
fn render(&self) -> String {
|
||||||
format!("<h1>{}</h1>", self.text)
|
let classes = self.classes.join(" ");
|
||||||
|
format!(
|
||||||
|
"<h{} id=\"{}\" class=\"{}\">{}</h{}>",
|
||||||
|
self.level, self.id, classes, self.text, self.level
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl h1 {
|
impl Heading {
|
||||||
pub(crate) fn new<T>(text: T) -> Self
|
pub(crate) fn builder() -> HeadingBuilder {
|
||||||
|
HeadingBuilder::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct HeadingBuilder {
|
||||||
|
level: Option<u8>,
|
||||||
|
id: Option<String>,
|
||||||
|
text: Option<String>,
|
||||||
|
classes: Option<Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HeadingBuilder {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
level: None,
|
||||||
|
id: None,
|
||||||
|
text: None,
|
||||||
|
classes: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn level(self, level: u8) -> Self {
|
||||||
|
Self {
|
||||||
|
level: Some(level),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn id<T>(self, id: T) -> Self
|
||||||
where
|
where
|
||||||
T: Into<String>,
|
T: Into<String>,
|
||||||
{
|
{
|
||||||
Self { text: text.into() }
|
Self {
|
||||||
|
id: Some(id.into()),
|
||||||
|
..self
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub(crate) struct h2 {
|
|
||||||
text: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for h2 {
|
|
||||||
fn render(&self) -> String {
|
|
||||||
format!("<h2>{}</h2>", self.text)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl h2 {
|
pub(crate) fn text<T>(self, text: T) -> Self
|
||||||
pub(crate) fn new<T>(text: T) -> Self
|
|
||||||
where
|
where
|
||||||
T: Into<String>,
|
T: Into<String>,
|
||||||
{
|
{
|
||||||
Self { text: text.into() }
|
Self {
|
||||||
|
text: Some(text.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build(self) -> Heading {
|
||||||
|
let level = self.level.unwrap_or(1);
|
||||||
|
let id = self.id.unwrap_or(String::new());
|
||||||
|
let text = self.text.unwrap_or(String::new());
|
||||||
|
let classes = self.classes.unwrap_or(Vec::new());
|
||||||
|
Heading {
|
||||||
|
level,
|
||||||
|
id,
|
||||||
|
text,
|
||||||
|
classes,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
pub(crate) struct Link {
|
||||||
pub(crate) struct link {
|
|
||||||
rel: String,
|
rel: String,
|
||||||
href: String,
|
href: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for link {
|
impl Render for Link {
|
||||||
fn render(&self) -> String {
|
fn render(&self) -> String {
|
||||||
format!("<link rel=\"{}\" href=\"{}\">", self.rel, self.href)
|
format!("<link rel=\"{}\" href=\"{}\">", self.rel, self.href)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl link {
|
impl Link {
|
||||||
pub(crate) fn new<T, U>(rel: T, href: U) -> Self
|
pub(crate) fn builder() -> LinkBuilder {
|
||||||
|
LinkBuilder::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct LinkBuilder {
|
||||||
|
rel: Option<String>,
|
||||||
|
href: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LinkBuilder {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
rel: None,
|
||||||
|
href: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn rel<T>(self, rel: T) -> Self
|
||||||
where
|
where
|
||||||
T: Into<String>,
|
T: Into<String>,
|
||||||
U: Into<String>,
|
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
rel: rel.into(),
|
rel: Some(rel.into()),
|
||||||
href: href.into(),
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
pub(crate) fn href<T>(self, href: T) -> Self
|
||||||
pub(crate) struct div {
|
|
||||||
id: String,
|
|
||||||
classes: Vec<String>,
|
|
||||||
content: Vec<Box<dyn Render>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for div {
|
|
||||||
fn render(&self) -> String {
|
|
||||||
let classes = self.classes.join(" ");
|
|
||||||
format!(
|
|
||||||
"<div id=\"{}\" class=\"{}\">{}</div>",
|
|
||||||
self.id,
|
|
||||||
classes,
|
|
||||||
self.content.render()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl div {
|
|
||||||
pub(crate) fn new<T, U>(id: T, classes: Vec<U>) -> Self
|
|
||||||
where
|
where
|
||||||
T: Into<String>,
|
T: Into<String>,
|
||||||
U: Into<String> + Clone,
|
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
id: id.into(),
|
href: Some(href.into()),
|
||||||
classes: classes.iter().map(|x| x.clone().into()).collect(),
|
..self
|
||||||
content: vec![],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn append_element(&mut self, element: impl Render + 'static) {
|
pub(crate) fn build(self) -> Link {
|
||||||
self.content.push(Box::new(element));
|
let rel = self.rel.unwrap_or(String::new());
|
||||||
|
let href = self.href.unwrap_or(String::new());
|
||||||
|
Link { rel, href }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
pub(crate) struct Paragraph {
|
||||||
pub(crate) struct p {
|
|
||||||
id: String,
|
id: String,
|
||||||
classes: Vec<String>,
|
classes: Vec<String>,
|
||||||
text: String,
|
text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for p {
|
impl Render for Paragraph {
|
||||||
fn render(&self) -> String {
|
fn render(&self) -> String {
|
||||||
let classes = self.classes.join(" ");
|
let classes = self.classes.join(" ");
|
||||||
format!(
|
format!(
|
||||||
@@ -119,29 +164,72 @@ impl Render for p {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl p {
|
impl Paragraph {
|
||||||
pub(crate) fn new<T, U, V>(id: T, classes: Vec<U>, text: V) -> Self
|
pub(crate) fn builder() -> ParagraphBuilder {
|
||||||
where
|
ParagraphBuilder::new()
|
||||||
T: Into<String>,
|
|
||||||
U: Into<String> + Clone,
|
|
||||||
V: Into<String>,
|
|
||||||
{
|
|
||||||
Self {
|
|
||||||
id: id.into(),
|
|
||||||
classes: classes.iter().map(|x| x.clone().into()).collect(),
|
|
||||||
text: text.into(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
pub(crate) struct ParagraphBuilder {
|
||||||
pub(crate) struct img {
|
id: Option<String>,
|
||||||
|
classes: Option<Vec<String>>,
|
||||||
|
text: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ParagraphBuilder {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
id: None,
|
||||||
|
classes: None,
|
||||||
|
text: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn id<T>(self, id: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
id: Some(id.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn text<T>(self, text: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
text: Some(text.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build(self) -> Paragraph {
|
||||||
|
let id = self.id.unwrap_or(String::new());
|
||||||
|
let classes = self.classes.unwrap_or(Vec::new());
|
||||||
|
let text = self.text.unwrap_or(String::new());
|
||||||
|
Paragraph { id, classes, text }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct Image {
|
||||||
id: String,
|
id: String,
|
||||||
classes: Vec<String>,
|
classes: Vec<String>,
|
||||||
src: String,
|
src: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for img {
|
impl Render for Image {
|
||||||
fn render(&self) -> String {
|
fn render(&self) -> String {
|
||||||
let classes = self.classes.join(" ");
|
let classes = self.classes.join(" ");
|
||||||
format!(
|
format!(
|
||||||
@@ -151,30 +239,73 @@ impl Render for img {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl img {
|
impl Image {
|
||||||
pub(crate) fn new<T, U, V>(id: T, classes: Vec<U>, src: V) -> Self
|
pub(crate) fn builder() -> ImageBuilder {
|
||||||
where
|
ImageBuilder::new()
|
||||||
T: Into<String>,
|
|
||||||
U: Into<String> + Clone,
|
|
||||||
V: Into<String>,
|
|
||||||
{
|
|
||||||
Self {
|
|
||||||
id: id.into(),
|
|
||||||
classes: classes.iter().map(|x| x.clone().into()).collect(),
|
|
||||||
src: src.into(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
pub(crate) struct ImageBuilder {
|
||||||
pub(crate) struct a {
|
id: Option<String>,
|
||||||
|
classes: Option<Vec<String>>,
|
||||||
|
src: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImageBuilder {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
id: None,
|
||||||
|
classes: None,
|
||||||
|
src: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn id<T>(self, id: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
id: Some(id.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn src<T>(self, src: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
src: Some(src.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build(self) -> Image {
|
||||||
|
let id = self.id.unwrap_or(String::new());
|
||||||
|
let classes = self.classes.unwrap_or(Vec::new());
|
||||||
|
let src = self.src.unwrap_or(String::new());
|
||||||
|
Image { id, classes, src }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct Anchor {
|
||||||
id: String,
|
id: String,
|
||||||
classes: Vec<String>,
|
classes: Vec<String>,
|
||||||
href: String,
|
href: String,
|
||||||
text: String,
|
text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for a {
|
impl Render for Anchor {
|
||||||
fn render(&self) -> String {
|
fn render(&self) -> String {
|
||||||
let classes = self.classes.join(" ");
|
let classes = self.classes.join(" ");
|
||||||
format!(
|
format!(
|
||||||
@@ -184,19 +315,190 @@ impl Render for a {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl a {
|
impl Anchor {
|
||||||
pub(crate) fn new<T, U, V, W>(id: T, classes: Vec<U>, href: V, text: W) -> Self
|
pub(crate) fn builder() -> AnchorBuilder {
|
||||||
|
AnchorBuilder::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct AnchorBuilder {
|
||||||
|
id: Option<String>,
|
||||||
|
classes: Option<Vec<String>>,
|
||||||
|
href: Option<String>,
|
||||||
|
text: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AnchorBuilder {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
id: None,
|
||||||
|
classes: None,
|
||||||
|
href: None,
|
||||||
|
text: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn id<T>(self, id: T) -> Self
|
||||||
where
|
where
|
||||||
T: Into<String>,
|
T: Into<String>,
|
||||||
U: Into<String> + Clone,
|
|
||||||
V: Into<String>,
|
|
||||||
W: Into<String>,
|
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
id: id.into(),
|
id: Some(id.into()),
|
||||||
classes: classes.iter().map(|x| x.clone().into()).collect(),
|
..self
|
||||||
href: href.into(),
|
}
|
||||||
text: text.into(),
|
}
|
||||||
|
|
||||||
|
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn href<T>(self, href: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
href: Some(href.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(crate) fn text<T>(self, text: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
text: Some(text.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build(self) -> Anchor {
|
||||||
|
let id = self.id.unwrap_or(String::new());
|
||||||
|
let classes = self.classes.unwrap_or(Vec::new());
|
||||||
|
let href = self.href.unwrap_or(String::new());
|
||||||
|
let text = self.text.unwrap_or(String::new());
|
||||||
|
Anchor {
|
||||||
|
id,
|
||||||
|
classes,
|
||||||
|
href,
|
||||||
|
text,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct Iframe {
|
||||||
|
id: String,
|
||||||
|
classes: Vec<String>,
|
||||||
|
src: String,
|
||||||
|
title: String,
|
||||||
|
width: usize,
|
||||||
|
height: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for Iframe {
|
||||||
|
fn render(&self) -> String {
|
||||||
|
let classes = self.classes.join(" ");
|
||||||
|
format!(
|
||||||
|
"<iframe id=\"{}\" classes=\"{}\" title=\"{}\" width=\"{}\" height=\"{}\" src=\"{}\"></iframe>",
|
||||||
|
self.id, classes, self.title, self.width, self.height, self.src
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct IframeBuilder {
|
||||||
|
id: Option<String>,
|
||||||
|
classes: Option<Vec<String>>,
|
||||||
|
src: Option<String>,
|
||||||
|
title: Option<String>,
|
||||||
|
width: Option<usize>,
|
||||||
|
height: Option<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IframeBuilder {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
id: None,
|
||||||
|
classes: None,
|
||||||
|
src: None,
|
||||||
|
title: None,
|
||||||
|
width: None,
|
||||||
|
height: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn id<T>(self, id: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
id: Some(id.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn src<T>(self, src: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
src: Some(src.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn title<T>(self, title: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
title: Some(title.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn width(self, width: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
width: Some(width),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn height(self, height: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
height: Some(height),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build(self) -> Iframe {
|
||||||
|
let id = self.id.unwrap_or(String::new());
|
||||||
|
let classes = self.classes.unwrap_or(Vec::new());
|
||||||
|
let title = self.title.unwrap_or(String::new());
|
||||||
|
let width = self.width.unwrap_or(0);
|
||||||
|
let height = self.height.unwrap_or(0);
|
||||||
|
let src = self.src.unwrap_or(String::new());
|
||||||
|
Iframe {
|
||||||
|
id,
|
||||||
|
title,
|
||||||
|
classes,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
src,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
83
src/html/layouts.rs
Normal file
83
src/html/layouts.rs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
use crate::html::Render;
|
||||||
|
|
||||||
|
pub(crate) struct Division {
|
||||||
|
id: String,
|
||||||
|
classes: Vec<String>,
|
||||||
|
elements: Vec<Box<dyn Render>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for Division {
|
||||||
|
fn render(&self) -> String {
|
||||||
|
let classes = self.classes.join(" ");
|
||||||
|
format!(
|
||||||
|
"<div id=\"{}\" class=\"{}\">{}</div>",
|
||||||
|
self.id,
|
||||||
|
classes,
|
||||||
|
self.elements.render()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Division {
|
||||||
|
pub(crate) fn builder() -> DivisionBuilder {
|
||||||
|
DivisionBuilder::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn append_element(&mut self, element: impl Render + 'static) {
|
||||||
|
self.elements.push(Box::new(element));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct DivisionBuilder {
|
||||||
|
id: Option<String>,
|
||||||
|
classes: Option<Vec<String>>,
|
||||||
|
elements: Option<Vec<Box<dyn Render>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DivisionBuilder {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
id: None,
|
||||||
|
classes: None,
|
||||||
|
elements: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn id<T>(self, id: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
id: Some(id.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn elements(self, elements: Vec<Box<dyn Render>>) -> Self {
|
||||||
|
Self {
|
||||||
|
elements: Some(elements),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build(self) -> Division {
|
||||||
|
let id = self.id.unwrap_or(String::new());
|
||||||
|
let classes = self.classes.unwrap_or(Vec::new());
|
||||||
|
let elements = self.elements.unwrap_or(Vec::new());
|
||||||
|
Division {
|
||||||
|
id,
|
||||||
|
classes,
|
||||||
|
elements,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,66 +1,31 @@
|
|||||||
pub(crate) mod elements;
|
pub(crate) mod elements;
|
||||||
|
pub(crate) mod layouts;
|
||||||
|
pub(crate) mod pages;
|
||||||
|
|
||||||
|
macro_rules! boxed_vec {
|
||||||
|
($($element:ident),+) => {
|
||||||
|
vec![
|
||||||
|
$(
|
||||||
|
Box::new($element) as Box<dyn Render>,
|
||||||
|
)*
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) use boxed_vec;
|
||||||
|
|
||||||
pub(crate) trait Render {
|
pub(crate) trait Render {
|
||||||
fn render(&self) -> String;
|
fn render(&self) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Page {
|
|
||||||
title: String,
|
|
||||||
head: Vec<Box<dyn Render>>,
|
|
||||||
body: Vec<Box<dyn Render>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Vec<Box<dyn Render>> {
|
impl Render for Vec<Box<dyn Render>> {
|
||||||
fn render(&self) -> String {
|
fn render(&self) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
for element in self {
|
for element in self {
|
||||||
let render = element.render();
|
let render = element.render();
|
||||||
result.push_str(&render);
|
result.push_str(&render);
|
||||||
|
result.push('\n');
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for Page {
|
|
||||||
fn render(&self) -> String {
|
|
||||||
format!(
|
|
||||||
"\
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang=\"en\">
|
|
||||||
<head>
|
|
||||||
<meta charset=\"UTF-8\">
|
|
||||||
<title>{}</title>
|
|
||||||
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
|
|
||||||
{}
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
{}
|
|
||||||
</body>
|
|
||||||
</html>",
|
|
||||||
self.title,
|
|
||||||
self.head.render(),
|
|
||||||
self.body.render()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Page {
|
|
||||||
pub(crate) fn new<T>(title: T) -> Self
|
|
||||||
where
|
|
||||||
T: Into<String>,
|
|
||||||
{
|
|
||||||
Page {
|
|
||||||
title: title.into(),
|
|
||||||
head: vec![],
|
|
||||||
body: vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn append_element_to_body(&mut self, element: impl Render + 'static) {
|
|
||||||
self.body.push(Box::new(element));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn append_element_to_head(&mut self, element: impl Render + 'static) {
|
|
||||||
self.head.push(Box::new(element));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
215
src/html/pages.rs
Normal file
215
src/html/pages.rs
Normal file
@@ -0,0 +1,215 @@
|
|||||||
|
use crate::html::Render;
|
||||||
|
use crate::html::boxed_vec;
|
||||||
|
use crate::html::elements::{Anchor, Image, Link, Paragraph};
|
||||||
|
use crate::html::layouts::Division;
|
||||||
|
|
||||||
|
pub(crate) struct Page {
|
||||||
|
title: String,
|
||||||
|
head: Vec<Box<dyn Render>>,
|
||||||
|
body: Vec<Box<dyn Render>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for Page {
|
||||||
|
fn render(&self) -> String {
|
||||||
|
format!(
|
||||||
|
"\
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang=\"en\">
|
||||||
|
<head>
|
||||||
|
<meta charset=\"UTF-8\">
|
||||||
|
<title>{}</title>
|
||||||
|
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
|
||||||
|
{}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{}
|
||||||
|
</body>
|
||||||
|
</html>",
|
||||||
|
self.title,
|
||||||
|
self.head.render(),
|
||||||
|
self.body.render()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Page {
|
||||||
|
pub(crate) fn builder() -> PageBuilder {
|
||||||
|
PageBuilder::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn append_element_to_body(&mut self, element: impl Render + 'static) {
|
||||||
|
self.body.push(Box::new(element));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn append_element_to_head(&mut self, element: impl Render + 'static) {
|
||||||
|
self.head.push(Box::new(element));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct PageBuilder {
|
||||||
|
title: Option<String>,
|
||||||
|
head: Option<Vec<Box<dyn Render>>>,
|
||||||
|
body: Option<Vec<Box<dyn Render>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PageBuilder {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
title: None,
|
||||||
|
head: None,
|
||||||
|
body: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn title<T>(self, title: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
title: Some(title.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn head(self, head: Vec<Box<dyn Render>>) -> Self {
|
||||||
|
Self {
|
||||||
|
head: Some(head),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn body(self, body: Vec<Box<dyn Render>>) -> Self {
|
||||||
|
Self {
|
||||||
|
body: Some(body),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build(self) -> Page {
|
||||||
|
let title = self.title.unwrap_or(String::new());
|
||||||
|
let head = self.head.unwrap_or(Vec::new());
|
||||||
|
let body = self.body.unwrap_or(Vec::new());
|
||||||
|
Page { title, head, body }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct BasePage {
|
||||||
|
page: Page,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BasePage {
|
||||||
|
pub(crate) fn builder() -> BasePageBuilder {
|
||||||
|
BasePageBuilder::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn append_element_to_head(&mut self, element: impl Render + 'static) {
|
||||||
|
self.page.append_element_to_head(element);
|
||||||
|
}
|
||||||
|
pub(crate) fn append_element_to_body(&mut self, element: impl Render + 'static) {
|
||||||
|
self.page.append_element_to_body(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for BasePage {
|
||||||
|
fn render(&self) -> String {
|
||||||
|
self.page.render()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct BasePageBuilder {
|
||||||
|
title: Option<String>,
|
||||||
|
head: Option<Vec<Box<dyn Render>>>,
|
||||||
|
body: Option<Vec<Box<dyn Render>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BasePageBuilder {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
title: None,
|
||||||
|
head: None,
|
||||||
|
body: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn title<T>(self, title: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
title: Some(title.into()),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn head(self, head: Vec<Box<dyn Render>>) -> Self {
|
||||||
|
Self {
|
||||||
|
head: Some(head),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(crate) fn body(self, body: Vec<Box<dyn Render>>) -> Self {
|
||||||
|
Self {
|
||||||
|
body: Some(body),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build(self) -> BasePage {
|
||||||
|
let title = self.title.unwrap_or(String::new());
|
||||||
|
let head = self.head.unwrap_or(Vec::new());
|
||||||
|
let body = self.body.unwrap_or(Vec::new());
|
||||||
|
let name = Paragraph::builder()
|
||||||
|
.id("name")
|
||||||
|
.classes(vec!["name"])
|
||||||
|
.text("AINDUSTRIES")
|
||||||
|
.build();
|
||||||
|
let home = Anchor::builder()
|
||||||
|
.id("home")
|
||||||
|
.classes(vec!["nav-button"])
|
||||||
|
.href("/")
|
||||||
|
.text("Home")
|
||||||
|
.build();
|
||||||
|
let projects = Anchor::builder()
|
||||||
|
.id("projects")
|
||||||
|
.classes(vec!["nav-button"])
|
||||||
|
.href("/projects")
|
||||||
|
.text("Projects")
|
||||||
|
.build();
|
||||||
|
let music = Anchor::builder()
|
||||||
|
.id("music")
|
||||||
|
.classes(vec!["nav-button"])
|
||||||
|
.href("/music")
|
||||||
|
.text("Music")
|
||||||
|
.build();
|
||||||
|
let buttons = Division::builder()
|
||||||
|
.classes(vec!["nav-buttons"])
|
||||||
|
.elements(boxed_vec![home, projects, music])
|
||||||
|
.build();
|
||||||
|
let header = Division::builder()
|
||||||
|
.id("header")
|
||||||
|
.classes(vec!["header"])
|
||||||
|
.elements(boxed_vec![name, buttons])
|
||||||
|
.build();
|
||||||
|
// background
|
||||||
|
let image = Image::builder()
|
||||||
|
.id("background")
|
||||||
|
.classes(vec!["background"])
|
||||||
|
.src("/static/img/background.png")
|
||||||
|
.build();
|
||||||
|
// css
|
||||||
|
let base = Link::builder()
|
||||||
|
.rel("stylesheet")
|
||||||
|
.href("/static/css/base.css")
|
||||||
|
.build();
|
||||||
|
let mut final_head = boxed_vec![base];
|
||||||
|
final_head.extend(head);
|
||||||
|
let mut final_body = boxed_vec![image, header];
|
||||||
|
final_body.extend(body);
|
||||||
|
let page = Page::builder()
|
||||||
|
.title(title)
|
||||||
|
.head(final_head)
|
||||||
|
.body(final_body)
|
||||||
|
.build();
|
||||||
|
BasePage { page }
|
||||||
|
}
|
||||||
|
}
|
||||||
85
src/main.rs
85
src/main.rs
@@ -3,21 +3,40 @@ mod middleware;
|
|||||||
mod pages;
|
mod pages;
|
||||||
|
|
||||||
use actix_web::{App, HttpServer, web};
|
use actix_web::{App, HttpServer, web};
|
||||||
|
use clap::Parser;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use sqlx::PgPool;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use std::process::exit;
|
||||||
use toml::from_slice;
|
use toml::from_slice;
|
||||||
|
|
||||||
|
use crate::pages::files::robots;
|
||||||
use middleware::MimeType;
|
use middleware::MimeType;
|
||||||
use pages::{
|
use pages::{
|
||||||
files::file,
|
files::file,
|
||||||
index,
|
index,
|
||||||
|
ip::ip,
|
||||||
|
music::music,
|
||||||
projects::{project, projects},
|
projects::{project, projects},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(name = "aindustries-be", version, about = "This is the amazing webserver for aindustries.be!", long_about = None)]
|
||||||
|
struct Cli {
|
||||||
|
#[arg(
|
||||||
|
short,
|
||||||
|
long,
|
||||||
|
value_name = "CONFIG",
|
||||||
|
help = "Path to config file, defaults to /etc/aindustries-be/config.toml"
|
||||||
|
)]
|
||||||
|
config: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
server: ServerConfig,
|
server: ServerConfig,
|
||||||
|
database: DatabaseConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@@ -26,29 +45,75 @@ struct ServerConfig {
|
|||||||
bind: String,
|
bind: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct DatabaseConfig {
|
||||||
|
address: String,
|
||||||
|
user: String,
|
||||||
|
password: String,
|
||||||
|
database_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
struct AppState {
|
struct AppState {
|
||||||
assets: String,
|
assets: String,
|
||||||
|
pool: PgPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut config_file =
|
let cli = Cli::parse();
|
||||||
File::open("/etc/aindustries-be/config.toml").expect("Could not open config file");
|
let config_file_path = cli
|
||||||
|
.config
|
||||||
|
.unwrap_or("/etc/aindustries-be/config.toml".to_string());
|
||||||
|
let mut config_file = match File::open(&config_file_path) {
|
||||||
|
Ok(config_file) => config_file,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Could not open config file ({config_file_path}): {e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
let mut buf = vec![];
|
let mut buf = vec![];
|
||||||
config_file
|
if let Err(e) = config_file.read_to_end(&mut buf) {
|
||||||
.read_to_end(&mut buf)
|
eprintln!("Could not read config file: {e}");
|
||||||
.expect("Could not read config file");
|
exit(1);
|
||||||
let config: Config = from_slice(&buf).expect("Could not parse config");
|
}
|
||||||
|
let config: Config = match from_slice(&buf) {
|
||||||
|
Ok(config) => config,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Could not parse config file {e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
let bind_address = config.server.bind;
|
let bind_address = config.server.bind;
|
||||||
let assets = config.server.assets;
|
let assets = config.server.assets;
|
||||||
let data = web::Data::new(AppState { assets });
|
let database_address = format!(
|
||||||
|
"postgres://{}:{}@{}/{}",
|
||||||
|
config.database.user,
|
||||||
|
config.database.password,
|
||||||
|
config.database.address,
|
||||||
|
config.database.database_name
|
||||||
|
);
|
||||||
|
let pool = match PgPool::connect(&database_address).await {
|
||||||
|
Ok(pool) => pool,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Could not connect to database: {e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let assets = assets.replace("~", "/home/conta/Code/aindustries.be/assets");
|
||||||
|
let data = web::Data::new(AppState { assets, pool });
|
||||||
if let Ok(server) = HttpServer::new(move || {
|
if let Ok(server) = HttpServer::new(move || {
|
||||||
App::new().app_data(data.clone()).service(file).service(
|
App::new()
|
||||||
|
.app_data(data.clone())
|
||||||
|
.service(file)
|
||||||
|
.service(robots)
|
||||||
|
.service(
|
||||||
web::scope("")
|
web::scope("")
|
||||||
.wrap(MimeType::new("text/html"))
|
.wrap(MimeType::new("text/html"))
|
||||||
.service(index)
|
.service(index)
|
||||||
.service(projects)
|
.service(projects)
|
||||||
.service(project),
|
.service(project)
|
||||||
|
.service(music)
|
||||||
|
.service(ip),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.bind(bind_address)
|
.bind(bind_address)
|
||||||
@@ -56,7 +121,7 @@ async fn main() {
|
|||||||
match server.run().await {
|
match server.run().await {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("An error occurred: {}", e);
|
println!("An error occurred: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,14 @@ where
|
|||||||
let val = HeaderValue::from_str(self.mime_type.as_str()).expect("Invalid MimeType");
|
let val = HeaderValue::from_str(self.mime_type.as_str()).expect("Invalid MimeType");
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let mut res = fut.await?;
|
let mut res = fut.await?;
|
||||||
|
match res.headers_mut().get_mut(CONTENT_TYPE) {
|
||||||
|
Some(content_type) => {
|
||||||
|
*content_type = val;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
res.headers_mut().append(CONTENT_TYPE, val);
|
res.headers_mut().append(CONTENT_TYPE, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(res)
|
Ok(res)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use std::path::PathBuf;
|
|||||||
#[get("/static/{file_type}/{file_name}")]
|
#[get("/static/{file_type}/{file_name}")]
|
||||||
async fn file(file: Path<(String, String)>, data: web::Data<AppState>) -> impl Responder {
|
async fn file(file: Path<(String, String)>, data: web::Data<AppState>) -> impl Responder {
|
||||||
let mut path = PathBuf::from(&data.assets);
|
let mut path = PathBuf::from(&data.assets);
|
||||||
path.push(&file.0.replace("/", ""));
|
path.push(file.0.replace("/", ""));
|
||||||
path.push(&file.1.replace("/", ""));
|
path.push(file.1.replace("/", ""));
|
||||||
let mut file = File::open(path).unwrap();
|
let mut file = File::open(path).unwrap();
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
if file.read_to_end(&mut bytes).is_err() {
|
if file.read_to_end(&mut bytes).is_err() {
|
||||||
@@ -17,3 +17,10 @@ async fn file(file: Path<(String, String)>, data: web::Data<AppState>) -> impl R
|
|||||||
};
|
};
|
||||||
HttpResponse::Ok().body(bytes)
|
HttpResponse::Ok().body(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/robots.txt")]
|
||||||
|
async fn robots() -> impl Responder {
|
||||||
|
"User-agent: *\n\
|
||||||
|
Disallow: /\n\
|
||||||
|
Allow: /$"
|
||||||
|
}
|
||||||
|
|||||||
34
src/pages/ip.rs
Normal file
34
src/pages/ip.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
use crate::html::elements::{Heading, Link};
|
||||||
|
use crate::html::layouts::Division;
|
||||||
|
use crate::html::pages::BasePage;
|
||||||
|
use crate::html::{Render, boxed_vec};
|
||||||
|
use actix_web::HttpRequest;
|
||||||
|
use actix_web::{Responder, get};
|
||||||
|
|
||||||
|
#[get("/ip")]
|
||||||
|
async fn ip(request: HttpRequest) -> impl Responder {
|
||||||
|
let info = request.connection_info();
|
||||||
|
let ip = info.realip_remote_addr();
|
||||||
|
|
||||||
|
let text = Heading::builder()
|
||||||
|
.text(format!(
|
||||||
|
"Your IP address is {}<br>Don't ask how I know.",
|
||||||
|
ip.unwrap_or("?")
|
||||||
|
))
|
||||||
|
.build();
|
||||||
|
let div = Division::builder()
|
||||||
|
.classes(vec!["ip"])
|
||||||
|
.elements(boxed_vec![text])
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let css = Link::builder()
|
||||||
|
.rel("stylesheet")
|
||||||
|
.href("/static/css/ip.css")
|
||||||
|
.build();
|
||||||
|
BasePage::builder()
|
||||||
|
.title("Your IP address")
|
||||||
|
.head(boxed_vec![css])
|
||||||
|
.body(boxed_vec![div])
|
||||||
|
.build()
|
||||||
|
.render()
|
||||||
|
}
|
||||||
@@ -1,76 +1,41 @@
|
|||||||
pub(crate) mod files;
|
pub(crate) mod files;
|
||||||
|
pub(crate) mod ip;
|
||||||
|
pub(crate) mod music;
|
||||||
pub(crate) mod projects;
|
pub(crate) mod projects;
|
||||||
|
|
||||||
use crate::html::elements::{a, div, h1, h2, img, link, p};
|
use crate::html::elements::{Heading, Link};
|
||||||
use crate::html::{Page, Render};
|
use crate::html::layouts::Division;
|
||||||
|
use crate::html::pages::BasePage;
|
||||||
|
use crate::html::{Render, boxed_vec};
|
||||||
use actix_web::{Responder, get};
|
use actix_web::{Responder, get};
|
||||||
|
|
||||||
pub(crate) struct BasePage {
|
|
||||||
page: Page,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BasePage {
|
|
||||||
pub(crate) fn new<T>(title: T) -> Self
|
|
||||||
where
|
|
||||||
T: Into<String>,
|
|
||||||
{
|
|
||||||
let mut page = Page::new(title);
|
|
||||||
// header
|
|
||||||
let mut header = div::new("header", vec!["header"]);
|
|
||||||
let name = p::new("name", vec!["name"], "AINDUSTRIES");
|
|
||||||
let mut buttons = div::new("buttons", vec!["nav-buttons"]);
|
|
||||||
let home = a::new("home", vec!["nav-button"], "/", "Home");
|
|
||||||
let projects = a::new("projects", vec!["nav-button"], "/projects", "Projects");
|
|
||||||
buttons.append_element(home);
|
|
||||||
buttons.append_element(projects);
|
|
||||||
header.append_element(name);
|
|
||||||
header.append_element(buttons);
|
|
||||||
// background
|
|
||||||
let image = img::new(
|
|
||||||
"background",
|
|
||||||
vec!["background"],
|
|
||||||
"/static/img/background.png",
|
|
||||||
);
|
|
||||||
// css
|
|
||||||
let base = link::new("stylesheet", "/static/css/base.css");
|
|
||||||
// add elements to the page in order
|
|
||||||
page.append_element_to_head(base);
|
|
||||||
page.append_element_to_body(image);
|
|
||||||
page.append_element_to_body(header);
|
|
||||||
Self { page }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn append_element_to_head(&mut self, element: impl Render + 'static) {
|
|
||||||
self.page.append_element_to_head(element);
|
|
||||||
}
|
|
||||||
pub(crate) fn append_element_to_body(&mut self, element: impl Render + 'static) {
|
|
||||||
self.page.append_element_to_body(element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for BasePage {
|
|
||||||
fn render(&self) -> String {
|
|
||||||
self.page.render()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
async fn index() -> impl Responder {
|
async fn index() -> impl Responder {
|
||||||
let mut page = BasePage::new("AINDUSTRIES");
|
|
||||||
// introduction
|
// introduction
|
||||||
let mut intro = div::new("intro", vec!["intro"]);
|
let hi = Heading::builder().text("Hello and welcome!").build();
|
||||||
let hi = h1::new("Hello and welcome!");
|
let detail = Heading::builder()
|
||||||
let detail = h2::new(
|
.level(2)
|
||||||
|
.text(
|
||||||
"This here is a small website to show the passion I have for computers.<br>\
|
"This here is a small website to show the passion I have for computers.<br>\
|
||||||
I have always had a good time creating and discovering new things.<br>\
|
I have always had a good time creating and discovering new things.<br>\
|
||||||
Your may discover some of my projects here on this showcase.",
|
Your may discover some of my projects here on this little showcase.",
|
||||||
);
|
)
|
||||||
intro.append_element(hi);
|
.build();
|
||||||
intro.append_element(detail);
|
let intro = Division::builder()
|
||||||
|
.id("intro")
|
||||||
|
.classes(vec!["intro"])
|
||||||
|
.elements(boxed_vec![hi, detail])
|
||||||
|
.build();
|
||||||
// css
|
// css
|
||||||
let index = link::new("stylesheet", "static/css/index.css");
|
let index = Link::builder()
|
||||||
|
.rel("stylesheet")
|
||||||
|
.href("static/css/index.css")
|
||||||
|
.build();
|
||||||
// add elements to the page in order
|
// add elements to the page in order
|
||||||
page.append_element_to_head(index);
|
let page = BasePage::builder()
|
||||||
page.append_element_to_body(intro);
|
.title("AINDUSTRIES")
|
||||||
|
.head(boxed_vec![index])
|
||||||
|
.body(boxed_vec![intro])
|
||||||
|
.build();
|
||||||
page.render()
|
page.render()
|
||||||
}
|
}
|
||||||
|
|||||||
54
src/pages/music.rs
Normal file
54
src/pages/music.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use crate::html::layouts::DivisionBuilder;
|
||||||
|
use crate::html::pages::BasePageBuilder;
|
||||||
|
use crate::html::{Render, boxed_vec};
|
||||||
|
|
||||||
|
use crate::AppState;
|
||||||
|
use crate::html::elements::{HeadingBuilder, IframeBuilder, Link};
|
||||||
|
use actix_web::web::Data;
|
||||||
|
use actix_web::{Responder, get, web};
|
||||||
|
use sqlx::query_as;
|
||||||
|
|
||||||
|
#[get("/music")]
|
||||||
|
async fn music(state: Data<AppState>) -> impl Responder {
|
||||||
|
let featured_title = HeadingBuilder::new()
|
||||||
|
.level(1)
|
||||||
|
.text("The song I've been listening to the most recently is \"Shiawase - VIP\" by Dion Timmer.")
|
||||||
|
.build();
|
||||||
|
let featured_iframe = IframeBuilder::new()
|
||||||
|
.src("https://www.youtube.com/embed/suisIF4hwyw")
|
||||||
|
.build();
|
||||||
|
let featured = DivisionBuilder::new()
|
||||||
|
.classes(vec!["featured"])
|
||||||
|
.id("featured")
|
||||||
|
.elements(boxed_vec![featured_title, featured_iframe])
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let playlist_title = HeadingBuilder::new()
|
||||||
|
.text(
|
||||||
|
"You can find below my primary playlist, \
|
||||||
|
the one containing almost all the songs I have been listening to throughout the years.",
|
||||||
|
)
|
||||||
|
.build();
|
||||||
|
let playlist_iframe = IframeBuilder::new()
|
||||||
|
.src(
|
||||||
|
"https://open.spotify.com/embed/playlist/\
|
||||||
|
3owjyc1RILDGkgCmdCal39?utm_source=generator",
|
||||||
|
)
|
||||||
|
.build();
|
||||||
|
let playlist = DivisionBuilder::new()
|
||||||
|
.id("playlist")
|
||||||
|
.classes(vec!["playlist"])
|
||||||
|
.elements(boxed_vec![playlist_title, playlist_iframe])
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let css = Link::builder()
|
||||||
|
.rel("stylesheet")
|
||||||
|
.href("/static/css/music.css")
|
||||||
|
.build();
|
||||||
|
let page = BasePageBuilder::new()
|
||||||
|
.title("Music")
|
||||||
|
.head(boxed_vec![css])
|
||||||
|
.body(boxed_vec![featured, playlist])
|
||||||
|
.build();
|
||||||
|
page.render()
|
||||||
|
}
|
||||||
@@ -1,64 +1,126 @@
|
|||||||
use super::BasePage;
|
use crate::AppState;
|
||||||
use crate::html::Render;
|
use crate::html::elements::{Anchor, Heading, Link, Paragraph};
|
||||||
use crate::html::elements::{a, div, h1, h2, link, p};
|
use crate::html::layouts::Division;
|
||||||
|
use crate::html::pages::BasePage;
|
||||||
|
use crate::html::{Render, boxed_vec};
|
||||||
|
use actix_web::web::Data;
|
||||||
use actix_web::{Responder, get, web};
|
use actix_web::{Responder, get, web};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use sqlx::query_as;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Project {
|
||||||
|
id: String,
|
||||||
|
title: String,
|
||||||
|
description: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/projects")]
|
#[get("/projects")]
|
||||||
async fn projects() -> impl Responder {
|
async fn projects(app_state: Data<AppState>) -> impl Responder {
|
||||||
let mut page = BasePage::new("Projects");
|
let title = Heading::builder().text("My projects").build();
|
||||||
let title = h1::new("My projects");
|
let desc = Heading::builder()
|
||||||
let desc = h2::new(
|
.level(2)
|
||||||
|
.text(
|
||||||
"Here you will find all my projects which deserve to be shown<br>\
|
"Here you will find all my projects which deserve to be shown<br>\
|
||||||
(I've done a lot of small projects but they are not worth it.)",
|
(I've done a lot of small projects but they are not worth it.)",
|
||||||
);
|
)
|
||||||
let mut website = div::new("project-website", vec!["project"]);
|
.build();
|
||||||
let website_title = h1::new("Website");
|
let projects = match query_as!(Project, "SELECT id, title, description FROM Projects")
|
||||||
let mut info = div::new("project-website-info", vec!["project-info"]);
|
.fetch_all(&app_state.pool)
|
||||||
let website_desc = p::new(
|
.await
|
||||||
"website-desc",
|
{
|
||||||
vec!["project-desc"],
|
Ok(projects) => projects,
|
||||||
"This project is the website you currently are on.",
|
Err(_) => {
|
||||||
);
|
vec![]
|
||||||
let view = a::new(
|
}
|
||||||
"website-view",
|
};
|
||||||
vec!["project-view"],
|
let items: Vec<Box<dyn Render>> = projects
|
||||||
"/projects/website",
|
.into_iter()
|
||||||
"Learn More",
|
.map(|p| {
|
||||||
);
|
let title = Heading::builder().text(p.title).build();
|
||||||
info.append_element(website_desc);
|
let description = Paragraph::builder()
|
||||||
info.append_element(view);
|
.classes(vec!["project-desc"])
|
||||||
website.append_element(website_title);
|
.text(p.description)
|
||||||
website.append_element(info);
|
.build();
|
||||||
let css = link::new("stylesheet", "/static/css/projects.css");
|
let view = Anchor::builder()
|
||||||
page.append_element_to_head(css);
|
.classes(vec!["project-view"])
|
||||||
page.append_element_to_body(title);
|
.href(format!("/projects/{}", p.id))
|
||||||
page.append_element_to_body(desc);
|
.text("Learn More")
|
||||||
page.append_element_to_body(website);
|
.build();
|
||||||
|
let info = Division::builder()
|
||||||
|
.classes(vec!["project-info"])
|
||||||
|
.elements(boxed_vec![description, view])
|
||||||
|
.build();
|
||||||
|
Box::new(
|
||||||
|
Division::builder()
|
||||||
|
.classes(vec!["project"])
|
||||||
|
.elements(boxed_vec![title, info])
|
||||||
|
.build(),
|
||||||
|
) as Box<dyn Render>
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let css = Link::builder()
|
||||||
|
.rel("stylesheet")
|
||||||
|
.href("/static/css/projects.css")
|
||||||
|
.build();
|
||||||
|
let mut body = boxed_vec![title, desc];
|
||||||
|
body.extend(items);
|
||||||
|
let page = BasePage::builder()
|
||||||
|
.title("Projects")
|
||||||
|
.head(boxed_vec![css])
|
||||||
|
.body(body)
|
||||||
|
.build();
|
||||||
page.render()
|
page.render()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ProjectDetail {
|
||||||
|
id: String,
|
||||||
|
title: String,
|
||||||
|
git_url: String,
|
||||||
|
git_title: String,
|
||||||
|
description: String,
|
||||||
|
}
|
||||||
#[get("/projects/{project}")]
|
#[get("/projects/{project}")]
|
||||||
async fn project(project: web::Path<String>) -> impl Responder {
|
async fn project(project: web::Path<String>, app_state: Data<AppState>) -> impl Responder {
|
||||||
let project = project.into_inner();
|
let project = project.into_inner();
|
||||||
let mut page = BasePage::new(format!("Project-{}", project));
|
let css = Link::builder()
|
||||||
match project.as_str() {
|
.rel("stylesheet")
|
||||||
"website" => {
|
.href("/static/css/project.css")
|
||||||
let title = h1::new("Website");
|
.build();
|
||||||
let desc = p::new(
|
let mut page = BasePage::builder()
|
||||||
"description",
|
.title(format!("Project-{}", project))
|
||||||
vec!["description"],
|
.head(boxed_vec![css])
|
||||||
"This project, the website you are on, \
|
.build();
|
||||||
is made in Rust such that all the pages are generated by code.<br>\
|
let project = match query_as!(
|
||||||
That is that each html element is represented by a struct which implements the Render trait (as in render the element to html).<br>\
|
ProjectDetail,
|
||||||
As it is right now the system is not that impressive but I believe it can do amazing things in the futur.<br>\
|
"SELECT * FROM Project_Detail WHERE id = $1",
|
||||||
<br>\
|
project
|
||||||
Wish to see more? Check out the gitea repository: ",
|
)
|
||||||
);
|
.fetch_one(&app_state.pool)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(project) => project,
|
||||||
|
Err(error) => {
|
||||||
|
// return not found page
|
||||||
|
eprintln!("Error fetching project: {:?}", error);
|
||||||
|
return page.render();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let title = Heading::builder().text(project.title).build();
|
||||||
|
let gitea = Anchor::builder()
|
||||||
|
.href(project.git_url)
|
||||||
|
.text(project.git_title)
|
||||||
|
.build();
|
||||||
|
let desc = Paragraph::builder()
|
||||||
|
.classes(vec!["description"])
|
||||||
|
.text(format!(
|
||||||
|
"{}<br><br> Wish to see more? Check out the gitea repository: {}",
|
||||||
|
project.description,
|
||||||
|
gitea.render()
|
||||||
|
))
|
||||||
|
.build();
|
||||||
page.append_element_to_body(title);
|
page.append_element_to_body(title);
|
||||||
page.append_element_to_body(desc);
|
page.append_element_to_body(desc);
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
let css = link::new("stylesheet", "/static/css/project.css");
|
|
||||||
page.append_element_to_head(css);
|
|
||||||
page.render()
|
page.render()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user