simplejrpc

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 1 Imported by: 0

README

simplejrpc-go

simplejrpc-go is a lightweight JSON-RPC framework for Go, designed to help developers build high-performance and maintainable JSON-RPC services with essential built-in features.

Features

  • Configuration Management: Load and access nested configuration from JSON files using dot notation
  • Structured Logging: Powered by go.uber.org/zap with log rotation, stdout output, and customizable log levels
  • Data Validation: Comprehensive struct field validation with rules for required fields, length, numeric ranges, etc.
  • i18n Support: Internationalization resource loading (currently INI file format)
  • Network Communication: JSON-RPC server implementation with middleware support

Installation

go get github.com/DemonZack/simplejrpc-go@latest

Configuration Management

Example config.json:

{
    "test": {
        "version": "1.0.0",
        "jsonrpc": {
            "sockets": "rpc.sock"
        },
        "logger": {
            "path": "logs/",
            "file": "{Y-m-d}.log",
            "level": "error",
            "stdout": false,
            "StStatus": 0,
            "rotateBackupLimit": 7,
            "writerColorEnable": true,
            "RotateBackupCompress": 9,
            "rotateExpire": "1d",
            "Flag": 44
        }
    }
}

Usage example:

package main

import (
	"fmt"
	"path/filepath"
	"github.com/DemonZack/simplejrpc-go/core"
	"github.com/DemonZack/simplejrpc-go/core/config"
	"github.com/DemonZack/simplejrpc-go/os/gpath"
)

func main() {
	env := "test"
	fullPath, _ := filepath.Abs(filepath.Dir("."))
	gpath.GmCfgPath = filepath.Join(filepath.Dir(fullPath), "..")
	core.InitContainer(config.WithConfigEnvFormatterOptionFunc(env))

	val, err := core.Container.CfgFmt().GetValue("logger.level").String()
	if err != nil {
		panic(err)
	}
	fmt.Println("[*] logger.level:", val)
}

Logging

Example logging setup:

package main

import (
	"fmt"
	"time"
	"go.uber.org/zap"
	"github.com/DemonZack/simplejrpc-go/core/glog"
)

func main() {
	config := map[string]any{
		"path":                 "logs/",
		"file":                 "{Y-m-d}.log",
		"level":                "error",
		"stdout":               false,
		"rotateBackupLimit":    7,
		"writerColorEnable":    true,
		"RotateBackupCompress": 9,
		"rotateExpire":         "1d"
	}

	logger, err := glog.NewLogger(config)
	if err != nil {
		panic(fmt.Sprintf("init logger failed: %v", err))
	}
	defer logger.Sync()

	logger.Info("Logger initialized",
		zap.String("path", config["path"].(string)),
		zap.Int("backupLimit", config["rotateBackupLimit"].(int)),
	)
}

Data Validation

Validation example:

package main

import (
	"fmt"
	"github.com/DemonZack/simplejrpc-go/core"
)

type User struct {
	Username string `validate:"min_length:6#Username must be at least 6 characters"`
	Age      any    `validate:"required|range:18,100|int"`
	Email    string `validate:"required"`
}

func main() {
	user := User{
		Username: "test",
		Age:      15,
	}

	err := core.Container.Valid().Walk(&user)
	if err != nil {
		fmt.Println("Validation error:", err)
	}
}

Internationalization (i18n)

i18n example:

package main

import (
	"path/filepath"
	"github.com/DemonZack/simplejrpc-go/core/gi18n"
)

func main() {
	path := filepath.Join("testdata", "i18n")
	gi18n.Instance().SetPath(path)
	gi18n.Instance().SetLanguage(gi18n.English)
	
	println(gi18n.Instance().T("Welcome"))
}

JSON-RPC Server

Server implementation:

package main

import (
	"github.com/DemonZack/simplejrpc-go/net/gsock"
	rpc "github.com/DemonZack/simplejrpc-go"
)

type Handler struct{}

func (h *Handler) Hello(req *gsock.Request) (any, error) {
	return "Hello World", nil
}

func main() {
	srv := rpc.NewDefaultServer(
		gsock.WithJsonRpcSimpleServiceHandler(gsock.NewJsonRpcSimpleServiceHandler()),
	)
	
	srv.RegisterHandle("hello", (&Handler{}).Hello)
	err := srv.StartServer("rpc.sock")
	if err != nil {
		panic(err)
	}
}

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a pull request

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

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

Server represents a JSON-RPC server with middleware support. It wraps the underlying IRpcServer implementation and provides additional functionality.

func NewDefaultServer

func NewDefaultServer(opts ...gsock.JsonRpcSimpleServiceOptionFunc) *Server

NewDefaultServer creates a new Server instance with default configuration. It accepts optional configuration functions that can modify the behavior of the JSON-RPC service. opts: Optional configuration functions for the JSON-RPC service Returns a pointer to the newly created Server instance

func (*Server) Middlewares

func (s *Server) Middlewares() []gsock.RPCMiddleware

Middlewares returns the server's global middlewares. Returns: A slice of RPCMiddleware currently registered as global middlewares

func (*Server) RegisterHandle

func (s *Server) RegisterHandle(api string, hand func(req *gsock.Request) (any, error), middlewares ...gsock.RPCMiddleware)

RegisterHandle registers a new handler function for a specific API endpoint. The handler will be wrapped with any provided middlewares, which will be executed in addition to the server's global middlewares. api: The API endpoint path (e.g., "/api/v1/method") hand: The handler function that processes the request and returns a response or error middlewares: Optional middlewares that apply only to this specific handler

func (*Server) Server

func (s *Server) Server() gsock.IRpcServer

Server returns the underlying IRpcServer implementation. This provides access to the core server functionality if needed. Returns: The underlying IRpcServer instance

func (*Server) StartServer

func (s *Server) StartServer(socketPath string) error

StartServer starts the JSON-RPC server listening on the specified Unix domain socket path. socketPath: The filesystem path where the Unix domain socket will be created Returns: An error if the server fails to start, nil otherwise

Directories

Path Synopsis
boxs
container
example
app command
config command
i18n command
logger command
server command
validate command
net
os

Jump to

Keyboard shortcuts

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