goway

package module
v0.0.0-...-7b8a158 Latest Latest
Warning

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

Go to latest
Published: May 22, 2017 License: LGPL-3.0 Imports: 14 Imported by: 1

README

Go Way In Go

wercker status Build Status

Goway:

Martini is a powerful package for quickly writing modular web applications/services in Golang.

Goway it's an web framework,The martini framework code to do some optimization.

Usage
    import (
        "github.com/wackonline/goway"
    )
Within the main function is to write like this:
    func main() {
        gm := goway.Bootstrap()

        gm.Get("/", func() string {
            return "hello,world"
        })

        gm.Run()
        //gm.RunOnAddr("0.0.0.0:8080")
    }

Config

In Goway, is configure web app config file.It mainly to inform how the app works,it's an JSON data struct.

    {
      // App version
      "version":"0.0.1",
      // Application debugging information
      // false and true
      "debug":true,
      // Logging
      // E_ALL|E_ERROR|E_WARNING|E_STRICT|E_NOTICE
      "logger":"E_ALL",
      // App run environment
      // development|testing|product
      "env":"development",
      // Setting static directory path
      // Directory to the current app running directory to the root directory
      "staticPath": "/public",
      // HTTP Server ip address
      "httpServer":"0.0.0.0",
      // HTTP Server port
      "serverPort":"8080"
    }
Routing

In Goway, a route is an HTTP method paired with a URL-matching pattern. Each route can take one or more handler methods:

    gm.Get("/", func() {
      // show something
    })

    gm.Patch("/", func() {
      // update something
    })

    gm.Post("/", func() {
      // create something
    })

    gm.Put("/", func() {
      // replace something
    })

    gm.Delete("/", func() {
      // destroy something
    })

    gm.Options("/", func() {
      // http options
    })

    // You can also create routes for static files
    pwd, _ := os.Getwd()
    gm.Static("/public", pwd)

Logger

In Goway, a logger is an HTTP request after call debug infomation

    // Tlogs is map[int]string data struct
    var Logs = []Tlogs
    var log = Goway.Tlogs{}
    // E_ERROR | E_WARNING | E_NOTICE | E_STRICT
    log[E_ERROR] = "the is error!"
    Logs = append(Logs,log)
    gm.Logs.Use(Logs)
    //OR
    gm.Logs.Error("the is error!")
    gm.Logs.Notice("the is notice!")
    gm.Logs.Warning("the is warning!")
    gm.Logs.Strict("the is strict!")
    //use params
    sid := 110
    gm.Logs.Error("the is error! Sessionid: %d", sid)
    gm.Logs.Notice("the is notice! Sessionid: %d", sid)
    gm.Logs.Warning("the is warning! Sessionid: %d", sid)
    gm.Logs.Strict("the is strict! Sessionid: %d", sid)
Other example
About Goway

Inspired by Martini(Go). This framework is simple enough, and the use of modular programming, this is a way I like it very much. Subsequent functional may not continue like as Martini,Goway will learn other good characteristics of the web framework.

License

Go Way is released under the GPLV3 license: License

Documentation

Overview

-*- indent-tabs-mode:nil; coding: utf-8 -*- * Copyleft (C) 2015 * "Tag bao" known as "wackonline" <bb.qnyd@gmail.com> * Goway is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License and GNU * Lesser General Public License published by the Free Software * Foundation, either version 3 of the License, or (at your option) * any later version. * Goway is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License and GNU Lesser General Public License * for more details. * You should have received a copy of the GNU General Public License * and GNU Lesser General Public License along with this program. * If not, see <http://www.gnu.org/licenses/>.

-*- Indent-tabs-mode:nil; coding: utf-8 -*- * Copyleft (C) 2015 * "Tag bao" known as "wackonline" <bb.qnyd@gmail.com> * Goway is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License and GNU * Lesser General Public License published by the Free Software * Foundation, either version 3 of the License, or (at your option) * any later version. * Goway is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License and GNU Lesser General Public License * for more details. * You should have received a copy of the GNU General Public License * and GNU Lesser General Public License along with this program. * If not, see <http://www.gnu.org/licenses/>.

Index

Constants

View Source
const (
	CONFIGFILE = "/conf/config.json"
	HTTPSERVER = "0.0.0.0"
	PORT       = "8080"
	ENV        = "development"
	DEBUG      = true
	LOGGER     = "E_ALL"
	STATICPATH = "/public"
	VERSION    = "0.0.1"
)
View Source
const (
	//E_ALL     = 1
	E_ERROR   = 1
	E_WARNING = 2
	E_STRICT  = 4
	E_NOTICE  = 8
)
View Source
const (
	CONNECT = "CONNECT"
	DELETE  = "DELETE"
	GET     = "GET"
	HEAD    = "HEAD"
	OPTIONS = "OPTIONS"
	PATCH   = "PATCH"
	POST    = "POST"
	PUT     = "PUT"
	TRACE   = "TRACE"
	ANY     = "*"
)

Variables

View Source
var Params map[string]string

Functions

func InterfaceOf

func InterfaceOf(value interface{}) reflect.Type

Types

type ClassicGoway

type ClassicGoway struct {
	*Goway
	Router
	Logs    Logger
	Configs Config
}

func Bootstrap

func Bootstrap() *ClassicGoway

type Config

type Config interface {
	Getconf(string, string) interface{}
	Get(string) interface{}
	Set(string, interface{}) Config
	Logs() []Tlogs
}

