Compare commits

..

4 Commits

Author SHA1 Message Date
Aravinth Manivannan 76692109bc
feat: add Settings.pages.base_path to store website content
DIRECTORY STRUCTURE:
    Settings.pages.base_path > page.hostname
2022-11-09 14:20:56 +05:30
Aravinth Manivannan 71533b9860
feat: db: add, rm, list and get site 2022-11-09 14:07:45 +05:30
Aravinth Manivannan c055cb30fd
feat: link to project homepage in footer 2022-11-09 13:32:21 +05:30
Aravinth Manivannan 1b9e1215c9
feat: bootstrap dashboard homepage 2022-11-09 13:32:06 +05:30
10 changed files with 574 additions and 17 deletions

View File

@ -26,6 +26,10 @@ domain = "demo.librepages.org"
cookie_secret = "94b2b2732626fdb7736229a7c777cb451e6304c147c4549f30"
[page]
base_path = "/tmp/librepages-defualt-config/"
[database]
# This section deals with the database location and how to access it

View File

@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS librepages_sites (
site_secret VARCHAR(32) NOT NULL UNIQUE,
repo_url VARCHAR(3000) NOT NULL UNIQUE,
branch TEXT NOT NULL,
hostname VARCHAR(3000) NOT NULL UNIQUE,
ID SERIAL PRIMARY KEY NOT NULL,
owned_by INTEGER NOT NULL references librepages_users(ID) ON DELETE CASCADE
);
CREATE UNIQUE INDEX librepages_sites_site_secret ON librepages_sites(site_secret);

View File

@ -39,6 +39,19 @@
},
"query": "UPDATE librepages_users set name = $1\n WHERE name = $2"
},
"416b9f0412f0d7ee05d4a350839c5a6d1e06c1d7f8942744f6d806ddc47084c2": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Text",
"Text"
]
}
},
"query": "DELETE FROM librepages_sites\n WHERE hostname = ($1)\n AND owned_by = ( SELECT ID FROM librepages_users WHERE name = $2);\n "
},
"5c5d774bde06c0ab83c3616a56a28f12dfd9c546cbaac9f246d3b350c587823e": {
"describe": {
"columns": [],
@ -51,6 +64,22 @@
},
"query": "DELETE FROM librepages_users WHERE name = ($1)"
},
"67311c6196639edd153b7b7dd56a37703b67abe750b88f5afdcf0d3d779432e7": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Varchar",
"Varchar",
"Text",
"Varchar",
"Text"
]
}
},
"query": "\n INSERT INTO librepages_sites\n (site_secret, repo_url, branch, hostname, owned_by)\n VALUES ($1, $2, $3, $4, ( SELECT ID FROM librepages_users WHERE name = $5 ));\n "
},
"6a557f851d4f47383b864085093beb0954e79779f21b655978f07e285281e0ac": {
"describe": {
"columns": [],
@ -97,19 +126,82 @@
},
"query": "UPDATE librepages_users set password = $1\n WHERE name = $2"
},
"aa0e7d72a4542f28db816a8cc20e3f7b778e90e3cbe982a5a24af0b682adaf7d": {
"9fd163d10860ad4519f9398582aaa0615d6d7b784e844ee71038f77dcd069eed": {
"describe": {
"columns": [],
"nullable": [],
"columns": [
{
"name": "site_secret",
"ordinal": 0,
"type_info": "Varchar"
},
{
"name": "repo_url",
"ordinal": 1,
"type_info": "Varchar"
},
{
"name": "branch",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "hostname",
"ordinal": 3,
"type_info": "Varchar"
}
],
"nullable": [
false,
false,
false,
false
],
"parameters": {
"Left": [
"Varchar",
"Text",
"Varchar"
"Text"
]
}
},
"query": "insert into librepages_users\n (name , password, email) values ($1, $2, $3)"
"query": "SELECT site_secret, repo_url, branch, hostname\n FROM librepages_sites\n WHERE owned_by = (SELECT ID FROM librepages_users WHERE name = $1 );\n "
},
"aad26d1f932001cbe49b147348aa528eca5101ec6ef83cb034e1ccd0dbd17878": {
"describe": {
"columns": [
{
"name": "site_secret",
"ordinal": 0,
"type_info": "Varchar"
},
{
"name": "repo_url",
"ordinal": 1,
"type_info": "Varchar"
},
{
"name": "branch",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "hostname",
"ordinal": 3,
"type_info": "Varchar"
}
],
"nullable": [
false,
false,
false,
false
],
"parameters": {
"Left": [
"Text",
"Text"
]
}
},
"query": "SELECT site_secret, repo_url, branch, hostname\n FROM librepages_sites\n WHERE owned_by = (SELECT ID FROM librepages_users WHERE name = $1 )\n AND hostname = $2;\n "
},
"b48c77db6e663d97df44bf9ec2ee92fd3e02f2dcbcdbd1d491e09fab2da68494": {
"describe": {
@ -176,5 +268,19 @@
}
},
"query": "SELECT email FROM librepages_users WHERE name = $1"
},
"faa4170a309f19a4abf1ca3f8dd3c0526945aa00f028ebf8bd7063825d448f5b": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Varchar",
"Text",
"Varchar"
]
}
},
"query": "INSERT INTO librepages_users\n (name , password, email) VALUES ($1, $2, $3)"
}
}

