Categorygithub.com/newcore-network/go-stdlib
modulepackage
0.3.6
Repository: https://github.com/newcore-network/go-stdlib.git
Documentation: pkg.go.dev

# README

Newcore.gg Standard Libraries

Welcome to the Newcore Standard Library, a comprehensive suite of reusable components designed for seamless integration across all Newcore repositories and services. These libraries offer robust functionality, streamline development, and promote consistency throughout projects.

Test Coverage: 11.3%

Overview

The Newcore Standard Library includes key packages for common functionalities such as configuration management, database connections, structured logging, and standardized HTTP responses. Each package is designed to be modular and easy to integrate into different projects.

Docs

Explore the available utilities in docs/ folder

  • configuration: Manage and load application configuration from environment files.
  • database: Establish and manage database connections with support for PostgreSQL, MySQL, and MariaDB.
  • logger: A structured logging package with support for JSON formatting and log file rotation.
  • stdResponses: Simplifies the creation of standardized HTTP responses and error handling for APIs.

Getting Started

Prerequisites

Ensure you have the following in place before using the libraries:

  • Using Fiber version 3.x
  • Go version 1.23 or higher: Ensure you have the latest version of Go installed on your system.
  • .env file: Required for loading configuration details such as database credentials. Refer to the .env.example file in the repository for a template to get started.

Installation

Add the desired libraries to your project using Go modules:

$ go get github.com/newcore-network/go-stdlib

for all libraries

Simple configuration

To set up your application using the configuration library:

  • Create a .env file: This file should contain all necessary environment variables for your application, such as database host, user, password, etc.
  • Load configuration: Use the configuration package to load and manage your application settings.
#Configure the database connection
POSTGRES_HOST=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=admin
POSTGRES_DATABASE=altv
POSTGRES_PORT=5432
POSTGRES_SSLMODE=false

# Configure the redis connection

REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=admin
REDIS_DB=0

Usage in your Go application:

import (
    "github.com/newcore-network/go-stdlib"
)

func main() {
    config := stdlib.LoadCfg(".env")
    // Now you can use the `config` object in your application
    // -> Check the list of libraries to review their documentation and learn how to implement each one... <-
}

# Functions

CaptureError logs an error message with an additional error object.
CreateCacheRepository initializes a new instance of 'abstractCacheRepositoryImpl' with the given Redis client, context, and an self-reference.
CreateRepository initializes a new instance of `abstractRepositoryImpl` with the provided GORM database instance and a self-reference.
Debug logs a debug message, typically used for low-level system information.
ErrBadRequest logs a bad request error and returns a JSON response.
ErrConflict logs a conflict error and returns a JSON response.
ErrEmptyParametersOrArguments logs an error for missing parameters and returns a JSON response.
ErrForbbiden logs a forbidden error and returns a JSON response.
ErrInternalServer logs an internal server error and returns a JSON response.
ErrNotFound logs a not found error and returns a JSON response.
Error logs an error message, including context such as file and line number.
ErrUnauthorized logs an unauthorized error and returns a JSON response.
ErrUUIDParse logs a bad UUID error and returns a JSON response.
Fatal logs a fatal error message and exits the application.
Info logs an informational message.
No description provided by the author
LoadCfg loads the configuration from the specified file or defaults to ".env".
NewConnection establishes a database connection with retry logic and wraps it in a DBWrapper.
NewRedisConnection creates a new Redis client and pings the server to ensure connectivity.
No description provided by the author
PersonalizedErr returns an error with a custom message and status code.
RegisterValidatorErr logs validation errors and returns a JSON response.
No description provided by the author
No description provided by the author
Warn logs a warning message.

# Constants

use for disable status in the .env file.
use for enable status in the .env file.
use for error loading response.
use for port error.
use for get the DATABASE from the .env file.
use for get the HOST from the .env file.
use for get the PASSWORD from the .env file.
use for get the PORT from the .env file.
use for get the SSLMODE from the .env file.
use for get the USER from the .env file.
use for get the REDISDB from the .env file.
use for get the REDISHOST from the .env file.
use for get the REDISPORT from the .env file.

# Structs

CachePipeline is a wrapper around redis.Pipeliner that allows you to chain commands and add options (e.g a TTL) in a convenient way.
Conn represents a database connection encapsulated with Gorm.
DBWrapper is a wrapper around the Gorm database connection.
MariaDBConnection is a struct that implements the Connection interface for MariaDB.
PostgresConnection is a struct that implements the Connection interface for PostgreSQL.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

AbstractCacheRepository defines a generic interface for interacting with a Redis-based cache.
T is a generic type that represents a database entity.
Connection defines an interface for database drivers to implement.
ID is a generic type that represents the primary key of the entity, only accepting uint or uuid.UUID.
Identifiable is a generic interface that represents an entity that has an ID.
No description provided by the author