calculator

package module
v0.0.0-...-0895859 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 5 Imported by: 0

README

go-calculator: A sample Shana microservice

This is a sample microservice that implements a simple scientific calculator. It is intended to be used as a reference for building Shana microservices.

Some best practices are followed, such as:

  • Use of internal package.
    • Move the config.go to an internal package to make it secret. In general, service config is private and should not be exposed to the outside world.
    • Implement all logic in an internal package. All public packages can be considered as "controllers" in the MVC sense. They should be as thin as possible and should not contain any business logic.
  • Implement Validate and/or Init method to validate the data structure.
    • It's recommended to do validation in a Validate(context.Context) method and initialization in a Init(context.Context). If a type implements any of these methods, Shana framework will call it automatically before calling the handler or finishing loading config.
    • In a validate method, we can use github.com/go-shana/core/validator and its subpackages to validate the request. We can also simply throw errors if anything goes wrong.
    • In a initialize method, we can set default values for the request. Again, if anything goes wrong, throw an error to let Shana knows.
  • Return error code.
    • By default, Shana RPC returns error string to the client if there is any error. However, it's recommended to return error code instead of error string. This is because error string is not stable and may change in the future. Error code is stable and can be used to identify the error type.
    • Optionally, we can return an OK error with 0 code to indicate that the request is successful.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorCode

type ErrorCode int
const (
	ErrorCodeOK         ErrorCode = 0    // OK.
	ErrorCodeDivideZero ErrorCode = 1000 // Divide by zero.
)

type Request

type Request struct {
	Value string `json:"value"`
	// contains filtered or unexported fields
}

func (*Request) Validate

func (req *Request) Validate(ctx context.Context)

Validate makes sure the value is a valid integer.

type Response

type Response struct {
	Value string `json:"value"`
}

func Add

func Add(ctx context.Context, req *Request) (resp *Response, err error)

func Div

func Div(ctx context.Context, req *Request) (resp *Response, err error)

func Mul

func Mul(ctx context.Context, req *Request) (resp *Response, err error)

func Sub

func Sub(ctx context.Context, req *Request) (resp *Response, err error)

func Value

func Value(ctx context.Context, req *struct{}) (resp *Response, err error)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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