spond

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2025 License: MIT Imports: 6 Imported by: 0

README

Spond Logo

Spond

Go Reference

Description

  • It is a compact library for standardized communication between the server (in Go) and web clients via JSON responses.
  • Solves the problem of unified success/error structures for the API, custom response codes, minimalistic logging, and unified error handling.

Install
go get github.com/Aurivena/[email protected]

Opportunities

  • A single JSON response format (success and error)
  • Easy expansion of the list of status codes and messages
  • Integration with Gin (or any other web framework)
  • Built-in thread-safe logger (rotation, color output)
  • Minimum dependencies, pure Go-style

Usage example

Work with API-output
import "spond"

sp := spond.NewSpond()
// Success output
sp.SendResponseSuccess(c, spond.Success, map[string]string{"foo": "bar"})
// Error
sp.SendResponseError(c, sp.BuildError(spond.BadRequest, "Error", "incorect data"))
Logger (rotation, color, thread-safe)
import "spond/log"

logger := log.NewLog("log/io.log", 50*1024*1024) // 50 МБ
defer logger.Close()

logger.Info("Test INFO %s", "hello")
logger.Error("Some error: %v", err)

Extension

Append new code output
import "spond/log"

sp := spond.NewSpond()
err := sp.AppendCode(7777, "Мой статус")
if err != nil {
    panic(err)
}

Project Structure

spond/
  ├── log/          # Minimalistic logger (rotation, color, thread-safe)
  ├── response/     # Response structs, status codes, Success/Error types
  └── spond.go      # Core API handler logic

Testing

go test ./...

Unit tests are available for all main features.

Documentation

Overview

Package spond lives to facilitate communication between server and web via JSON structures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Spond

type Spond struct {
	// contains filtered or unexported fields
}

func NewSpond

func NewSpond() *Spond

For initialization struct Spond Usage example spond:=NewSpond() spond.SendResponseSuccess(c, spond.OK, nil)

func (*Spond) AppendCode

func (s *Spond) AppendCode(code response.StatusCode, message string) error

AppendCode adds a new status code and message to the statusMessages card. If the code already exists, returns the errorAppendCode error.

func (*Spond) BuildError

func (s *Spond) BuildError(code response.StatusCode, title, message string) response.ErrorResponse

BuildError forms an error structure for responding to the client. If the input parameters do not pass validation, it returns an error with the UnprocessableEntity code.

func (*Spond) SendResponseError

func (s *Spond) SendResponseError(c *gin.Context, rsp response.ErrorResponse)

SendResponseError sends the error to the client as JSON via gin.Context. rsp — structure with error details.

func (*Spond) SendResponseSuccess

func (s *Spond) SendResponseSuccess(c *gin.Context, status response.StatusCode, successResponse any)

SendResponseSuccess sends a successful JSON response via gin.Context. status is the response status, SuccessResponse is the payload for the client.

Directories

Path Synopsis
This package implemetns Logger with colored output in console.
This package implemetns Logger with colored output in console.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL