Categorygithub.com/acoshift/arpc/v2
modulepackage
2.2.0
Repository: https://github.com/acoshift/arpc.git
Documentation: pkg.go.dev

# README

arpc

Build Status codecov Go Report Card GoDoc

ARPC is the Acoshift's opinionated HTTP-RPC styled api

Installation

go get -u github.com/acoshift/arpc/v2

HTTP Status Code

ARPC will response http with only these 3 status codes

  • 200 OK - function works as expected
  • 400 Bad Request - developer (api caller) error, should never happened in production
  • 500 Internal Server Error - server error, should never happened (server broken)

Example Responses

Success Result

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
	"ok": true,
	"result": {
		// result object
	}
}

Error Result

  • Validate error
  • Precondition failed
  • User error
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
	"ok": false,
	"error": {
		"message": "some error message"
	}
}

Function not found

  • Developer (api caller) call not exists function
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8

{
	"ok": false,
	"error": {
		"message": "not found"
	}
}

Unsupported Content-Type

  • Developer (api caller) send invalid content type
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8

{
	"ok": false,
	"error": {
		"message": "unsupported content type"
	}
}

Internal Server Error

  • Server broken !!!
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8

{
	"ok": false,
	"error": {} // internal error always return empty object
}

How to use

package main

import (
	"context"
	"log"
	"net/http"
	
	"github.com/acoshift/arpc/v2"
)

func main() { 
	// create new manager 
	am := arpc.New()

	mux := http.NewServeMux()
	mux.Handle("/hello", am.Handle(Hello))
	
	// start server 
	log.Fatal(http.ListenAndServe(":8080", mux))
}

type HelloParams struct {
	Name string `json:"name"`
}

func (r *HelloParams) Valid() error {
	if r.Name == "" {
		return arpc.NewError("name required")
	}
	return nil
}

type HelloResult struct {
	Message string `json:"message"`
}

func Hello(ctx context.Context, req *HelloParams) (*HelloResult, error) {
	return &HelloResult{
		Message: "hello " + req.Name,
	}, nil
}

License

MIT

# Functions

New creates new arpc manager.
NewError creates new Error with message.
NewErrorCode creates new Error with code and message.
No description provided by the author
WrapError wraps given error with OKError.

# Variables

predefined errors.
predefined errors.

# Structs

No description provided by the author
Error always return 200 status with false ok value use this error for validate, precondition failed, etc.
No description provided by the author
No description provided by the author
ProtocolError always returns 400 status with false ok value only use this error for invalid protocol usages.

# Interfaces

FormUnmarshaler interface.
MultipartFormUnmarshaler interface.
OKError implements this interface to mark errors as 200.
RequestAdapter converts request to arpc before decode.
RequestUnmarshaler interface.
No description provided by the author
Validatable interface.

# Type aliases

Decoder is the request decoder.
Encoder is the response encoder.
ErrorEncoder is the error response encoder.
No description provided by the author