Compare commits

...

25 commits

Author SHA1 Message Date
2b87ad09a7
feat: handle reqwest errors
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-31 02:23:43 +05:30
86d56734cf
feat: display form data with pagination
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-31 01:36:59 +05:30
c62466515f
feat: view form submissions
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-31 01:27:53 +05:30
a11e7a75ea
feat: list forms associated with a hostname 2022-12-31 01:27:45 +05:30
c2c139575d
feat: link to the forms associated with a deployment 2022-12-31 01:27:03 +05:30
efc8ce5a27
feat: list forms for hostname REST API 2022-12-31 01:26:03 +05:30
0e08a294fc
feat: REST API endpoint to list form submissions 2022-12-30 20:02:50 +05:30
ff9864f5ad
chore: use a different name for development forms container 2022-12-30 19:38:53 +05:30
8a42978379
fix: cp -r
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-12-30 03:46:25 +05:30
2029d3a4e1
feat: publish config file
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-30 03:27:15 +05:30
c44ecdc53f
feat: publish systemd service file
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-30 03:26:17 +05:30
cd185c0d95
chore: stop printing db url 2022-12-30 03:26:06 +05:30
7eb622c29a
feat: load config from /etc/librepages/librepages 2022-12-30 03:25:53 +05:30
a421a6bbb4
fix: bin upload filename
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-12-30 02:40:44 +05:30
7579bbfc86
fix: CI: REMOVE QUOTES AROUND WOODPECKER CONFIGURATION
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
what a waste of time
2022-12-30 01:35:02 +05:30
cafa729234
fix: build badge
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-29 20:13:04 +05:30
a515c9e5b7
fix: conductor: load bearer auth token
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-29 18:17:28 +05:30
998060777f
feat: switch conductor to use bearer auth
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-29 17:41:19 +05:30
ce5b28292e
fix: CI: correct forms-postgres url
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-29 17:11:20 +05:30
764de46015
fix: CI: rewrite librepages/forms address
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-29 17:04:49 +05:30
4272ea103a
feat: REST endpoints to list submissions and delete form submission with auth
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-29 17:00:19 +05:30
2b9fcd2729
feat: read and delete form submissions with authentication
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-12-29 16:40:19 +05:30
e819cf850a
feat: load form api settings 2022-12-29 16:40:06 +05:30
088088c91e
feat: CI: deploy librepages/forms
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-12-29 03:42:50 +05:30
fe76a648a4
feat: deploy forms in dev env script 2022-12-29 03:39:26 +05:30
27 changed files with 1060 additions and 15 deletions

View file

@ -10,6 +10,7 @@ pipeline:
- rustup component add clippy
# rewrite conducotr configuration
- sed -i 's%url = "http:\/\/localhost:5000"%url = "http:\/\/librepages-conductor:5000"%' config/default.toml
- sed -i 's%url = "http:\/\/localhost:6000"%url = "http:\/\/librepages-forms:6000"%' config/default.toml
- make dev-env
- make migrate
- make lint
@ -34,7 +35,8 @@ pipeline:
password:
from_secret: DOCKER_TOKEN
repo: realaravinth/librepages
tags: latest
tags:
latest
# build_publisher_docker_img:
# image: plugins/docker
@ -64,6 +66,27 @@ services:
environment:
- POSTGRES_PASSWORD=password
forms-postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=password
librepages-forms:
image: realaravinth/librepages-forms
command: forms serve
environment:
- LPFORMS_SOURCE_CODE=https://git.batsense.net/LibrePages/forms
- LPFORMS_SERVER__PROXY_HAS_TLS=false
- LPFORMS_DEBUG=false
- LPFORMS_SERVER_URL_PREFIX=""
- LPFORMS_SERVER_DOMAIN=forms.librepages.test
- LPFORMS_SERVER_IP=0.0.0.0
- LPFORMS_SERVER_PROXY_HAS_TLS=false
- LPFORMS_DASH_API_KEY=longrandomlygeneratedpassword
- LPFORMS_DATABASE_POOL=2
- PORT=6000
- DATABASE_URL=postgres://postgres:password@forms-postgres:5432/postgres
librepages-conductor:
image: realaravinth/librepages-conductor
command: conductor serve
@ -72,11 +95,10 @@ services:
- LPCONDUCTOR_DEBUG=false
- LPCONDUCTOR_CONDUCTOR=dummy
- LPCONDUCTOR_SERVER_URL_PREFIX=""
- LPCONDUCTOR_SERVER_DOMAIN="librepages.test"
- LPCONDUCTOR_SERVER_DOMAIN=librepages.test
- LPCONDUCTOR_SERVER_IP=0.0.0.0
- LPCONDUCTOR_SERVER_PROXY_HAS_TLS=false
- LPCONDUCTOR_SERVER_PORT=7000
- LPCONDUCTOR_SOURCE_CODE=https://example.org
- LPCONDUCTOR_CREDS_USERNAME="librepages_api"
- LPCONDUCTOR_CREDS_PASSWORD="longrandomlygeneratedpassword"
- LPCONDUCTOR_CREDS_TOKEN=longrandomlygeneratedpassword
- PORT=5000

