goweb

package module
v0.0.0-...-885c2a5 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2015 License: LGPL-2.1 Imports: 7 Imported by: 0

README

GOWEB

"GOWEB" is a light but powerful web framework for golang developer

Feature

1)MVC design
http request ->  http handle  -> goweb.Router -> goweb.Controller ->    Action  -> http response 
                                                    [M]                   [C]         [V]
              (golang builtin)  (goweb buildin)  (goweb buildin)    (user define)  
2)Injectable

GOWEB support inject factory(like EJB in J2EE) to controller, or inject factory to factory.

http request ->  http handle  -> goweb.Router -> goweb.Controller ->    Action  -> http response 
                                                    [M]                   [C]         [V]
                                                     ^
                                                     ^ (Injection) 
                                      (Injection)    ^
                      goweb.Factory(1)   > >    goweb.Factory(2)
                                                    [F]
3)Stateful, Stateless or Standalone controller and factory design
  • Stateful Factory and Controller instance are created at first use in session, and destroy with session destroy.
  • Stateless Factory and Controller instance are created by request every time, and destroy after response.
  • Standalone Factory and Controller instance are created at application startup, and destroy with application destroy.

Hello Word for Stateful, Stateless and Standalone

  • Build and run code:
    package main
    import (
    	"github.com/ywzjackal/goweb"
    	"github.com/ywzjackal/goweb/context"
    	"github.com/ywzjackal/goweb/controller"
    	"github.com/ywzjackal/goweb/factory"
    	"github.com/ywzjackal/goweb/router"
    	"github.com/ywzjackal/goweb/session"
    	"github.com/ywzjackal/goweb/storage"
    	"net/http"
    	"fmt"
    )
    //...Setup basic configuration begin...
    var (
    	FactoryContainer    = factory.NewContainer()
    	ControllerContainer = controller.NewContainer(FactoryContainer)
    	// root router of webservice
    	Router = router.NewRouter(
    		ControllerContainer,
    		contextGenerator,
    	)
    )
    // ContextGenerator is an function that return an instance of goweb.Context
    func contextGenerator(res http.ResponseWriter, req *http.Request) goweb.Context {
    	return context.NewContext(
    		res,
    		req,
    		FactoryContainer,
    		session.NewSession(
    			res,
    			req,
    			storage.NewStorageMemory()),
    	)
    }
    //```Setup basic configuration finish```
    //...Setup Hello Word Controller begin...
    type index struct {
    	controller.Controller
    	controller.Standalone
    	Message string
    	Counter int
    }
    func (i *index) Action() {
    	msg := fmt.Sprintf("message:%s, counter:%d", i.Message, i.Counter)
    	i.Context().ResponseWriter().Write([]byte(msg))
    	i.Counter++
    }
    func init() {
    	ControllerContainer.Register("/", &index{})
    }
    //```Setup Hello Word Controller finish```
    func main() {
    	http.Handle("/", Router)
    	er := http.ListenAndServe(":8080", nil)
    	if er != nil {
    		panic(er)
    	}
    }
  • Open browser and type url "localhost:8080/?message=hello_word_from_goweb",will return: "message:hello_word_from_goweb,counter:0" and counter will increase when you refresh browser.
  • Restart browser and retype url above to see what happened with counter. yes, its standalone counter.
  • Change index type to Stateful Controller, rebuild and run :
    ...
    type index struct {
    	controller.Controller
    	controller.Stateful // changed this line
    	Message string
    	Counter int
    }
    ...
  • Refresh browser and the counter will increase too. but if you restart browser and reopen url, you will see counter increase from 0 not like Standalone Controller, a new Stateful Controller will be created when session begin.
  • Change index type to Stateless Controller, rebuild and run :
    ...
    type index struct {
    	controller.Controller
    	controller.Stateless // changed this line
    	Message string
    	Counter int
    }
    ...
  • the counter will always be 0, not increase. because Stateless Controller will always be created when request arrived.

#####Note:

  • Basic configuration only need setup once in whole application, it told Router which ControllerContainer will be used and how to generate an context.
  • See more example from https://github.com/ywzjackal/goweb_example /src/goweb/example/helloword/controller/..

#Hello Word for Injection

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Debug = true
)

Functions

This section is empty.

Types

type ActionPostprocessor

type ActionPostprocessor interface {
	// AfterAction() will be called when action method finished.
	AfterAction()
}

type ActionPreprocessor

type ActionPreprocessor interface {
	// BeforeAction() filter all request before action method
	// return false will stop framework to continue call action method
	// return true for normal
	BeforeAction() bool
}

type Context

