fetch_ml/examples/auth_integration_example.go
Jeremie Fraeys 803677be57 feat: implement Go backend with comprehensive API and internal packages
- Add API server with WebSocket support and REST endpoints
- Implement authentication system with API keys and permissions
- Add task queue system with Redis backend and error handling
- Include storage layer with database migrations and schemas
- Add comprehensive logging, metrics, and telemetry
- Implement security middleware and network utilities
- Add experiment management and container orchestration
- Include configuration management with smart defaults
2025-12-04 16:53:53 -05:00

78 lines
1.5 KiB
Go

package main
import (
"fmt"
"log"
"os"
"github.com/jfraeys/fetch_ml/internal/auth"
"gopkg.in/yaml.v3"
)
// Example: How to integrate auth into TUI startup
func checkAuth(configFile string) error {
// Load config
data, err := os.ReadFile(configFile)
if err != nil {
return fmt.Errorf("failed to read config: %w", err)
}
var cfg struct {
Auth auth.AuthConfig `yaml:"auth"`
}
if err := yaml.Unmarshal(data, &cfg); err != nil {
return fmt.Errorf("failed to parse config: %w", err)
}
// If auth disabled, proceed normally
if !cfg.Auth.Enabled {
fmt.Println("🔓 Authentication disabled - proceeding normally")
return nil
}
// Check for API key
apiKey := os.Getenv("FETCH_ML_API_KEY")
if apiKey == "" {
apiKey = getAPIKeyFromUser()
}
// Validate API key
user, err := cfg.Auth.ValidateAPIKey(apiKey)
if err != nil {
return fmt.Errorf("authentication failed: %w", err)
}
fmt.Printf("🔐 Authenticated as: %s", user.Name)
if user.Admin {
fmt.Println(" (admin)")
} else {
fmt.Println()
}
return nil
}
func getAPIKeyFromUser() string {
fmt.Print("🔑 Enter API key: ")
var key string
fmt.Scanln(&key)
return key
}
// Example usage in main()
func exampleMain() {
configFile := "config_dev.yaml"
// Check authentication first
if err := checkAuth(configFile); err != nil {
log.Fatalf("Authentication failed: %v", err)
}
// Proceed with normal TUI initialization
fmt.Println("Starting TUI...")
}
func main() {
exampleMain()
}