Crest Configuration Guide
Complete guide to configuring your Crest application.
Table of Contents
- Basic Configuration
- Application Settings
- Server Settings
- Documentation Settings
- Network Settings
- Production Configuration
- Environment-Specific Configuration
Basic Configuration
C++ Configuration
#include "crest/crest.hpp"
crest::Config config;
config.title = "My API";
config.description = "API Description";
config.version = "1.0.0";
crest::App app(config);
C Configuration
#include "crest/crest.h"
crest_config_t config = {
.title = "My API",
.description = "API Description",
.version = "1.0.0",
.docs_enabled = true
};
crest_app_t* app = crest_create_with_config(&config);
Application Settings
Title
Set the application title displayed in documentation.
C++:
C:
Description
Set the application description displayed in documentation.
C++:
config.description = "A powerful RESTful API for managing resources";
// or
app.set_description("A powerful RESTful API for managing resources");
C:
Version
Set the API version.
C++:
C:
Server Settings
Host and Port
Configure the server host and port.
C++:
// Default: 0.0.0.0:8000
app.run("0.0.0.0", 8000);
// Localhost only
app.run("127.0.0.1", 3000);
// Specific interface
app.run("192.168.1.100", 8080);
C:
Connection Limits
C++:
Timeout
C++:
Documentation Settings
Enable/Disable Documentation
C++:
C:
Custom Documentation Paths
C++:
config.docs_path = "/api-docs"; // Default: /docs
config.openapi_path = "/api-spec.json"; // Default: /openapi.json
Documentation Example
crest::Config config;
config.title = "E-Commerce API";
config.description = "RESTful API for e-commerce platform";
config.version = "2.1.0";
config.docs_enabled = true;
config.docs_path = "/documentation";
config.openapi_path = "/api-spec.json";
crest::App app(config);
Network Settings
Proxy Configuration
Configure proxy settings for outbound requests.
C++:
config.proxy_url = "http://proxy.company.com:8080";
// or
app.set_proxy("http://proxy.company.com:8080");
C:
Proxy with Authentication
Production Configuration
Recommended Production Settings
C++:
crest::Config config;
config.title = "Production API";
config.description = "Production RESTful API";
config.version = "1.0.0";
config.docs_enabled = false; // Disable docs in production
config.max_connections = 2000; // Higher connection limit
config.timeout_seconds = 60; // Longer timeout
crest::App app(config);
app.run("0.0.0.0", 8000);
Security Considerations
-
Disable Documentation
-
Bind to Specific Interface
-
Use Reverse Proxy
- Deploy behind nginx or Apache
- Let reverse proxy handle SSL/TLS
- Configure rate limiting at proxy level
Environment-Specific Configuration
Using Environment Variables
C++:
#include <cstdlib>
std::string getEnv(const char* key, const char* default_value) {
const char* val = std::getenv(key);
return val ? std::string(val) : std::string(default_value);
}
int main() {
crest::Config config;
config.title = getEnv("API_TITLE", "My API");
config.version = getEnv("API_VERSION", "1.0.0");
bool is_production = getEnv("ENVIRONMENT", "development") == "production";
config.docs_enabled = !is_production;
crest::App app(config);
std::string host = getEnv("HOST", "0.0.0.0");
int port = std::stoi(getEnv("PORT", "8000"));
app.run(host, port);
return 0;
}
Development Configuration
crest::Config dev_config;
dev_config.title = "Dev API";
dev_config.docs_enabled = true;
dev_config.max_connections = 100;
dev_config.timeout_seconds = 30;
crest::App app(dev_config);
app.run("127.0.0.1", 8000);
Staging Configuration
crest::Config staging_config;
staging_config.title = "Staging API";
staging_config.docs_enabled = true; // Keep docs for testing
staging_config.max_connections = 500;
staging_config.timeout_seconds = 45;
crest::App app(staging_config);
app.run("0.0.0.0", 8000);
Production Configuration
crest::Config prod_config;
prod_config.title = "Production API";
prod_config.docs_enabled = false; // Disable docs
prod_config.max_connections = 2000;
prod_config.timeout_seconds = 60;
crest::App app(prod_config);
app.run("0.0.0.0", 8000);
Configuration File Example
JSON Configuration (Custom Implementation)
{
"app": {
"title": "My API",
"description": "RESTful API",
"version": "1.0.0"
},
"server": {
"host": "0.0.0.0",
"port": 8000,
"max_connections": 1000,
"timeout_seconds": 30
},
"docs": {
"enabled": true,
"path": "/docs",
"openapi_path": "/openapi.json"
},
"network": {
"proxy_url": ""
}
}
Loading Configuration
#include "crest/crest.hpp"
#include <fstream>
#include <nlohmann/json.hpp> // Example JSON library
crest::Config load_config(const std::string& filename) {
std::ifstream file(filename);
nlohmann::json j;
file >> j;
crest::Config config;
config.title = j["app"]["title"];
config.description = j["app"]["description"];
config.version = j["app"]["version"];
config.docs_enabled = j["docs"]["enabled"];
config.docs_path = j["docs"]["path"];
config.openapi_path = j["docs"]["openapi_path"];
config.max_connections = j["server"]["max_connections"];
config.timeout_seconds = j["server"]["timeout_seconds"];
if (!j["network"]["proxy_url"].empty()) {
config.proxy_url = j["network"]["proxy_url"];
}
return config;
}
int main() {
crest::Config config = load_config("config.json");
crest::App app(config);
// Load server settings
std::ifstream file("config.json");
nlohmann::json j;
file >> j;
app.run(j["server"]["host"], j["server"]["port"]);
return 0;
}
Complete Configuration Example
#include "crest/crest.hpp"
#include <iostream>
#include <cstdlib>
int main() {
// Determine environment
const char* env = std::getenv("ENVIRONMENT");
std::string environment = env ? env : "development";
// Create configuration
crest::Config config;
config.title = "My API";
config.description = "Production-ready RESTful API";
config.version = "1.0.0";
// Environment-specific settings
if (environment == "production") {
config.docs_enabled = false;
config.max_connections = 2000;
config.timeout_seconds = 60;
} else if (environment == "staging") {
config.docs_enabled = true;
config.max_connections = 500;
config.timeout_seconds = 45;
} else { // development
config.docs_enabled = true;
config.max_connections = 100;
config.timeout_seconds = 30;
}
// Create app
crest::App app(config);
// Register routes
app.get("/", [](crest::Request& req, crest::Response& res) {
res.json(200, R"({"status":"ok"})");
});
// Get host and port from environment
const char* host_env = std::getenv("HOST");
const char* port_env = std::getenv("PORT");
std::string host = host_env ? host_env : "0.0.0.0";
int port = port_env ? std::stoi(port_env) : 8000;
// Start server
std::cout << "Starting server in " << environment << " mode\n";
std::cout << "Listening on " << host << ":" << port << "\n";
try {
app.run(host, port);
} catch (const crest::Exception& e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
return 0;
}
Configuration Best Practices
- Use Environment Variables for deployment-specific settings
- Disable Documentation in production environments
- Set Appropriate Timeouts based on your use case
- Configure Connection Limits based on expected load
- Use Configuration Files for complex setups
- Validate Configuration before starting the server
- Document Your Configuration for team members
- Version Your Configuration files
- Use Secrets Management for sensitive data
- Test Configuration in staging before production
Troubleshooting
Port Already in Use
try {
app.run("0.0.0.0", 8000);
} catch (const crest::Exception& e) {
std::cerr << "Failed to start: " << e.what() << "\n";
std::cerr << "Try a different port\n";
}
Permission Denied (Port < 1024)
On Unix systems, ports below 1024 require root privileges:
Configuration Not Applied
Ensure configuration is set before calling run():