func NewConfig

func NewConfig() Config

Initialize the configuration file if read config file failer,used default config

type Context

type Context interface {
	Injector
	Next()
}

type Goway

type Goway struct {
	Injector
	// contains filtered or unexported fields
}

func (*Goway) Action

func (g *Goway) Action(handler Handler)

func (*Goway) Handers

func (g *Goway) Handers(handlers ...Handler)

func (*Goway) Run

func (g *Goway) Run()

func (*Goway) RunOnAddr

func (g *Goway) RunOnAddr(addr string)

func (*Goway) ServeHTTP

func (g *Goway) ServeHTTP(w http.ResponseWriter, r *http.Request)

Type conversion (*Context)(nil) and (*http.ResponseWriter)(nil):

As (* interface{})(nil) Will be nil to the pointer to the interface type

function NewResponseWriter:

It is to implement the HTTP package ResponseWriter interface

func (*Goway) Use

func (g *Goway) Use(handler Handler)

type Handler

type Handler interface{}

To accept the custom function Example:

goway.Get("/",function(){
   //this function is Handler
})

The func() is Handler

func Recovery

func Recovery(conf Config) Handler

Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. While Martini is in development mode, Recovery will also output the panic as HTML.

type Injector

type Injector interface {
	Invoke(interface{}) ([]reflect.Value, error)
	Map(interface{}) Injector
	Set(reflect.Type, reflect.Value) Injector
	Get(reflect.Type) reflect.Value
	SetParent(Injector)
	MapTo(interface{}, interface{}) Injector
	All() map[reflect.Type]reflect.Value
}

func NewInjector

func NewInjector() Injector

New returns a new Injector.

type Logger

type Logger interface {
	Setloglevel(string)
	IsLogger(int) bool
	StartLogger() Handler
	Error(string, ...interface{})
	Warning(string, ...interface{})
	Strict(string, ...interface{})
	Notice(string, ...interface{})
	Use([]Tlogs)
	Print()
}

func NewLogger

func NewLogger() Logger

type Logs

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

func (*Logs) Error

func (lg *Logs) Error(str string, a ...interface{})

func (*Logs) IsLogger

func (lg *Logs) IsLogger(v int) bool

Determine whether excluded set log level Example:

all := A|B|C|D|E
all & C not eq 0 (C in ALL)
all1 := A|B|C
all1 & D eq 0 (D not in ALL)

func (*Logs) Notice

func (lg *Logs) Notice(str string, a ...interface{})

func (*Logs) Print

func (lg *Logs) Print()

func (*Logs) Setloglevel

func (lg *Logs) Setloglevel(lv string)

Excluded error message is set to false

func (*Logs) StartLogger

func (lg *Logs) StartLogger() Handler

func (*Logs) Strict

func (lg *Logs) Strict(str string, a ...interface{})

func (*Logs) Use

func (lg *Logs) Use(lgs []Tlogs)

func (*Logs) Warning

func (lg *Logs) Warning(str string, a ...interface{})

type ResponseWriter

type ResponseWriter interface {
	Header() http.Header
	Write([]byte) (int, error)
	WriteHeader(int)
	IsWrite() bool
	Status() int
	Size() int
}

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) ResponseWriter

Created an new ResponseWriter

type ReturnHandler

type ReturnHandler func(Context, []reflect.Value)

type Router

type Router interface {
	Get(pattern string, handler Handler)
	// Patch adds a route for a HTTP PATCH request to the specified matching pattern.
	Patch(string, Handler)
	// Post adds a route for a HTTP POST request to the specified matching pattern.
	Post(string, Handler)
	// Put adds a route for a HTTP PUT request to the specified matching pattern.
	Put(string, Handler)
	// Delete adds a route for a HTTP DELETE request to the specified matching pattern.
	Delete(string, Handler)
	// Any adds a route for any HTTP method request to the specified matching pattern.
	Any(string, Handler)
	// AddRoute adds a route for a given HTTP method request to the specified matching pattern.
	Static(string, string)
	NotFound(handlers Handler)
}

type Routes

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

func NewRouter

func NewRouter() *Routes

func (*Routes) AddRouter

func (r *Routes) AddRouter(method string, pattern string, handler Handler)

func (*Routes) Any

func (r *Routes) Any(pattern string, handler Handler)

Post adds a new Route for POST requests.

func (*Routes) Delete

func (r *Routes) Delete(pattern string, handler Handler)

Del adds a new Route for DELETE requests.

func (*Routes) Get

func (r *Routes) Get(pattern string, handler Handler)

Get adds a new Route for GET requests.

func (*Routes) Handler

func (routes *Routes) Handler(w http.ResponseWriter, r *http.Request, c Context)

func (*Routes) Match

func (routes *Routes) Match(method string, route *router, r *http.Request) (bool, Handler)

func (*Routes) NotFound

func (r *Routes) NotFound(handler Handler)

func (*Routes) Patch

func (r *Routes) Patch(pattern string, handler Handler)

Patch adds a new Route for PATCH requests.

func (*Routes) Post

func (r *Routes) Post(pattern string, handler Handler)

Post adds a new Route for POST requests.

func (*Routes) Put

func (r *Routes) Put(pattern string, handler Handler)

Put adds a new Route for PUT requests.

func (*Routes) Static

func (r *Routes) Static(pattern string, dir string)

Adds a new Route for Static http requests. Serves static files from the specified directory

type Tlogs

type Tlogs map[int]string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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