rvr

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

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

Go to latest
Published: Apr 8, 2022 License: MIT Imports: 10 Imported by: 0

README

RVR = Request, Validate, Response

  • Request: value type conversion, easy access to context
  • Validator: extendable custom validators
  • Response: response with custom templates

Example

package main

import (
	"log"
	"net/http"

	"code.ernlel.com/Ernest/az"
	"code.ernlel.com/Ernest/rvr"
)

func main() {
	r := az.New()

	r.Route("/ping", r.Handler(
		func(w http.ResponseWriter, req *http.Request) {

			RVR := rvr.New(true)

			request, v, response := RVR.Init(req, w)

			a := request.GetQuery("a").String()
			b := 3

			v.Not(b == 2).And(v.V.CompareString(a, "=", "hello")).AddError("error")

			if response.ValidationError(v) {
				return
			}

			log.Println(request.Query)

			response.WriteDebug(a)
			response.Header("Content-Type", "application/json")
			response.WriteJson(map[string]string{"ping": "pong"}, true)

		}),
	)

	log.Fatal(http.ListenAndServe(":5000", r))

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBody

func GetBody(r *http.Request) []byte

Types

type Params

type Params map[string]interface{}

func GetCookies

func GetCookies(r *http.Request) Params

func GetFiles

func GetFiles(r *http.Request) Params

func GetForm

func GetForm(r *http.Request) Params

func GetHeaders

func GetHeaders(r *http.Request) Params

func GetQuery

func GetQuery(r *http.Request) Params

type Req

type Req struct {
	Query   Params
	Form    Params
	Files   Params
	Header  Params
	Cookies Params
	Json    map[string]interface{}
	Body    []byte
	// contains filtered or unexported fields
}

func (*Req) GetCookies

func (r *Req) GetCookies(name string) value.Value

func (*Req) GetFiles

func (r *Req) GetFiles(name string) value.Value

func (*Req) GetForm

func (r *Req) GetForm(name string) value.Value

func (*Req) GetHeader

func (r *Req) GetHeader(name string) value.Value

func (*Req) GetJson

func (r *Req) GetJson(path string) value.Value

func (*Req) GetQuery

func (r *Req) GetQuery(name string) value.Value

func (*Req) LoadJsonBody

func (r *Req) LoadJsonBody() *Req

type Res

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

func (*Res) Code

func (r *Res) Code(code int) *Res

func (*Res) Error

func (r *Res) Error(err error, message string, ErrorCode ...int) bool

func (*Res) Header

func (r *Res) Header(key string, value string) *Res

func (*Res) ValidationError

func (r *Res) ValidationError(v *Valid) bool

func (*Res) Write

func (r *Res) Write(body []byte)

func (*Res) WriteDebug

func (r *Res) WriteDebug(body interface{})

func (*Res) WriteJson

func (r *Res) WriteJson(body interface{}, indent bool)

func (*Res) WriteTemplate

func (r *Res) WriteTemplate(name string, context ...interface{})

type Rvr

type Rvr struct {
	Debug bool

	ResponseTemplates func(string, ...interface{}) []byte
	// contains filtered or unexported fields
}

func New

func New(debug bool) *Rvr

func (*Rvr) AddResponseHeader

func (rvr *Rvr) AddResponseHeader(key string, value string)

func (*Rvr) Init

func (rvr *Rvr) Init(r *http.Request, w http.ResponseWriter) (*Req, *Valid, *Res)

func (*Rvr) Request

func (rvr *Rvr) Request(r *http.Request) *Req

func (*Rvr) Response

func (rvr *Rvr) Response(w http.ResponseWriter) *Res

func (*Rvr) Validator

func (rvr *Rvr) Validator() *Valid

type Valid

type Valid struct {
	V defaultValidators
	// contains filtered or unexported fields
}

func (*Valid) AddError

func (v *Valid) AddError(message ...string) *Valid

func (*Valid) And

func (v *Valid) And(c bool) *Valid

func (*Valid) AndNot

func (v *Valid) AndNot(c bool) *Valid

func (*Valid) GetErrors

func (v *Valid) GetErrors() [][]string

func (*Valid) If

func (v *Valid) If(c bool) *Valid

func (*Valid) Not

func (v *Valid) Not(c bool) *Valid

func (*Valid) Or

func (v *Valid) Or(c bool) *Valid

func (*Valid) OrNot

func (v *Valid) OrNot(c bool) *Valid

func (*Valid) Pass

func (v *Valid) Pass() bool

func (*Valid) Valid

func (v *Valid) Valid() bool

Jump to

Keyboard shortcuts

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