0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-24 15:08:22 -05:00

adding rust automation ci for testing and updating goals.org

This commit is contained in:
neon_arch 2023-04-28 13:15:41 +03:00
commit 7d4f2296f4
5 changed files with 135 additions and 34 deletions

29
.github/workflows/rust.yml vendored Normal file
View File

@ -0,0 +1,29 @@
name: Rust
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
toolchain:
- stable
- beta
- nightly
steps:
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

View File

@ -2,8 +2,6 @@
- [ ] Add unit tests, integration test, doc tests in the project source code.
- [ ] Write .gitlab-ci.yml for automating testing of the project.
- [ ] Add code to remove nsfw content from search results using a blocklist.
- [ ] Add code to disallow user to search sensitive content (similar functionality to swisscows search engine) if strict safe search is turned on.
@ -24,6 +22,8 @@
- [ ] Add search engine logo to index page and to the navbar (on the right hand side).
- [X] Write rust.yml for automating testing of the project.
- [X] Add better documentation to source code.
- [X] Add FAQ section in readme.org for answering commonly asked questions like why websurfx?, why rust?, why GPLv3?, etc.

View File

@ -3,15 +3,11 @@
//! This module contains the main function which handles the logging of the application to the
//! stdout and handles the command line arguments provided and launches the `websurfx` server.
use std::ops::RangeInclusive;
use std::{ops::RangeInclusive, net::TcpListener};
use websurfx::server::routes;
use actix_files as fs;
use actix_web::{middleware::Logger, web, App, HttpServer};
use clap::{command, Parser};
use env_logger::Env;
use handlebars::Handlebars;
use websurfx::run;
/// A commandline arguments struct.
#[derive(Parser, Debug, Default)]
@ -67,30 +63,7 @@ async fn main() -> std::io::Result<()> {
log::info!("started server on port {}", args.port);
let mut handlebars: Handlebars = Handlebars::new();
let listener = TcpListener::bind(("127.0.0.1", args.port))?;
handlebars
.register_templates_directory(".html", "./public/templates")
.unwrap();
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
HttpServer::new(move || {
App::new()
.app_data(handlebars_ref.clone())
.wrap(Logger::default()) // added logging middleware for logging.
// Serve images and static files (css and js files).
.service(fs::Files::new("/static", "./public/static").show_files_listing())
.service(fs::Files::new("/images", "./public/images").show_files_listing())
.service(routes::robots_data) // robots.txt
.service(routes::index) // index page
.service(routes::search) // search page
.service(routes::about) // about page
.service(routes::settings) // settings page
.default_service(web::route().to(routes::not_found)) // error page
})
// Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080.
.bind(("127.0.0.1", args.port))?
.run()
.await
run(listener)?.await
}

View File

@ -1,3 +1,60 @@
pub mod engines;
pub mod server;
pub mod search_results_handler;
use std::net::TcpListener;
use crate::server::routes;
use actix_files as fs;
use actix_web::{middleware::Logger, web, App, HttpServer, dev::Server};
use handlebars::Handlebars;
/// Runs the web server on the provided TCP listener and returns a `Server` instance.
///
/// # Arguments
///
/// * `listener` - A `TcpListener` instance representing the address and port to listen on.
///
/// # Returns
///
/// Returns a `Result` containing a `Server` instance on success, or an `std::io::Error` on failure.
///
/// # Example
///
/// ```
/// use std::net::TcpListener;
/// use web_server::Server;
///
/// let listener = TcpListener::bind("127.0.0.1:8080").expect("Failed to bind address");
/// let server = Server::run(listener).expect("Failed to start server");
/// ```
pub fn run(listener: TcpListener) -> std::io::Result<Server> {
let mut handlebars: Handlebars = Handlebars::new();
handlebars
.register_templates_directory(".html", "./public/templates")
.unwrap();
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
let server = HttpServer::new(move || {
App::new()
.app_data(handlebars_ref.clone())
.wrap(Logger::default()) // added logging middleware for logging.
// Serve images and static files (css and js files).
.service(fs::Files::new("/static", "./public/static").show_files_listing())
.service(fs::Files::new("/images", "./public/images").show_files_listing())
.service(routes::robots_data) // robots.txt
.service(routes::index) // index page
.service(routes::search) // search page
.service(routes::about) // about page
.service(routes::settings) // settings page
.default_service(web::route().to(routes::not_found)) // error page
})
// Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080.
.listen(listener)?
.run();
Ok(server)
}

42
tests/index.rs Normal file
View File

@ -0,0 +1,42 @@
use std::net::TcpListener;
use handlebars::Handlebars;
use websurfx::run;
// Starts a new instance of the HTTP server, bound to a random available port
fn spawn_app() -> String {
// Binding to port 0 will trigger the OS to assign a port for us.
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let server = run(listener).expect("Failed to bind address");
tokio::spawn(server);
format!("http://127.0.0.1:{}/", port)
}
// Creates a new instance of Handlebars and registers the templates directory.
// This is used to compare the rendered template with the response body.
fn handlebars() -> Handlebars<'static> {
let mut handlebars = Handlebars::new();
handlebars
.register_templates_directory(".html", "./public/templates")
.unwrap();
handlebars
}
#[tokio::test]
async fn test_index() {
let address = spawn_app();
let client = reqwest::Client::new();
let res = client.get(address).send().await.unwrap();
assert_eq!(res.status(), 200);
let handlebars = handlebars();
let template = handlebars.render("index", &()).unwrap();
assert_eq!(res.text().await.unwrap(), template);
}