Merge branch 'development_fix' of github.com:freeflowuniverse/herolib into development_fix

This commit is contained in:
2025-02-09 13:51:43 +01:00
3 changed files with 338 additions and 0 deletions

124
lib/osal/traefik/README.md Normal file
View File

@@ -0,0 +1,124 @@
# Traefik Module
This module provides functionality to manage Traefik configurations using Redis as a Key-Value store provider.
## Overview
The module allows you to:
- Define HTTP/HTTPS routes
- Configure backend services
- Set up middlewares
- Manage TLS certificates
- Store configurations in Redis using Traefik's KV store format
## Usage Example
```v
import freeflowuniverse.herolib.lib.osal.traefik
fn main() ! {
// Create a new Traefik configuration
mut config := traefik.new_traefik_config()
// Add a router with a service
config.add_route(
name: 'my-router'
rule: 'Host(`example.com`)'
service: 'my-service'
middlewares: ['auth']
tls: true
)
// Add the corresponding service
config.add_service(
name: 'my-service'
load_balancer: traefik.LoadBalancerConfig{
servers: [
traefik.ServerConfig{url: 'http://localhost:8080'},
traefik.ServerConfig{url: 'http://localhost:8081'}
]
}
)
// Add a basic auth middleware
config.add_middleware(
name: 'auth'
typ: 'basicAuth'
settings: {
'users': '["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"]'
}
)
// Add TLS configuration
config.add_tls(
domain: 'example.com'
cert_file: '/path/to/cert.pem'
key_file: '/path/to/key.pem'
)
// Store configuration in Redis
config.set()!
}
```
## Redis Key Structure
The module uses the following Redis key structure as per Traefik's KV store specification:
- `traefik/http/routers/<name>/*` - Router configurations
- `traefik/http/services/<name>/*` - Service configurations
- `traefik/http/middlewares/<name>/*` - Middleware configurations
- `traefik/tls/certificates` - TLS certificate configurations
## Configuration Types
### Router Configuration
```v
RouteConfig {
name: string // Router name
rule: string // Routing rule (e.g., "Host(`example.com`)")
service: string // Service to forward to
middlewares: []string // Middleware chain
priority: int // Route priority
tls: bool // Enable TLS
}
```
### Service Configuration
```v
ServiceConfig {
name: string
load_balancer: LoadBalancerConfig
}
LoadBalancerConfig {
servers: []ServerConfig
}
ServerConfig {
url: string
}
```
### Middleware Configuration
```v
MiddlewareConfig {
name: string
typ: string // Middleware type
settings: map[string]string // Configuration settings
}
```
### TLS Configuration
```v
TLSConfig {
domain: string // Domain name
cert_file: string // Certificate file path
key_file: string // Private key file path
}
```
## References
- [Traefik Redis Provider Documentation](https://doc.traefik.io/traefik/reference/install-configuration/providers/kv/redis/)
- [Traefik KV Dynamic Configuration](https://doc.traefik.io/traefik/reference/dynamic-configuration/kv/)

59
lib/osal/traefik/model.v Normal file
View File

@@ -0,0 +1,59 @@
module traefik
// Base configuration structs for Traefik components
@[params]
struct RouteConfig {
pub mut:
name string @[required] // Name of the router
rule string @[required] // Routing rule (e.g., "Host(`example.com`)")
service string @[required] // Name of the service to forward to
middlewares []string // List of middleware names to apply
priority int = 0 // Route priority
tls bool // Enable TLS for this router
}
@[params]
struct ServiceConfig {
pub mut:
name string @[required] // Name of the service
load_balancer LoadBalancerConfig @[required] // Load balancer configuration
}
@[params]
struct LoadBalancerConfig {
pub mut:
servers []ServerConfig @[required] // List of backend servers
}
@[params]
struct ServerConfig {
pub mut:
url string @[required] // URL of the backend server
}
@[params]
struct MiddlewareConfig {
pub mut:
name string @[required] // Name of the middleware
typ string @[required] // Type of middleware (e.g., "basicAuth", "stripPrefix")
settings map[string]string // Middleware-specific settings
}
@[params]
struct TLSConfig {
pub mut:
domain string @[required] // Domain for the certificate
cert_file string @[required] // Path to certificate file
key_file string @[required] // Path to private key file
}
// TraefikConfig represents a complete Traefik configuration
struct TraefikConfig {
pub mut:
routers []RouteConfig
services []ServiceConfig
middlewares []MiddlewareConfig
tls []TLSConfig
redis ?&redisclient.Redis
}

View File

@@ -0,0 +1,155 @@
module traefik
import json
import freeflowuniverse.herolib.core.redisclient
// new_traefik_config creates a new TraefikConfig
pub fn new_traefik_config() TraefikConfig {
return TraefikConfig{
routers: []RouteConfig{}
services: []ServiceConfig{}
middlewares: []MiddlewareConfig{}
tls: []TLSConfig{}
}
}
// add_route adds a route configuration
pub fn (mut tc TraefikConfig) add_route(args RouteConfig) {
tc.routers << RouteConfig{
name: args.name
rule: args.rule
service: args.service
middlewares: args.middlewares
priority: args.priority
tls: args.tls
}
}
// add_service adds a service configuration
pub fn (mut tc TraefikConfig) add_service(args ServiceConfig) {
tc.services << ServiceConfig{
name: args.name
load_balancer: args.load_balancer
}
}
// add_middleware adds a middleware configuration
pub fn (mut tc TraefikConfig) add_middleware(args MiddlewareConfig) {
tc.middlewares << MiddlewareConfig{
name: args.name
typ: args.typ
settings: args.settings
}
}
// add_tls adds a TLS configuration
pub fn (mut tc TraefikConfig) add_tls(args TLSConfig) {
tc.tls << TLSConfig{
domain: args.domain
cert_file: args.cert_file
key_file: args.key_file
}
}
// set populates Redis with the Traefik configuration
pub fn (tc TraefikConfig) set() ! {
mut redis := tc.redis or { redisclient.core_get()! }
// Store router configurations
for router in tc.routers {
base_key := 'traefik/http/routers/${router.name}'
// Set router rule
redis.set('${base_key}/rule', router.rule)!
// Set service
redis.set('${base_key}/service', router.service)!
// Set middlewares if any
if router.middlewares.len > 0 {
redis.set('${base_key}/middlewares', json.encode(router.middlewares))!
}
// Set priority if non-zero
if router.priority != 0 {
redis.set('${base_key}/priority', router.priority.str())!
}
// Set TLS if enabled
if router.tls {
redis.set('${base_key}/tls', 'true')!
}
}
// Store service configurations
for service in tc.services {
base_key := 'traefik/http/services/${service.name}'
// Set load balancer servers
mut servers := []map[string]string{}
for server in service.load_balancer.servers {
servers << {'url': server.url}
}
redis.set('${base_key}/loadbalancer/servers', json.encode(servers))!
}
// Store middleware configurations
for middleware in tc.middlewares {
base_key := 'traefik/http/middlewares/${middleware.name}'
// Set middleware type
redis.set('${base_key}/${middleware.typ}', json.encode(middleware.settings))!
}
// Store TLS configurations
for tls in tc.tls {
base_key := 'traefik/tls/certificates'
cert_config := {
'certFile': tls.cert_file
'keyFile': tls.key_file
}
redis.hset(base_key, tls.domain, json.encode(cert_config))!
}
}
// example shows how to use the Traefik configuration
pub fn (mut tc TraefikConfig) example() ! {
// Add a basic router with service
tc.add_route(
name: 'my-router'
rule: 'Host(`example.com`)'
service: 'my-service'
middlewares: ['auth']
tls: true
)
// Add the corresponding service
tc.add_service(
name: 'my-service'
load_balancer: LoadBalancerConfig{
servers: [
ServerConfig{url: 'http://localhost:8080'},
ServerConfig{url: 'http://localhost:8081'}
]
}
)
// Add a basic auth middleware
tc.add_middleware(
name: 'auth'
typ: 'basicAuth'
settings: {
'users': '["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"]'
}
)
// Add TLS configuration
tc.add_tls(
domain: 'example.com'
cert_file: '/path/to/cert.pem'
key_file: '/path/to/key.pem'
)
// Store configuration in Redis
tc.set()!
}