znet

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2019 License: MIT Imports: 23 Imported by: 21

Documentation

Overview

Package zweb provides web service

package main

import (
	"github.com/sohaha/zlsgo/znet"
)

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

	r.SetMode(znet.DebugMode)
	r.GET("/", func(c znet.Context) {
		c.String(200, "hello world")
	})

	znet.Run()
}

Index

Constants

View Source
const (

	// DebugMode debug
	DebugMode = "debug"
	// ReleaseMode release
	ReleaseMode = "release"
	// TestMode test
	TestMode = "test"
)

Variables

View Source
var (
	// ErrGenerateParameters is returned when generating a route withRequestLog wrong parameters.
	ErrGenerateParameters = errors.New("params contains wrong parameters")

	// ErrNotFoundRoute is returned when generating a route that can not find route in tree.
	ErrNotFoundRoute = errors.New("cannot find route in tree")

	// ErrNotFoundMethod is returned when generating a route that can not find method in tree.
	ErrNotFoundMethod = errors.New("cannot find method in tree")

	// ErrPatternGrammar is returned when generating a route that pattern grammar error.
	ErrPatternGrammar = errors.New("pattern grammar error")
)
View Source
var (
	// Log Log
	Log = zlog.New(zlog.ColorTextWrap(zlog.ColorGreen, "[Z] "))
)

Functions

func ClientIP

func ClientIP(r *http.Request) (ip string)

func ClientPublicIP

func ClientPublicIP(r *http.Request) string

func HasLocalIP

func HasLocalIP(ip net.IP) bool

func HasLocalIPddr

func HasLocalIPddr(ip string) bool

func IP2Long

func IP2Long(ip net.IP) (i uint, err error)

func IPString2Long

func IPString2Long(ip string) (i uint, err error)

func Long2IP

func Long2IP(i uint) (ip net.IP, err error)

func Long2IPString

func Long2IPString(i uint) (IP string, err error)

func RemoteIP

func RemoteIP(r *http.Request) string

func Run

func Run()

Run Run

Types

type Context

type Context struct {
	Writer  http.ResponseWriter
	Request *http.Request
	Code    int
	Engine  *Engine
	Info    *info
	Log     *zlog.Logger
	// contains filtered or unexported fields
}

Context context

func (*Context) Abort

func (c *Context) Abort(code ...int)

Abort Abort

func (*Context) ClientIP

func (c *Context) ClientIP() (IP string)

ClientIP Client IP

func (c *Context) CompletionLink(link string) string

CompletionLink Complete the link and add the current domain name if it is not linked

func (*Context) DefaultPostForm

func (c *Context) DefaultPostForm(key, defaultValue string) string

func (*Context) DefaultQuery

func (c *Context) DefaultQuery(key string, defaultValue string) string

func (*Context) File

func (c *Context) File(filepath string)

func (*Context) FormFile

func (c *Context) FormFile(name string) (*multipart.FileHeader, error)

func (*Context) GetAllParam

func (c *Context) GetAllParam() ParamsMapType

GetAllParam Get the value of all param in the route

func (*Context) GetAllQueryst

func (c *Context) GetAllQueryst() url.Values

func (*Context) GetCookie

func (c *Context) GetCookie(name string) string

GetCookie Get Cookie

func (*Context) GetHeader

func (c *Context) GetHeader(key string) string

GetHeader Get Header

func (*Context) GetParam

func (c *Context) GetParam(key string) string

GetParam Get the value of the param inside the route

func (*Context) GetPostForm

func (c *Context) GetPostForm(key string) (string, bool)

func (*Context) GetPostFormArray

func (c *Context) GetPostFormArray(key string) ([]string, bool)

func (*Context) GetPostFormMap

func (c *Context) GetPostFormMap(key string) (map[string]string, bool)

func (*Context) GetQuery

func (c *Context) GetQuery(key string) (string, bool)

func (*Context) GetQueryArray

func (c *Context) GetQueryArray(key string) ([]string, bool)

func (*Context) HTML

func (c *Context) HTML(code int, html string)

func (*Context) Host

func (c *Context) Host() string

Host Get the current Host

func (*Context) IsWebsocket

func (c *Context) IsWebsocket() bool

IsWebsocket Is Websocket

func (*Context) JSON

func (c *Context) JSON(code int, values interface{})

func (*Context) MultipartForm

func (c *Context) MultipartForm() (*multipart.Form, error)

func (*Context) Next

func (c *Context) Next() (next HandlerFunc)

Next Execute the next Handler Func

func (*Context) PostFormMap

func (c *Context) PostFormMap(key string) map[string]string

func (*Context) Redirect

func (c *Context) Redirect(link string, statusCode ...int)

Redirect Redirect

func (*Context) Referer

func (c *Context) Referer() string

Referer request referer.

func (*Context) SaveUploadedFile

func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dist string) error

func (*Context) SetCookie

func (c *Context) SetCookie(name, value string)

SetCookie Set Cookie

func (*Context) SetHeader

func (c *Context) SetHeader(key, value string)

SetHeader Set Header

func (*Context) StatusCode

func (c *Context) StatusCode(statusCode int)

StatusCode StatusCode

func (*Context) String

func (c *Context) String(code int, format string, values ...interface{})

func (*Context) Template

func (c *Context) Template(code int, name string, data ...interface{})

func (*Context) UserAgent

func (c *Context) UserAgent() string