11
Cargo.lock generated
View file

@ -1418,6 +1418,15 @@ dependencies = [
"serde",
]
[[package]]
name = "libforms"
version = "0.1.0"
source = "git+https://git.batsense.net/librepages/forms/#7e50f9be0db184b1b9c551912137ad63106d64ce"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "libgit2-sys"
version = "0.13.4+1.4.2"
@ -1454,6 +1463,7 @@ dependencies = [
"lazy_static",
"libconductor",
"libconfig",
"libforms",
"mime",
"mime_guess",
"mktemp",
@ -1469,6 +1479,7 @@ dependencies = [
"sha2",
"sqlx",
"tera",
"time",
"tokio",
"toml",
"tracing",

View file

@ -23,6 +23,7 @@ sqlx = { version = "0.6.2", features = ["runtime-actix-rustls", "postgres", "tim
clap = { version = "3.2.20", features = ["derive"]}
libconfig = { version = "0.1.0", git = "https://git.batsense.net/librepages/libconfig" }
libconductor = { version = "0.1.0", git = "https://git.batsense.net/librepages/conductor/" }
libforms = { version = "0.1.0", git = "https://git.batsense.net/librepages/forms/" }
config = "0.13"
git2 = "0.14.2"
@ -56,6 +57,7 @@ reqwest = { version = "0.11.13", features = ["json"] }
sha2 = "0.10.6"
hmac = "0.12.1"
hex= "0.4.3"
time = { version = "0.3.17", features = ["formatting"] }
[dependencies.cache-buster]
git = "https://github.com/realaravinth/cache-buster"

View file

@ -5,6 +5,7 @@
**Auto-deploy static websites from git repositories**
</p>
[![status-badge](https://ci.batsense.net/api/badges/LibrePages/librepages/status.svg)](https://ci.batsense.net/LibrePages/librepages)

View file

@ -4,7 +4,7 @@ allow_registration = true
source_code = "https://git.batsense.net/LibrePages/pages"
support_email = "support@librepages.example.org"
conductors = [
{ username = "librepages_api", api_key = "longrandomlygeneratedpassword", url = "http://localhost:5000"}
{ api_key = "longrandomlygeneratedpassword", url = "http://localhost:5000"}
]
[server]
@ -38,3 +38,7 @@ password = "password"
name = "postgres"
pool = 4
database_type="postgres" # "postgres"
[form]
api_key = "longrandomlygeneratedpassword"
url = "http://localhost:6000"

View file

@ -0,0 +1,24 @@
[Unit]
Description=LibrePages: Easiest way to deploy websites
[Service]
Type=simple
User=librepages
ExecStart=/usr/bin/librepages serve
Restart=on-failure
RestartSec=1
SuccessExitStatus=3 4
RestartForceExitStatus=3 4
SystemCallArchitectures=native
MemoryDenyWriteExecute=true
NoNewPrivileges=true
Environment="RUST_LOG=info"
[Unit]
Wants=network-online.target
Wants=network-online.target
Requires=postgresql.service
After=syslog.target
[Install]
WantedBy=multi-user.target

View file

@ -25,7 +25,7 @@ DUMBSERVE_USERNAME=librepages
DUMBSERVE_PASSWORD=$4
DUMBSERVE_HOST="https://$DUMBSERVE_USERNAME:$DUMBSERVE_PASSWORD@dl.librepages.org"
NAME=librebages
NAME=librepages
KEY=67880CA5F4BC99BF247330E2DA576B07BC323961
TMP_DIR=$(mktemp -d)
@ -42,6 +42,9 @@ get_bin(){
#docker cp $container_id:/usr/local/bin/pages $TARGET_DIR/
#docker rm -v $container_id
cp target/release/librepages $TARGET_DIR
cp -r contrib/ $TARGET_DIR
cp -r config/ $TARGET_DIR
mv $TARGET_DIR/config/default.toml $TARGET_DIR/config/config.toml
}
copy() {

View file

@ -15,8 +15,7 @@ docker create --name $NAME -p 5000:5000 \
-e LPCONDUCTOR_SERVER_PROXY_HAS_TLS="false" \
-e LPCONDUCTOR_SERVER_PORT=7000 \
-e LPCONDUCTOR_SOURCE_CODE="https://example.org" \
-e LPCONDUCTOR_CREDS_USERNAME=$LPCONDUCTOR_CREDS_USERNAME \
-e LPCONDUCTOR_CREDS_PASSWORD=$LPCONDUCTOR_CREDS_PASSWORD \
-e LPCONDUCTOR_CREDS_TOKEN=$LPCONDUCTOR_CREDS_PASSWORD \
-e PORT="5000"\
realaravinth/librepages-conductor conductor serve

32
scripts/forms.sh Executable file
View file

@ -0,0 +1,32 @@
#!/bin/bash
readonly NAME=librepages-forms-develop
readonly DB_NAME=librepages-forms-postgres-develop
docker rm -f $NAME
docker rm -f $DB_NAME
docker create --name $DB_NAME -p 5433:5432 \
-e POSTGRES_PASSWORD=password \
postgres
docker create --name $NAME -p 6000:6000 \
--network=host \
-e LPFORMS__SOURCE_CODE="https://git.batsense.net/LibrePages/forms" \
-e LPFORMS_SERVER__PROXY_HAS_TLS=false \
-e LPFORMS_DEBUG="false" \
-e LPFORMS_SERVER_URL_PREFIX="" \
-e LPFORMS_SERVER_DOMAIN="forms.librepages.test" \
-e LPFORMS_SERVER_IP="0.0.0.0" \
-e LPFORMS_SERVER_PROXY_HAS_TLS="false" \
-e LPFORMS_SOURCE_CODE="https://example.org" \
-e LPFORMS_DASH_API_KEY=$LPFORMS_DASH_API_KEY \
-e LPFORMS_DATABASE_POOL=2 \
-e PORT="6000"\
-e DATABASE_URL=postgres://postgres:password@localhost:5433/postgres \
realaravinth/librepages-forms forms serve
docker start $DB_NAME
sleep 5
docker start $NAME

215
src/api/v1/forms.rs Normal file
View file

@ -0,0 +1,215 @@
/*
* 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_identity::Identity;
use actix_web::{web, HttpResponse, Responder};
use libforms::Table;
use serde::{Deserialize, Serialize};
use super::get_auth_middleware;
use crate::{errors::*, AppCtx};
pub mod routes {
pub struct Forms {
pub list_submissions: &'static str,
pub delete_submission: &'static str,
pub get_forms_for_host: &'static str,
}
impl Forms {
pub const fn new() -> Self {
Self {
list_submissions: "/api/v1/forms/submissions/list",
delete_submission: "/api/v1/forms/delete/{id}",
get_forms_for_host: "/api/v1/forms/list",
}
}
pub fn get_list(&self, page: usize) -> String {
format!("{}?page={}", self.list_submissions, page)
}
pub fn get_forms_for_host_route(&self, host: &str) -> String {
format!("{}?host={}", self.get_forms_for_host, host)
}
pub fn get_delete(&self, id: usize, host: &str, path: &str) -> String {
let del = self.delete_submission.replace("{id}", &id.to_string());
format!("{}?host={}&path={}", del, host, path)
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Page {
page: usize,
}
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.forms.list_submissions",
wrap = "get_auth_middleware()"
)]
#[tracing::instrument(name = "List form submission" skip(id, ctx, payload))]
async fn list_submission(
ctx: AppCtx,
id: Identity,
page: web::Query<Page>,
payload: web::Json<Table>,
) -> ServiceResult<impl Responder> {
let owner = id.identity().unwrap();
let resp = ctx
.get_all_form_submission(&owner, page.page, &payload)
.await?;
Ok(HttpResponse::Ok().json(resp))
}
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.forms.delete_submission",
wrap = "get_auth_middleware()"
)]
#[tracing::instrument(name = "Delete form submission" skip(id, ctx))]
async fn delete_form_submission(
ctx: AppCtx,
id: Identity,
sub_id: web::Path<usize>,
payload: web::Json<Table>,
) -> ServiceResult<impl Responder> {
let owner = id.identity().unwrap();
ctx.delete_form_submission(&owner, *sub_id, &payload)
.await?;
Ok(HttpResponse::Ok())
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Host {
pub host: String,
}
#[actix_web_codegen_const_routes::get(
path = "crate::V1_API_ROUTES.forms.get_forms_for_host",
wrap = "get_auth_middleware()"
)]
#[tracing::instrument(name = "Get forms belonging to hostname", skip(ctx, id))]
async fn list_all_forms(
id: Identity,
ctx: AppCtx,
query: web::Query<Host>,
) -> ServiceResult<impl Responder> {
let owner = id.identity().unwrap();
let forms = ctx.get_all_forms_for_host(&owner, &query.host).await?;
Ok(HttpResponse::Ok().json(forms))
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(list_submission);
cfg.service(delete_form_submission);
cfg.service(list_all_forms);
}
#[cfg(test)]
mod tests {
use actix_web::{http::StatusCode, test};
use crate::tests;
use crate::*;
use libforms::*;
#[actix_rt::test]
async fn test_api_forms() {
const NAME: &str = "testapiformuser";
const PASSWORD: &str = "longpasswordasdfa2";
const EMAIL: &str = "testapiformuser@a.com";
let (_dir, ctx) = tests::get_ctx().await;
let _ = ctx.delete_user(NAME, PASSWORD).await;
let (_, signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await;
let page = ctx.add_test_site(NAME.into()).await;
let cookies = get_cookie!(signin_resp);
let app = get_app!(ctx).await;
let site_info = Table {
host: page.domain.clone(),
path: format!("/foo/{NAME}"),
};
if let Ok(subs) = ctx.get_all_form_submission(NAME, 0, &site_info).await {
for s in subs.iter() {
let _ = ctx.delete_form_submission(NAME, s.id, &site_info).await;
}
}
ctx.add_form_submission(NAME, &site_info, &serde_json::to_value(&site_info).unwrap())
.await
.unwrap();
// get all forms for host
let list_forms_resp = test::call_service(
&app,
test::TestRequest::get()
.uri(&V1_API_ROUTES.forms.get_forms_for_host_route(&page.domain))
.cookie(cookies.clone())
.to_request(),
)
.await;
assert_eq!(list_forms_resp.status(), StatusCode::OK);
let forms: Vec<String> = test::read_body_json(list_forms_resp).await;
assert_eq!(forms, vec![site_info.path.to_string()]);
// list subs using REST API
let list_form_submissions = test::call_service(
&app,
post_request!(&site_info, &V1_API_ROUTES.forms.get_list(0))
.cookie(cookies.clone())
.to_request(),
)
.await;
assert_eq!(list_form_submissions.status(), StatusCode::OK);
let subs: Vec<FormSubmissionResp> = test::read_body_json(list_form_submissions).await;
assert_eq!(subs.len(), 1);
assert_eq!(
subs[0].value,
Some(serde_json::to_value(&site_info).unwrap())
);
// delete form submission
let delete_form_submission_resp = test::call_service(
&app,
post_request!(
&site_info,
&V1_API_ROUTES
.forms
.get_delete(subs[0].id, &site_info.host, &site_info.path)
)
.cookie(cookies.clone())
.to_request(),
)
.await;
assert_eq!(delete_form_submission_resp.status(), StatusCode::OK);
// list subs using REST API post deletion. Len = 0
let list_form_submissions = test::call_service(
&app,
post_request!(&site_info, &V1_API_ROUTES.forms.get_list(0))
.cookie(cookies.clone())
.to_request(),
)
.await;
assert_eq!(list_form_submissions.status(), StatusCode::OK);
let subs: Vec<FormSubmissionResp> = test::read_body_json(list_form_submissions).await;
assert!(subs.is_empty());
}
}

View file

@ -21,6 +21,7 @@ use serde::Deserialize;
pub mod account;
pub mod auth;
pub mod forgejo;
pub mod forms;
pub mod meta;
pub mod pages;
pub mod routes;
@ -32,6 +33,7 @@ pub fn services(cfg: &mut ServiceConfig) {
account::services(cfg);
meta::services(cfg);
forgejo::services(cfg);
forms::services(cfg);
pages::services(cfg);
}

View file

@ -20,6 +20,7 @@ use actix_auth_middleware::GetLoginRoute;
use crate::serve::routes::Serve;
use super::forgejo::routes::Forgejo;
use super::forms::routes::Forms;
use super::meta::routes::Meta;
use super::pages::routes::Deploy;
@ -94,6 +95,7 @@ pub struct Routes {
/// Meta routes
pub meta: Meta,
pub forgejo: Forgejo,
pub forms: Forms,
pub deploy: Deploy,
pub serve: Serve,
}
@ -106,6 +108,7 @@ impl Routes {
account: Account::new(),
meta: Meta::new(),
forgejo: Forgejo::new(),
forms: Forms::new(),
deploy: Deploy::new(),
serve: Serve::new(),
}

View file

@ -45,11 +45,10 @@ impl Conductor {
event_url.set_path("/api/v1/events/new");
self.client
.post(event_url)
.basic_auth(&c.username, Some(&c.api_key))
.bearer_auth(&c.api_key)
.json(e)
.send()
.await
.unwrap();
.await?;
}
Ok(())
}

170
src/ctx/api/v1/forms.rs Normal file
View file

@ -0,0 +1,170 @@
/*
* 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/>.
*/
//! Account management utility datastructures and methods
use libforms::*;
pub use super::auth;
use crate::ctx::Ctx;
use crate::errors::*;
impl Ctx {
/// Delete form submission
pub async fn delete_form_submission(
&self,
owner: &str,
sub_id: usize,
payload: &Table,
) -> ServiceResult<()> {
let _site = self.db.get_site(owner, &payload.host).await?;
let mut form_url = self.settings.form.url.clone();
form_url.set_path(&format!("/api/v1/forms/delete/{sub_id}"));
self.client
.post(form_url)
.bearer_auth(&self.settings.form.api_key)
.json(payload)
.send()
.await?;
Ok(())
}
/// Delete form submission
pub async fn get_all_form_submission(
&self,
owner: &str,
page: usize,
payload: &Table,
) -> ServiceResult<Vec<FormSubmissionResp>> {
let _site = self.db.get_site(owner, &payload.host).await?;
let mut form_url = self.settings.form.url.clone();
form_url.set_path("/api/v1/forms/list");
form_url.set_query(Some(&format!("page={}", page)));
let res = self
.client
.post(form_url)
.bearer_auth(&self.settings.form.api_key)
.json(payload)
.send()
.await?
.json()
.await?;
Ok(res)
}
/// List all forms associated with hostname
pub async fn get_all_forms_for_host(
&self,
owner: &str,
host: &str,
) -> ServiceResult<Vec<String>> {
let _site = self.db.get_site(owner, host).await?;
let mut form_url = self.settings.form.url.clone();
form_url.set_path("/api/v1/forms/host");
form_url.set_query(Some(&format!("host={}", host)));
let res = self
.client
.get(form_url)
.bearer_auth(&self.settings.form.api_key)
.send()
.await?
.json()
.await?;
Ok(res)
}
}
#[cfg(test)]
mod tests {
use crate::tests;
use super::*;
impl Ctx {
/// Delete form submission
pub async fn add_form_submission(
&self,
owner: &str,
site_info: &Table,
payload: &serde_json::Value,
) -> ServiceResult<()> {
let _site = self.db.get_site(owner, &site_info.host).await?;
let mut form_url = self.settings.form.url.clone();
form_url.set_path("/api/v1/forms/submit");
form_url.set_query(Some(&format!(
"host={}&path={}",
site_info.host, site_info.path
)));
self.client
.post(form_url)
.json(payload)
.send()
.await
.unwrap();
Ok(())
}
}
#[actix_rt::test]
async fn test_ctx_forms_work() {
const NAME: &str = "testctxformswork";
const PASSWORD: &str = "longpasswordasdfa2";
const EMAIL: &str = "testctxformswork@a.com";
let (_dir, ctx) = tests::get_ctx().await;
let _ = ctx.delete_user(NAME, PASSWORD).await;
let (_, _signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await;
let page = ctx.add_test_site(NAME.into()).await;
let site_info = Table {
host: page.domain.clone(),
path: format!("/foo/{NAME}"),
};
if let Ok(subs) = ctx.get_all_form_submission(NAME, 0, &site_info).await {
for s in subs.iter() {
let _ = ctx.delete_form_submission(NAME, s.id, &site_info).await;
}
}
ctx.add_form_submission(NAME, &site_info, &serde_json::to_value(&site_info).unwrap())
.await
.unwrap();
let subs = ctx
.get_all_form_submission(NAME, 0, &site_info)
.await
.unwrap();
assert_eq!(subs.len(), 1);
assert_eq!(
subs[0].value,
Some(serde_json::to_value(&site_info).unwrap())
);
let forms = ctx
.get_all_forms_for_host(NAME, &page.domain)
.await
.unwrap();
assert_eq!(vec![site_info.path.clone()], forms);
ctx.delete_form_submission(NAME, subs[0].id, &site_info)
.await
.unwrap();
let subs = ctx
.get_all_form_submission(NAME, 0, &site_info)
.await
.unwrap();
assert!(subs.is_empty());
}
}

View file

@ -17,6 +17,7 @@
pub mod account;
pub mod auth;
pub mod forgejo;
pub mod forms;
pub mod pages;
#[cfg(test)]

View file

@ -50,7 +50,6 @@ impl ConnectionOptions {
async fn connect(self) -> ServiceResult<Database> {
let pool = match self {
Self::Fresh(fresh) => {
tracing::info!("DATABASE URL: {}", fresh.url);
let mut connect_options =
sqlx::postgres::PgConnectOptions::from_str(&fresh.url).unwrap();
if fresh.disable_logging {

View file

@ -30,7 +30,9 @@ use derive_more::{Display, Error};
use git2::Error as GitError;
use hmac::digest::InvalidLength;
use hmac::digest::MacError;
use reqwest::Error as ReqwestError;
use serde::{Deserialize, Serialize};
use tracing::error;
use url::ParseError;
use crate::page::Page;
@ -186,6 +188,14 @@ pub enum ServiceError {
WebhookNotFound,
}
impl From<ReqwestError> for ServiceError {
#[cfg(not(tarpaulin_include))]
fn from(e: ReqwestError) -> ServiceError {
error!("{}", e);
ServiceError::InternalServerError
}
}
impl From<InvalidLength> for ServiceError {
#[cfg(not(tarpaulin_include))]
fn from(_: InvalidLength) -> ServiceError {

View file

@ -0,0 +1,109 @@
/*
* 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 serde::{Deserialize, Serialize};
use tera::Context;
use super::get_auth_middleware;
use crate::api::v1::forms::Host;
use crate::errors::ServiceResult;
use crate::pages::errors::*;
use crate::settings::Settings;
use crate::AppCtx;
pub use super::*;
pub const DASH_SITE_FORM_HOST_LIST: TemplateFile = TemplateFile::new(
"dash_site_form_host_list",
"pages/dash/sites/forms/host-list.html",
);
pub struct List {
ctx: RefCell<Context>,
}
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct Form {
pub path: String,
pub view: String,
}
impl Form {
pub fn new(path: String, host: &str) -> Self {
let view = PAGES.dash.site.forms.get_view(0, host, &path);
Self { path, view }
}
}
impl CtxError for List {
fn with_error(&self, e: &ReadableError) -> String {
self.ctx.borrow_mut().insert(ERROR_KEY, e);
self.render()
}
}
impl List {
pub fn new(settings: &Settings, forms: Option<&[Form]>) -> Self {
let ctx = RefCell::new(context(settings));
if let Some(forms) = forms {
ctx.borrow_mut().insert(PAYLOAD_KEY, forms);
}
Self { ctx }
}
pub fn render(&self) -> String {
TEMPLATES
.render(DASH_SITE_FORM_HOST_LIST.name, &self.ctx.borrow())
.unwrap()
}
}
async fn get_forms(ctx: &AppCtx, id: &Identity, host: &str) -> ServiceResult<Vec<Form>> {
let owner = id.identity().unwrap();
let mut db_forms = ctx.get_all_forms_for_host(&owner, host).await?;
let mut forms = Vec::with_capacity(db_forms.len());
for form in db_forms.drain(0..) {
forms.push(Form::new(form, host));
}
Ok(forms)
}
#[actix_web_codegen_const_routes::get(
path = "PAGES.dash.site.forms.list_forms",
wrap = "get_auth_middleware()"
)]
#[tracing::instrument(name = "List all forms belonging to host" skip(ctx, id))]
pub async fn list_forms(
ctx: AppCtx,
id: Identity,
query: web::Query<Host>,
) -> PageResult<impl Responder, List> {
let sites = get_forms(&ctx, &id, &query.host)
.await
.map_err(|e| PageError::new(List::new(&ctx.settings, None), e))?;
let home = List::new(&ctx.settings, Some(&sites)).render();
let html = ContentType::html();
Ok(HttpResponse::Ok().content_type(html).body(home))
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(list_forms);
}

View file

@ -0,0 +1,42 @@
/*
* 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::*;
use super::get_auth_middleware;
pub use super::{context, Footer, TemplateFile, PAGES, PAYLOAD_KEY, TEMPLATES};
//pub mod delete;
pub mod list;
pub mod view;
pub fn register_templates(t: &mut tera::Tera) {
list::DASH_SITE_FORM_HOST_LIST
.register(t)
.expect(list::DASH_SITE_FORM_HOST_LIST.name);
view::DASH_SITE_FORM_VIEW
.register(t)
.expect(view::DASH_SITE_FORM_VIEW.name);
// delete::DASH_SITE_DELETE
// .register(t)
// .expect(delete::DASH_SITE_DELETE.name);
}
pub fn services(cfg: &mut web::ServiceConfig) {
list::services(cfg);
view::services(cfg);
//delete::services(cfg);
}

View file

@ -0,0 +1,283 @@
/*
* 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 libforms::{FormSubmissionResp, Table};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tera::Context;
use time::{format_description, OffsetDateTime};
use super::get_auth_middleware;
use crate::pages::errors::*;
use crate::settings::Settings;
use crate::AppCtx;
pub use super::*;
pub const DASH_SITE_FORM_VIEW: TemplateFile =
TemplateFile::new("dash_site_form_view", "pages/dash/sites/forms/view.html");
pub struct View {
ctx: RefCell<Context>,
}
impl CtxError for View {
fn with_error(&self, e: &ReadableError) -> String {
self.ctx.borrow_mut().insert(ERROR_KEY, e);
self.render()
}
}
impl View {
pub fn new(settings: &Settings, payload: Option<TemplateForms>) -> 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_SITE_FORM_VIEW.name, &self.ctx.borrow())
.unwrap()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct TemplateForms {
pub submissions: Vec<TemplateSubmission>,
pub next_page: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, Default, Eq, PartialEq)]
pub struct TemplateSubmission {
pub value: TemplateFormData,
pub delete: String,
pub time: String,
pub id: usize,
}
#[derive(Serialize, Deserialize, Clone, Debug, Default, Eq, PartialEq)]
pub struct TemplateFormData {
pub titles: Vec<String>,
pub rows: Vec<Vec<String>>,
}
impl From<Value> for TemplateFormData {
fn from(json: Value) -> Self {
let mut titles = Vec::default();
let mut rows: Vec<Vec<String>> = Vec::default();
match json {
Value::Object(m) => {
let mut row: Vec<String> = Vec::default();
for (k, v) in m.iter() {
titles.push(k.clone());
let value = match v {
Value::String(s) => s.clone(),
Value::Null => "-".into(),
Value::Bool(b) => b.to_string(),
Value::Number(n) => n.to_string(),
Value::Array(a) => format!("{:?}", a),
Value::Object(_) => unimplemented!(),
};
row.push(value);
}
rows.push(row);
}
_ => unimplemented!(),
}
Self { titles, rows }
}
}
impl TemplateSubmission {
fn from_form_submission_resp(f: FormSubmissionResp, host: &str, path: &str) -> Self {
let format = format_description::parse(
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \
sign:mandatory]:[offset_minute]:[offset_second]",
)
.unwrap();
let time = OffsetDateTime::from_unix_timestamp(f.time)
.unwrap()
.format(&format)
.unwrap();
Self {
value: f.value.unwrap().into(),
id: f.id,
time,
delete: PAGES.dash.site.forms.get_delete(f.id, host, path),
}
}
}
impl TemplateForms {
pub fn new(
current_page: usize,
mut form_submissions: Vec<FormSubmissionResp>,
host: &str,
path: &str,
) -> Self {
let mut submissions = Vec::with_capacity(form_submissions.len());
for sub in form_submissions.drain(0..) {
let sub = TemplateSubmission::from_form_submission_resp(sub, host, path);
submissions.push(sub);
}
let next_page = PAGES.dash.site.forms.get_view(current_page + 1, host, path);
Self {
next_page,
submissions,
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct FormPage {
page: usize,
host: String,
path: String,
}
impl FormPage {
pub fn to_table(self) -> (Table, usize) {
(
Table {
host: self.host,
path: self.path,
},
self.page,
)
}
}
#[actix_web_codegen_const_routes::get(
path = "PAGES.dash.site.forms.view",
wrap = "get_auth_middleware()"
)]
#[tracing::instrument(name = "Dashboard view site form submissions webpage", skip(ctx, id))]
pub async fn get_form_submissions(
ctx: AppCtx,
id: Identity,
payload: web::Query<FormPage>,
) -> PageResult<impl Responder, View> {
let owner = id.identity().unwrap();
let payload = payload.into_inner();
let (table, page) = payload.to_table();
let resp = ctx
.get_all_form_submission(&owner, page, &table)
.await
.unwrap();
// .map_err(|e| PageError::new(View::new(&ctx.settings, None), e))?;
let payload = TemplateForms::new(page, resp, &table.host, &table.path);
let page = View::new(&ctx.settings, Some(payload));
let body = page.render();
let html = ContentType::html();
Ok(HttpResponse::Ok().content_type(html).body(body))
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(get_form_submissions);
}
#[cfg(test)]
mod tests {
use actix_web::http::StatusCode;
use actix_web::test;
use serde::{Deserialize, Serialize};
use crate::ctx::api::v1::auth::Password;
use crate::ctx::ArcCtx;
use crate::errors::ServiceError;
use crate::pages::dash::sites::add::TemplateAddSite;
use crate::tests;
use crate::*;
use super::Table;
use super::PAGES;
#[actix_rt::test]
async fn postgres_dashboard_form_works() {
let (_, ctx) = tests::get_ctx().await;
dashboard_site_form_works(ctx.clone()).await;
}
async fn dashboard_site_form_works(ctx: ArcCtx) {
const NAME: &str = "testdashsiteformuser";
const EMAIL: &str = "testdashsiteformuser@foo.com";
const PASSWORD: &str = "longpassword";
let _ = ctx.delete_user(NAME, PASSWORD).await;
let (_, signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await;
let cookies = get_cookie!(signin_resp);
let app = get_app!(ctx.clone()).await;
let page = ctx.add_test_site(NAME.into()).await;
let site_info = Table {
host: page.domain.clone(),
path: format!("/foo/{NAME}"),
};
#[derive(Serialize, Deserialize)]
struct Foo {
foo: String,
}
if let Ok(subs) = ctx.get_all_form_submission(NAME, 0, &site_info).await {
for s in subs.iter() {
let _ = ctx.delete_form_submission(NAME, s.id, &site_info).await;
}
}
let foo = Foo {
foo: "barvalue".into(),
};
ctx.add_form_submission(NAME, &site_info, &serde_json::to_value(&foo).unwrap())
.await
.unwrap();
// list forms for host
let resp = get_request!(
&app,
&PAGES.dash.site.forms.get_list_host(&site_info.host),
cookies.clone()
);
assert_eq!(resp.status(), StatusCode::OK);
let res = String::from_utf8(test::read_body(resp).await.to_vec()).unwrap();
assert!(res.contains(&site_info.path));
// view form submission
let resp = get_request!(
&app,
&PAGES
.dash
.site
.forms
.get_view(0, &site_info.host, &site_info.path),
cookies.clone()
);
assert_eq!(resp.status(), StatusCode::OK);
let res = String::from_utf8(test::read_body(resp).await.to_vec()).unwrap();
assert!(res.contains("foo"));
assert!(res.contains("barvalue"));
}
}

View file

@ -22,6 +22,7 @@ pub use super::{context, Footer, TemplateFile, PAGES, PAYLOAD_KEY, TEMPLATES};
pub mod add;
pub mod delete;
pub mod forms;
pub mod view;
pub fn register_templates(t: &mut tera::Tera) {
@ -34,10 +35,12 @@ pub fn register_templates(t: &mut tera::Tera) {
delete::DASH_SITE_DELETE
.register(t)
.expect(delete::DASH_SITE_DELETE.name);
forms::register_templates(t);
}
pub fn services(cfg: &mut web::ServiceConfig) {
add::services(cfg);
view::services(cfg);
delete::services(cfg);
forms::services(cfg);
}

View file

@ -73,6 +73,7 @@ impl View {
pub struct TemplateSiteWithEvents {
pub site: Site,
pub view: String,
pub forms: String,
pub delete: String,
pub last_update: Option<TemplateSiteEvent>,
pub events: Vec<TemplateSiteEvent>,
@ -86,10 +87,12 @@ impl TemplateSiteWithEvents {
) -> Self {
let view = PAGES.dash.site.get_view(site.pub_id);
let delete = PAGES.dash.site.get_delete(site.pub_id);
let forms = PAGES.dash.site.forms.get_list_host(&site.hostname);
Self {
site,
last_update,
view,
forms,
delete,
events,
}

View file

@ -114,6 +114,44 @@ impl ForgejoWebhook {
}
}
#[derive(Serialize)]
/// Dashboard Site routes
pub struct DashSiteForms {
/// view form submission route
pub view: &'static str,
/// delete form submission route
pub delete: &'static str,
/// list forms belonging to hostname
pub list_forms: &'static str,
}
impl DashSiteForms {
/// create new instance of DashSiteForms route
pub const fn new() -> DashSiteForms {
let view = "/dash/site/forms/submission/view";
let delete = "/dash/site/forms/submission/delete";
let list_forms = "/dash/site/forms/list";
DashSiteForms {
view,
delete,
list_forms,
}
}
pub fn get_list_host(&self, host: &str) -> String {
format!("{}?host={}", self.list_forms, host)
}
pub fn get_view(&self, page: usize, host: &str, path: &str) -> String {
format!("{}?host={}&path={}&page={}", self.view, host, path, page)
}
pub fn get_delete(&self, id: usize, host: &str, path: &str) -> String {
format!("{}?host={}&path={}&id={}", self.view, host, path, id)
}
}
#[derive(Serialize)]
/// Dashboard Site routes
pub struct DashSite {
@ -123,6 +161,9 @@ pub struct DashSite {
pub view: &'static str,
/// delete site route
pub delete: &'static str,
/// forms routes
pub forms: DashSiteForms,
}
impl DashSite {
@ -131,7 +172,13 @@ impl DashSite {
let add = "/dash/site/add";
let view = "/dash/site/view/{deployment_pub_id}";
let delete = "/dash/site/delete/{deployment_pub_id}";
DashSite { add, view, delete }
let forms = DashSiteForms::new();
DashSite {
add,
view,
delete,
forms,
}
}
pub fn get_view(&self, deployment_pub_id: Uuid) -> String {

View file

@ -83,11 +83,17 @@ pub struct Settings {
pub database: Database,
pub page: PageConfig,
pub conductors: Vec<Conductor>,
pub form: Forms,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Conductor {
pub username: String,
pub api_key: String,
pub url: Url,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Forms {
pub api_key: String,
pub url: Url,
}
@ -104,7 +110,7 @@ impl Settings {
let mut s = Config::builder();
const CURRENT_DIR: &str = "./config/default.toml";
const ETC: &str = "/etc/static-pages/config.toml";
const ETC: &str = "/etc/liberapages/librepages/config.toml";
let mut read_file = false;

View file

@ -0,0 +1,16 @@
{% extends 'base' %}{% block title %} List Forms {% endblock title %} {% block nav
%} {% include "auth_nav" %} {% endblock nav %} {% block main %}
<main class="sites__main">
<div class="sites__collection">
{% if payload|length > 0 %}
{% for form in payload %}
<a href="{{ form.view }}" class="site__container">{{ form.path }}</a>
{% endfor %}
{% else %}
<p class="sites__banner">Form is empty</p>
{% endif %}
</div>
</main>
{% endblock main %}

View file

@ -0,0 +1,34 @@
{% extends 'base' %}{% block title %} View Data {% endblock title %} {% block nav
%} {% include "auth_nav" %} {% endblock nav %} {% block main %}
<main class="sites__main">
<div class="add-site__container">
<section>
{% for submission in payload.submissions %}
<table>
<thead>
<tr>
{% for title in submission.value.titles %}
<th>{{ title }}</th>
{% endfor %}
<th>Time</th>
</tr>
</thead>
{% for row in submission.value.rows %}
<tbody>
<tr>
{% for value in row %}
<th>{{ value }}</th>
{% endfor %}
<th>{{ submission.time }}</th>
</tr>
</tbody>
{% endfor %}
</table>
{% endfor %}
</section>
<a href="{{payload.next_page}}">Next ></a>
</div>
</main>
{% endblock main %}

View file

@ -28,6 +28,11 @@
</td>
</tr>
<tr>
<th>Forms</th>
<td><a href="{{ payload.forms }}" >See forms</a></td>
</tr>
<tr>
<th>Branch</th>
<td>{{ payload.site.branch }}</td>