| Crates.io | sunweb |
| lib.rs | sunweb |
| version | 0.2.1 |
| created_at | 2025-10-04 20:50:46.831267+00 |
| updated_at | 2025-11-03 15:34:21.374818+00 |
| description | A small lightweight rust webframework. |
| homepage | https://github.com/Sunnickel/SunWeb |
| repository | https://github.com/Sunnickel/SunWeb |
| max_upload_size | |
| id | 1868387 |
| size | 210,802 |
Sunnickel | 04.10.2025
A lightweight, fast, and flexible HTTP/HTTPS web server written in Rust with domain routing, middleware support, and TLS capabilities.
Add this to your Cargo.toml:
[dependencies] sunweb = "0.2.1"
Or install from GitHub:
[dependencies] sunweb = { git = "https://github.com/Sunnickel/SunWeb" }
use sunweb::webserver::{WebServer, ServerConfig};
use sunweb::WEB_LOGGER;
fn main() {
// Initialize logger
log::set_logger(&WEB_LOGGER).unwrap();
log::set_max_level(log::LevelFilter::Info);
// Create server configuration
let config = ServerConfig::new([127, 0, 0, 1], 8080)
.set_base_domain("localhost".to_string());
// Create and configure server
let mut server = WebServer::new(config);
// Add a simple route
server.add_route_file("/", "./static/index.html", None).unwrap();
// Start the server
server.start();
}
use sunweb::webserver::{WebServer, ServerConfig};
let config = ServerConfig::new([127, 0, 0, 1], 8080);
let server = WebServer::new(config); server.start();
let config = ServerConfig::new([127, 0, 0, 1], 443).add_cert("private_key.pem".to_string(), "cert.pem".to_string()).expect("Failed to load certificates");
let server = WebServer::new(config); server.start();
use sunweb::webserver::responses::{Response, ResponseCodes};
use std::sync::Arc;
server.add_custom_route("/api/hello", | req, domain| {
let content = Arc::new(String::from(r#"{"message": "Hello, World!"}"#));
let mut response = Response::new(content, Some(ResponseCodes::Ok), None);
response.headers.add_header("Content-Type", "application/json");
response
}, None).unwrap();
Static File Serving
// Serve all files in the ./static folder under /static route
server.add_static_route("/static", "./static", None).unwrap();
Working with Cookies
use sunweb::webserver::cookie::{Cookie, SameSite};
use sunweb::webserver::Domain;
server.add_custom_route("/set-cookie", | req, domain| {
let content = Arc::new(String::from("Cookie set!"));
let mut response = Response::new(content, None, None);
let cookie = Cookie::new("session_id", "abc123", domain)
.expires(Some(3600)) // 1 hour
.secure()
.http_only()
.same_site(SameSite::Strict);
response.add_cookie(cookie);
response
}, None).unwrap();
Subdomain Routing
use sunweb::webserver::Domain;
let api_domain = Domain::new("api");
server.add_subdomain_router( & api_domain);
// Add routes specifically for api.yourdomain.com
server.add_custom_route("/users", | req, domain| {
// API logic here
Response::new(
Arc::new(String::from(r#"{"users": []}"#)),
Some(ResponseCodes::Ok),
None
)
}, Some( & api_domain)).unwrap();
Configure your web server:
new(host: [u8; 4], port: u16) - Create new configurationadd_cert(private_key: String, cert: String) - Enable HTTPSset_base_domain(domain: String) - Set the base domainMain server instance:
new(config: ServerConfig) - Create new serverstart() - Start listening for connectionsadd_route_file(route: &str, file: &str, domain: Option<&Domain>) - Add file routeadd_static_route(route: &str, folder: &str, domain: Option<&Domain>) - Add static folderadd_custom_route(route: &str, handler: Fn, domain: Option<&Domain>) - Add custom handleradd_subdomain_router(domain: &Domain) - Enable subdomain routingadd_error_route(status_code: ResponseCodes, file: &str, domain: Option<&Domain>) - Add custom error-pagesHTTP response handling:
new(content: Arc<String>, code: Option<ResponseCodes>, protocol: Option<String>) - Create responseadd_cookie(cookie: Cookie) - Add cookie to responseheaders.add_header(key: &str, value: &str) - Add custom headerHTTP request information:
protocol: String - HTTP protocol versionmethod: String - HTTP method (GET, POST, etc.)route: String - Requested routevalues: HashMap<String, String> - Request headersremote_addr: String - Client IP addressget_cookies() - Get all cookiesget_cookie(key: &str) - Get specific cookieRustWebservice/
├── src/
│ ├── webserver/
│ │ ├── client_handling/ # Client connection handling
│ │ ├── cookie/ # Cookie management
│ │ ├── files/ # Static file serving
│ │ ├── logger/ # Colored logging
│ │ ├── middleware/ # Middleware system
│ │ ├── requests/ # Request parsing
│ │ ├── responses/ # Response building
│ │ └── server_config/ # Server configuration
│ └── lib.rs
├── Cargo.toml
└── README.md
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Sunnickel
This project's documentation, including this README.md file, has been created with the assistance of AI.
Specifically, AI was used for:
README.mdAll content has been reviewed and edited to ensure accuracy and clarity. While AI tools were utilized to accelerate the writing process, all information has been manually checked for correctness.