type Context interface {
	// Request() return pointer of http.Request of http request
	Request() *http.Request
	// ResponseWriter() return interface http.ResponseWriter of http response
	ResponseWriter() http.ResponseWriter
	// FactoryContainer() return interface of factory container
	FactoryContainer() FactoryContainer
	// Session() return user session if session has been set
	Session() Session
	// Error() WebError of Context, Can be reset by SetError() method.
	Error() WebError
	// SetError(err) of Context, Can be get by Error() method.
	SetError(WebError)
}

Context interface for http request to response

type Controller

type Controller interface {
	// Type() return one of FactoryTypeStandalone/FactoryTypeStateless/FactoryTypeStateful
	Type() LifeType
	// SetContext() set current context before invoke actions methods
	SetContext(Context)
	// Context() return current http context
	Context() Context
}

type ControllerCallAble

type ControllerCallAble interface {
	// Call() Controller Action by Context
	Call(ctx Context) WebError
}

type ControllerContainer

type ControllerContainer interface {
	// Register a new controller caller to container with its prefix.
	// prefix is url prefix
	Register(prefix string, caller Controller)
	// Get controller by url prefix
	// return nil if not found in container
	Get(prefix string, ctx Context) (ControllerCallAble, WebError)
}

ControllerContainer is a container of controllers

type ControllerSchema

type ControllerSchema interface {
	// NewCallAble return a struct implement `ControllerCallAble`, used by router be invoked.
	NewCallAble() ControllerCallAble
	// Type() return one of FactoryTypeStandalone/FactoryTypeStateless/FactoryTypeStateful
	Type() LifeType
}

type DestroyAble

type DestroyAble interface {
	Destroy()
}

type Factory

type Factory interface {
	Type() LifeType
}

type FactoryContainer

type FactoryContainer interface {
	// Init after create new instance.
	// notice:
	// 		fc := &FactoryContainerStruct{}
	// 		fc.Init() //!!Must be called after created!!
	// 初始化,当工厂容器被创建后,必须马上使用本函数初始化
	Init() WebError
	// Register an injectable factory with its alias to container, set alias to "" for ignore.
	// 注册工厂并以指定的名字命名, 如果想使用默认名菜,将名称的参数设置为“”即可
	Register(Factory, string)
	//
	LookupStandalone(string) (InjectAble, WebError)
	//
	LookupStateless(string) (InjectAble, WebError)
	//
	LookupStateful(string, InjectGetterSetter) (InjectAble, WebError)
	//
	Lookup(string, InjectGetterSetter) (Factory, WebError)
	//
	LookupType(string) LifeType
}

FactoryContainer is the container of factories

type InitAble

type InitAble interface {
	Init()
}

type InjectAble

type InjectAble interface {
	FullName() string               //
	Alias() string                  //
	Type() LifeType                 //
	ReflectType() reflect.Type      //
	ReflectValue() reflect.Value    //
	FieldsStandalone() []InjectNode //
	FieldsStateful() []InjectNode   //
	FieldsStateless() []InjectNode  //
	Target() Factory
}

type InjectGetterSetter

type InjectGetterSetter interface {
	Get(string) InjectAble
	Set(string, InjectAble)
}

type InjectNode

type InjectNode struct {
	Name  string        // full struct name with package path
	Value reflect.Value //
}

type LifeType

type LifeType int
const (
	LifeTypeError LifeType = iota
	LifeTypeStateless
	LifeTypeStandalone
	LifeTypeStateful
)

func (LifeType) String

func (l LifeType) String() string

type Router

type Router interface {
	http.Handler
	// ControllerContainer() return interface of controller container used by this router
	ControllerContainer() ControllerContainer
}

RouterInterface

type Session

type Session interface {
	// Id() return session's id
	Id() string
	// Get(key) session value by key
	Get(string) string
	// Set(key,value) to session
	Set(string, string)
	// Remove(key) by key
	Remove(string)
	// MemMap() return low-level memory map
	MemMap() map[interface{}]interface{}
}

Session interface for browser

type Storage

type Storage interface {
	// Get() return the element(interface{}) find by key,
	// return nil if not found with the key
	Get(string) interface{}
	// Set() element(interface{}) with it's key,
	// and data will removed after the default duration from last query
	Set(string, interface{})
	// SetWithLife() set data with deadline.
	SetWithLife(string, interface{}, time.Duration)
	// Remove() element before deadline.
	Remove(string)
}

Storage interface

type WebError

type WebError interface {
	error
	ErrorAll() string
	Code() int
	File() string
	Line() int
	FuncName() string
	Children() []WebError
	CallStack() []WebErrorStackNode
	Append(msg string, args ...interface{}) WebError
}

func NewWebError

func NewWebError(code int, msg string, args ...interface{}) WebError

type WebErrorStackNode

type WebErrorStackNode struct {
	File string
	Line int
	Func string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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