cmr

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: MIT Imports: 3 Imported by: 0

README

API CMR

Use API CMR (Code + Message + Result) Specification, like jSend.

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/api-cmr

Getting Started

package main

import (
  "github.com/go-zoox/api-cmr"
)

func main(t *testing.T) {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write(cmr.Success("Hello World!"))
		// or
		cmr.WriteSuccess(w, "Hello World!")
	})

	http.ListenAndServe(":8080", nil)
}

Inspired By

  • omniti-labs/jsend - JSend is a specification for a simple, no-frills, JSON based format for application-level communication

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Version = "1.0.1"

Version is the current version of the package.

Functions

func Register

func Register(code string, message string)

Register registers a pair of code and message

func WriteFail

func WriteFail(w http.ResponseWriter, code string, message string, result interface{})

WriteFail writes a fail response to the response writer.

func WriteSuccess

func WriteSuccess(w http.ResponseWriter, result interface{}, codemessage ...string)

WriteSuccess writes a success response to the response writer.

Types

type Body

type Body struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Result  any    `json:"result"`
}

Body is the response body

func Fail

func Fail[T any](code string, result T) *Body

Fail return a fail body, bad request causes calculation error

func Success

func Success[T any](result T, codemessage ...string) *Body

Success return a succes body

Jump to

Keyboard shortcuts

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