UserAgent http request UserAgent

type Engine

type Engine struct {
	// Log Log
	Log *zlog.Logger

	ShowFavicon        bool
	MaxMultipartMemory int64
	// contains filtered or unexported fields
}

Engine is a simple HTTP route multiplexer that parses a request path

func New

func New(serverName ...string) *Engine

New returns a newly initialized Engine object that implements the Engine

func Server

func Server(serverName ...string) (engine *Engine, ok bool)

Server Server

func (*Engine) Any

func (e *Engine) Any(path string, handle HandlerFunc)

func (*Engine) DELETE

func (e *Engine) DELETE(path string, handle HandlerFunc, moreHandler ...HandlerFunc)

func (*Engine) DELETEAndName

func (e *Engine) DELETEAndName(path string, handle HandlerFunc, routeName string)

func (*Engine) GET

func (e *Engine) GET(path string, handle HandlerFunc, moreHandler ...HandlerFunc)

func (*Engine) GETAndName

func (e *Engine) GETAndName(path string, handle HandlerFunc, routeName string)

func (*Engine) GenerateURL

func (e *Engine) GenerateURL(method string, routeName string, params map[string]string) (string, error)

func (*Engine) GetTrees

func (e *Engine) GetTrees() map[string]*Tree

GetTrees Get Trees

func (*Engine) Group

func (e *Engine) Group(prefix string, groupHandle ...func(e *Engine)) (engine *Engine)

func (*Engine) HEAD

func (e *Engine) HEAD(path string, handle HandlerFunc, moreHandler ...HandlerFunc)

func (*Engine) HEADAndName

func (e *Engine) HEADAndName(path string, handle HandlerFunc, routeName string)

func (*Engine) Handle

func (e *Engine) Handle(method string, path string, handle HandlerFunc, moreHandler ...HandlerFunc)

Handle registers new request handler

func (*Engine) HandleNotFound

func (e *Engine) HandleNotFound(c *Context, middleware []HandlerFunc)

func (*Engine) Match

func (e *Engine) Match(requestURL string, path string) bool

Match checks if the request matches the route pattern

func (*Engine) NotFoundFunc

func (e *Engine) NotFoundFunc(handler HandlerFunc)

func (*Engine) OPTIONS

func (e *Engine) OPTIONS(path string, handle HandlerFunc, moreHandler ...HandlerFunc)

func (*Engine) OPTIONSAndName

func (e *Engine) OPTIONSAndName(path string, handle HandlerFunc, routeName string)

func (*Engine) PATCH

func (e *Engine) PATCH(path string, handle HandlerFunc, moreHandler ...HandlerFunc)

func (*Engine) PATCHAndName

func (e *Engine) PATCHAndName(path string, handle HandlerFunc, routeName string)

func (*Engine) POST

func (e *Engine) POST(path string, handle HandlerFunc, moreHandler ...HandlerFunc)

func (*Engine) POSTAndName

func (e *Engine) POSTAndName(path string, handle HandlerFunc, routeName string)

func (*Engine) PUT

func (e *Engine) PUT(path string, handle HandlerFunc, moreHandler ...HandlerFunc)

func (*Engine) PUTAndName

func (e *Engine) PUTAndName(path string, handle HandlerFunc, routeName string)

func (*Engine) PanicHandler

func (e *Engine) PanicHandler(handler PanicFunc)

func (*Engine) ServeHTTP

func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Engine) SetAddr

func (e *Engine) SetAddr(addr ...string)

SetAddr SetAddr

func (*Engine) SetCustomMethodField

func (e *Engine) SetCustomMethodField(field string)

SetCustomMethodField SetCustomMethodField

func (*Engine) SetMode

func (e *Engine) SetMode(value string)

SetMode Setting Server Mode

func (*Engine) SetTimeLocation

func (e *Engine) SetTimeLocation(location string)

SetTimeLocation timezone

func (*Engine) SetTimeout

func (e *Engine) SetTimeout(Timeout time.Duration, WriteTimeout ...time.Duration)

SetTimeout setTimeout

func (*Engine) Static

func (e *Engine) Static(relativePath, root string)

func (*Engine) StaticFS

func (e *Engine) StaticFS(relativePath string, fs http.FileSystem)

func (*Engine) StaticFile

func (e *Engine) StaticFile(relativePath, filepath string)

func (*Engine) Use

func (e *Engine) Use(middleware ...HandlerFunc)

type H

type H map[string]interface{}

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc HandlerFunc

type J

type J struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type MiddlewareFunc

type MiddlewareFunc func(*Context, HandlerFunc)

MiddlewareFunc MiddlewareFunc

type MiddlewareType

type MiddlewareType HandlerFunc

MiddlewareType is a public type that is used for middleware

type Node

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

Node records any URL params, and executes an end handler.

func NewNode

func NewNode(key string, depth int) *Node

type PanicFunc

type PanicFunc func(c *Context, err error)

PanicFunc PanicFunc

type Parameters

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

Parameters records some parameters

type ParamsMapType

type ParamsMapType map[string]string

ParamsMapType Storage path parameters

type Tree

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

Tree records node

func NewTree

func NewTree() *Tree

func (*Tree) Add

func (t *Tree) Add(pattern string, handle HandlerFunc, middleware ...HandlerFunc)

func (*Tree) Find

func (t *Tree) Find(pattern string, isRegex bool) (nodes []*Node)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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