xweb

package
v0.0.0-...-2dced25 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2019 License: MIT Imports: 11 Imported by: 0

README

XWeb

参考了docker的实现流程,docker提供了一个非常棒的范式。

Example

Example

package main

import (
	"net/http"

	"flag"

	"github.com/hebl/gox/xweb"
	"golang.org/x/net/context"
)

//MyRouter simple router
type MyRouter struct {
	routes []xweb.Route
}

//Routes simple interface
func (mr *MyRouter) Routes() []xweb.Route {
	return mr.routes
}

//NewMyRouter new router
func NewMyRouter() xweb.Router {
	r := &MyRouter{}

	r.initRoutes()

	return r
}

func (mr *MyRouter) initRoutes() {
	mr.routes = []xweb.Route{
		//GET
		web.NewGetRoute("/get", mr.getHandler),
		//...
	}
}

//getHandler simple get handler
func (mr *MyRouter) getHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
	w.Write([]byte("get"))

	return nil
}

func main() {
	configFile := flag.String("c", "config.json", "JSON Config file")
	flag.Parse()

	router := NewMyRouter()
	server := xweb.New(*configFile)
	server.Init(router)
	server.StartServer()
}

run

go run main.go -c config.json

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckForJSON

func CheckForJSON(r *http.Request) error

CheckForJSON makes sure that the request's Content-Type is application/json.

func FileHandler

func FileHandler(w http.ResponseWriter, req *http.Request)

FileHandler 静态文件

func MatchesContentType

func MatchesContentType(contentType, expectedType string) bool

MatchesContentType validates the content type against the expected one

func NewPg

func NewPg(dbconfig DBConfig) *pg.DB

NewPg 新建一个PostgreSQL连接

func ParseForm

func ParseForm(r *http.Request) error

ParseForm ensures the request form is parsed even with invalid content types. If we don't do this, POST method without Content-type (even with empty body) will fail.

func ParseMultipartForm

func ParseMultipartForm(r *http.Request) error

ParseMultipartForm ensure the request form is parsed, even with invalid content types.

func WriteError

func WriteError(w http.ResponseWriter, err error)

WriteError decodes a specific docker error and sends it in the response.

func WriteJSON

func WriteJSON(w http.ResponseWriter, code int, v interface{}) error

WriteJSON writes the value v to the http response stream as json with standard json encoding.

Types

type CommonConfig

type CommonConfig struct {
	Name         string    `json:"name"`
	Version      string    `json:"version"`
	Host         string    `json:"host"`
	Port         int       `json:"port"`
	Logging      bool      `json:"logging"`
	LogLevel     log.Level `json:"log_level"`
	LogFile      string    `json:"log_file"`
	CookieSecret string    `json:"cookie_secret"`
	Static       []struct {
		URI        string `json:"uri"`
		Filesystem string `json:"filesystem"`
	} `json:"static"`
	CertFile string `json:"cert_file"`
	KeyFile  string `json:"key_file"`
}

CommonConfig 基础配置信息

type Config

type Config struct {
	CommonConfig
	Database DBConfig `json:"database"`
}

Config 系统配置信息 Load Config : utils.ParseJSONFile(filename, &Config)

func NewConfig

func NewConfig(configFile string) *Config

NewConfig 根据文件生成一个配置信息

type Context

type Context struct {
	Request *http.Request
	Writer  http.ResponseWriter

	Params Params

	Errors   errorMsgs
	Accepted []string
	// contains filtered or unexported fields
}

Context 上下文信息

type DBConfig

type DBConfig struct {
	Database string `json:"database"`
	Host     string `json:"host"`
	Port     string `json:"port"`
	User     string `json:"user"`
	Password string `json:"password"`
	PoolSize int    `json:"poolsize"`
}

DBConfig 数据库配置

type Error

type Error struct {
	Err  error
	Type ErrorType
	Meta interface{}
}

Error Error info

func (*Error) Error

func (msg *Error) Error() string

Implements the error interface

func (*Error) SetMeta

func (msg *Error) SetMeta(data interface{}) *Error

SetMeta SetMeta

func (*Error) SetType

func (msg *Error) SetType(flags ErrorType) *Error

SetType SetType

type ErrorType

type ErrorType uint64

ErrorType Error Type

type FilterHanderFunc

type FilterHanderFunc func(permission uint, handler HTTPAPIFunc) HTTPAPIFunc

FilterHanderFunc 权限过滤中间件

type HTTPAPIFunc

type HTTPAPIFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error

HTTPAPIFunc 函数类型

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc HandlerFunc

type HandlersChain

type HandlersChain []HandlerFunc

HandlersChain HandlersChain

type Middleware

type Middleware func(handler HTTPAPIFunc) HTTPAPIFunc

Middleware 中间件函数

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

type Route

type Route interface {
	Method() string
	Path() string
	Handler() HTTPAPIFunc
}

Route 路由表

func NewDeleteRoute

func NewDeleteRoute(path string, handler HTTPAPIFunc) Route

NewDeleteRoute initializes a new route with the http method DELETE.

func NewGetRoute

func NewGetRoute(path string, handler HTTPAPIFunc) Route

NewGetRoute initializes a new route with the http method GET.

func NewHeadRoute

func NewHeadRoute(path string, handler HTTPAPIFunc) Route

NewHeadRoute initializes a new route with the http method HEAD.

func NewOptionsRoute

func NewOptionsRoute(path string, handler HTTPAPIFunc) Route

NewOptionsRoute initializes a new route with the http method OPTIONS

func NewPostRoute

func NewPostRoute(path string, handler HTTPAPIFunc) Route

NewPostRoute initializes a new route with the http method POST.

func NewPutRoute

func NewPutRoute(path string, handler HTTPAPIFunc) Route

NewPutRoute initializes a new route with the http method PUT.

func NewRoute

func NewRoute(method, path string, handler HTTPAPIFunc) Route

NewRoute initializes a new local router for the reouter

type Router

type Router interface {
	Routes() []Route
}

Router router

type Server

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

Server Web程序

func NewServer

func NewServer(config *Config, router Router) *Server

NewServer 新建一个Web实例

func (*Server) CreateMux

func (s *Server) CreateMux() *mux.Router

CreateMux 根据router生成http mux

func (*Server) Init

func (s *Server) Init(router Router)

Init 初始化 router

func (*Server) StartServer

func (s *Server) StartServer()

StartServer 启动服务

func (*Server) StartServerTLS

func (s *Server) StartServerTLS()

StartServerTLS 启动https服务

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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