Тайлбар байхгүй

lblt 0b022fddc5 feat: add request validation middleware and error handling 2 долоо хоног өмнө
examples 08532b9cd4 refactor: streamline error handling and configuration management across services 2 долоо хоног өмнө
pkg 0b022fddc5 feat: add request validation middleware and error handling 2 долоо хоног өмнө
.gitignore f3abf78520 initial commit 2 долоо хоног өмнө
README.md 7f0e35fb11 add SMTP error handling and email templates; update logger configuration 2 долоо хоног өмнө
go.mod 0b022fddc5 feat: add request validation middleware and error handling 2 долоо хоног өмнө
go.sum 0b022fddc5 feat: add request validation middleware and error handling 2 долоо хоног өмнө

README.md

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

go get git.linuxforward.com/byom/byom-golang-lib

Quick Start

Here's a simple example that demonstrates how to use the main components:

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

log, err := logger.NewLogger(logger.LogConfig{
    Level:     "info",    // debug, info, warn, error
    Formatter: "text",    // text or json
})

Database (SQLite)

db, err := database.NewSQLiteDB(database.SQLiteConfig{
    Path: "app.db",
})
defer db.Close()

MinIO Storage

minioClient, err := storage.NewMinioClient(storage.MinioConfig{
    Endpoint:        "localhost:9000",
    AccessKeyID:     "minioadmin",
    SecretAccessKey: "minioadmin",
    UseSSL:          false,
    BucketName:      "mybucket",
})

JWT Authentication

token, err := auth.GenerateJWT(auth.Claims{
    UserID: "123",
    Role:   "admin",
})

Best Practices

Error Handling

Use the provided error types for consistent error handling:

if err != nil {
    return errors.NewDatabaseError("query", err)
}

Configuration

Use the config package for managing environment-specific configurations:

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. 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:

go test ./...

For coverage report:

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.