# BYOM Golang Library A production-ready Go library that provides pre-configured components for building robust web applications. This library simplifies the integration of common services and follows best practices for production environments. ## Features - **Web Server**: Pre-configured Gin server with middleware support - **Authentication**: JWT-based authentication - **Database**: SQLite integration with connection management - **Object Storage**: MinIO client for handling file storage - **Logging**: Structured logging using Logrus - **Error Handling**: Standardized error types and handling - **Configuration**: Centralized configuration management ## Installation ```bash go get git.linuxforward.com/byom/byom-golang-lib ``` ## Quick Start Here's a simple example that demonstrates how to use the main components: ```go package main import ( "time" "git.linuxforward.com/byom/byom-golang-lib/pkg/logger" "git.linuxforward.com/byom/byom-golang-lib/pkg/server" "git.linuxforward.com/byom/byom-golang-lib/pkg/database" ) func main() { // Initialize logger log, err := logger.NewLogger(logger.LogConfig{ Level: "info", Formatter: "text", }) if err != nil { panic(err) } // Initialize database db, err := database.NewSQLiteDB(database.SQLiteConfig{ Path: "app.db", }) if err != nil { log.Fatal(err) } defer db.Close() // Initialize server srv, err := server.NewGinServer(log, server.ServerConfig{ AllowedOrigins: []string{"http://localhost:3000"}, RequestTimeout: 30 * time.Second, }) if err != nil { log.Fatal(err) } // Add routes and start server srv.AddHealthCheck() if err := srv.Router().Run(":8080"); err != nil { log.Fatal(err) } } ``` ## Component Usage ### Logger ```go log, err := logger.NewLogger(logger.LogConfig{ Level: "info", // debug, info, warn, error Formatter: "text", // text or json }) ``` ### Database (SQLite) ```go db, err := database.NewSQLiteDB(database.SQLiteConfig{ Path: "app.db", }) defer db.Close() ``` ### MinIO Storage ```go minioClient, err := storage.NewMinioClient(storage.MinioConfig{ Endpoint: "localhost:9000", AccessKeyID: "minioadmin", SecretAccessKey: "minioadmin", UseSSL: false, BucketName: "mybucket", }) ``` ### JWT Authentication ```go token, err := auth.GenerateJWT(auth.Claims{ UserID: "123", Role: "admin", }) ``` ## Best Practices ### Error Handling Use the provided error types for consistent error handling: ```go if err != nil { return errors.NewDatabaseError("query", err) } ``` ### Configuration Use the config package for managing environment-specific configurations: ```go cfg, err := config.Load("config.yaml") ``` ### Middleware The server package includes common middleware for security and monitoring: - CORS configuration - Request timeout - Logging - Panic recovery - Request ID tracking ## Maintenance ### Versioning This library follows [Semantic Versioning](https://semver.org/). Version numbers follow the format: MAJOR.MINOR.PATCH. ### Contributing 1. Fork the repository 2. Create a feature branch (`git checkout -b feature/amazing-feature`) 3. Commit your changes (`git commit -m 'Add amazing feature'`) 4. Push to the branch (`git push origin feature/amazing-feature`) 5. Open a Pull Request ### Testing Run the test suite: ```bash go test ./... ``` For coverage report: ```bash go test ./... -coverprofile=coverage.out go tool cover -html=coverage.out ``` ### Logging Practices - Use appropriate log levels (DEBUG, INFO, WARN, ERROR) - Include relevant context in log messages - Use structured logging fields for machine-readable logs ### Error Handling Practices - Use appropriate error types from the errors package - Include context when wrapping errors - Log errors at the appropriate level - Don't expose internal errors to API clients ## License This project is licensed under the MIT License - see the LICENSE file for details.