165
src/db.rs
View File

@ -22,6 +22,7 @@ use sqlx::types::time::OffsetDateTime;
//use sqlx::types::Json;
use sqlx::ConnectOptions;
use sqlx::PgPool;
use url::quirks::hostname;
use crate::errors::*;
@ -96,8 +97,8 @@ impl Database {
/// register a new user
pub async fn register(&self, p: &Register<'_>) -> ServiceResult<()> {
sqlx::query!(
"insert into librepages_users
(name , password, email) values ($1, $2, $3)",
"INSERT INTO librepages_users
(name , password, email) VALUES ($1, $2, $3)",
&p.username,
&p.hash,
&p.email,
@ -248,6 +249,107 @@ impl Database {
Ok(())
}
pub async fn add_site(&self, msg: &Site) -> ServiceResult<()> {
sqlx::query!(
"
INSERT INTO librepages_sites
(site_secret, repo_url, branch, hostname, owned_by)
VALUES ($1, $2, $3, $4, ( SELECT ID FROM librepages_users WHERE name = $5 ));
",
msg.site_secret,
msg.repo_url,
msg.branch,
msg.hostname,
msg.owner,
)
.execute(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
Ok(())
}
pub async fn get_site(&self, owner: &str, hostname: &str) -> ServiceResult<Site> {
let site = sqlx::query_as!(
InnerSite,
"SELECT site_secret, repo_url, branch, hostname
FROM librepages_sites
WHERE owned_by = (SELECT ID FROM librepages_users WHERE name = $1 )
AND hostname = $2;
",
owner,
hostname
)
.fetch_one(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
let res = site.to_site(owner.into());
Ok(res)
}
pub async fn list_all_sites(&self, owner: &str) -> ServiceResult<Vec<Site>> {
let mut sites = sqlx::query_as!(
InnerSite,
"SELECT site_secret, repo_url, branch, hostname
FROM librepages_sites
WHERE owned_by = (SELECT ID FROM librepages_users WHERE name = $1 );
",
owner,
)
.fetch_all(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
let res = sites.drain(0..).map(|s| s.to_site(owner.into())).collect();
Ok(res)
}
pub async fn delete_site(&self, owner: &str, hostname: &str) -> ServiceResult<()> {
sqlx::query!(
"DELETE FROM librepages_sites
WHERE hostname = ($1)
AND owned_by = ( SELECT ID FROM librepages_users WHERE name = $2);
",
hostname,
owner
)
.execute(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
Ok(())
}
}
struct InnerSite {
site_secret: String,
repo_url: String,
branch: String,
hostname: String,
}
impl InnerSite {
fn to_site(self, owner: String) -> Site {
Site {
site_secret: self.site_secret,
repo_url: self.repo_url,
branch: self.branch,
hostname: self.hostname,
owner,
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
/// Data required to add a new site
pub struct Site {
pub site_secret: String,
pub repo_url: String,
pub branch: String,
pub hostname: String,
pub owner: String,
}
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
@ -454,4 +556,63 @@ mod tests {
"user is deleted so username shouldn't exist"
);
}
#[actix_rt::test]
pub async fn test_db_sites() {
let settings = Settings::new().unwrap();
let pool_options = PgPoolOptions::new().max_connections(1);
let db = ConnectionOptions::Fresh(Fresh {
pool_options,
url: settings.database.url.clone(),
disable_logging: !settings.debug,
})
.connect()
.await
.unwrap();
assert!(db.ping().await);
const EMAIL: &str = "postgresdbsiteuser@foo.com";
const NAME: &str = "postgresdbsiteuser";
const PASSWORD: &str = "pasdfasdfasdfadf";
db.migrate().await.unwrap();
let p = super::Register {
username: NAME,
email: EMAIL,
hash: PASSWORD,
};
if db.username_exists(p.username).await.unwrap() {
db.delete_user(p.username).await.unwrap();
assert!(
!db.username_exists(p.username).await.unwrap(),
"user is deleted so username shouldn't exist"
);
}
db.register(&p).await.unwrap();
// testing adding site
let site = Site {
site_secret: "foobar".into(),
repo_url: "https://git.batsense.net/LibrePages/librepages.git".into(),
branch: "librepages".into(),
hostname: "db_works.tests.librepages.librepages.org".into(),
owner: p.username.into(),
};
db.add_site(&site).await.unwrap();
// get site
let db_site = db.get_site(p.username, &site.hostname).await.unwrap();
assert_eq!(db_site, site);
// list all sites owned by user
let db_sites = db.list_all_sites(p.username).await.unwrap();
assert_eq!(db_sites.len(), 1);
assert_eq!(db_sites, vec![site.clone()]);
// delete site
db.delete_site(p.username, &site.hostname).await.unwrap();
assert!(db.list_all_sites(p.username).await.unwrap().is_empty());
}
}

74
src/pages/dash/home.rs Normal file
View File

@ -0,0 +1,74 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::cell::RefCell;
use actix_identity::Identity;
use actix_web::http::header::ContentType;
use tera::Context;
use crate::api::v1::RedirectQuery;
use crate::ctx::api::v1::auth::Login as LoginPayload;
use crate::pages::errors::*;
use crate::settings::Settings;
use crate::AppCtx;
pub use super::*;
pub const DASH_HOME: TemplateFile = TemplateFile::new("dash_home", "pages/dash/index.html");
pub struct Home {
ctx: RefCell<Context>,
}
impl CtxError for Home {
fn with_error(&self, e: &ReadableError) -> String {
self.ctx.borrow_mut().insert(ERROR_KEY, e);
self.render()
}
}
impl Home {
pub fn new(settings: &Settings, payload: Option<&LoginPayload>) -> Self {
let ctx = RefCell::new(context(settings));
if let Some(payload) = payload {
ctx.borrow_mut().insert(PAYLOAD_KEY, payload);
}
Self { ctx }
}
pub fn render(&self) -> String {
TEMPLATES
.render(DASH_HOME.name, &self.ctx.borrow())
.unwrap()
}
pub fn page(s: &Settings) -> String {
let p = Self::new(s, None);
p.render()
}
}
#[actix_web_codegen_const_routes::get(path = "PAGES.dash.home")]
pub async fn get_home(ctx: AppCtx) -> impl Responder {
let home = Home::page(&ctx.settings);
let html = ContentType::html();
HttpResponse::Ok().content_type(html).body(home)
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(get_home);
}

29
src/pages/dash/mod.rs Normal file
View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use actix_web::*;
pub use super::{context, Footer, TemplateFile, PAGES, PAYLOAD_KEY, TEMPLATES};
mod home;
pub fn register_templates(t: &mut tera::Tera) {
home::DASH_HOME.register(t).expect(home::DASH_HOME.name);
}
pub fn services(cfg: &mut web::ServiceConfig) {
home::services(cfg);
}

View File

@ -84,6 +84,12 @@ pub struct Settings {
pub source_code: String,
pub pages: Vec<Arc<Page>>,
pub database: Database,
pub page: PageConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PageConfig {
pub base_path: String,
}
#[cfg(not(tarpaulin_include))]
@ -174,14 +180,20 @@ impl Settings {
pub fn init(&self) {
for (index, page) in self.pages.iter().enumerate() {
Url::parse(&page.repo).unwrap();
let path = Path::new(&page.path);
if path.exists() && path.is_file() {
panic!("Path is a file, should be a directory: {:?}", page);
fn create_dir_util(path: &Path) {
if path.exists() && path.is_file() {
panic!("Path is a file, should be a directory: {:?}", path);
}
if !path.exists() {
std::fs::create_dir_all(&path).unwrap();
}
}
if !path.exists() {
std::fs::create_dir_all(&path).unwrap();
}
create_dir_util(Path::new(&page.path));
create_dir_util(Path::new(&self.page.base_path));
for (index2, page2) in self.pages.iter().enumerate() {
if index2 == index {
continue;

View File

@ -41,6 +41,7 @@ pub async fn get_ctx() -> (Temp, Arc<Ctx>) {
let tmp_dir = Temp::new_dir().unwrap();
println!("[log] Test temp directory: {}", tmp_dir.to_str().unwrap());
let mut pages = Vec::with_capacity(settings.pages.len());
let page_base_path = tmp_dir.as_path().join("base_path");
for page in settings.pages.iter() {
let name = Path::new(&page.path).file_name().unwrap().to_str().unwrap();
let path = tmp_dir.as_path().join(name);

View File

@ -8,11 +8,11 @@
</div>
<div class="footer__column">
<a
href="/"
href="https://librepages.org"
class="footer__link"
target="_blank"
rel="noopener"
title="RSS"
title="Project Homepage"
>
Home
</a>

View File

@ -0,0 +1,160 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="{{ assets.css }}" />
<title>LibrePages</title>
</head>
<body class="auth__body">
<header>
<nav>
<p>LibrePages</p>
<span class="nav__spacer"></span>
<ul class="nav__links">
<li class="nav__item">Help</li>
<li class="nav__item">Settings</li>
<li class="nav__item">Logout</li>
</ul>
</nav>
</header>
<main>
<div class="sites__collection">
<div class="sites__actions">
<a class="sites__actions__new-site" href="/add/new/site">
<button>Add new site</button>
</a>
</div>
<a href="/sites/mcaptcha.org" class="site__container">
<div class="site__info--head">
<img
class="site__container--preview"
src="https://mcaptcha.org/favicon.ico"
/>
<div class="site__info--column">
<p href="https://mcaptcha.org"><b>mcaptcha.org</b></p>
<p>Deploys from {{ source_url }}</p>
</div>
</div>
<div class="site__info--tail">
<p>Last update {{ last_update }}</p>
</div></a
>
</div>
</main>
{% include "footer" %}
</body>
<style>
header {
width: 100%;
}
nav {
width: 100%;
margin: auto;
display: flex;
}
.nav__spacer {
flex: 4;
}
.nav__links {
display: flex;
list-style: none;
}
.nav__item {
margin: 0 20px;
}
body {
display: flex;
flex-direction: column;
align-items: center;
}
main {
width: 100%;
margin: 20px;
}
.sites__collection {
margin: auto;
width: 70%;
border: 1px solid #e8ebed;
border-radius: 8px;
}
.sites__actions {
width: 100%;
height: 50px;
display: flex;
flex-direction: row-reverse;
align-items: center;
box-sizing: border-box;
padding: 0px 20px;
margin: 10px 0;
}
.sites__actions__new-site {
min-height: 36px;
background: green;
display: flex;
align-items: center;
padding: 0px 8px;
}
.sites__actions__new-site > button {
margin: 0;
padding: 0;
height: 100%;
border: none;
width: 100%;
color: white;
background: none;
}
.site__container {
box-sizing: border-box;
margin: 10px 0;
width: 100%;
display: flex;
justify-content: space-between;
padding: 10px 20px;
align-items: center;
}
.site__container:hover {
background: #f7f8f8;
}
.site__info--head {
display: flex;
align-items: center;
}
.site__info--column {
margin-left: 20px;
}
.site__info--column > p,
.site__info--column > a {
margin: 0;
padding: 0;
}
.site__container:visited,
.site__container {
color: black;
text-decoration: none;
}
.site__container--preview {
width: 50px;
height: 50px;
border-radius: 50%;
}
</style>
</html>