orivil

package module
v2.0.0-...-f5e0f75 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2016 License: MIT Imports: 33 Imported by: 0

README

Orivil Web Framework

Fast & Simple & Powerful Go Web Framework. Inspired by Symfony and Laravel.

Version

v2.0

Overview

  • micro MVC(alpha version)
  • service container
  • view compiler
  • middleware
  • comment routes
  • fast speed
  • components
  • I18n

Install

go get -v gopkg.in/orivil/orivil.v2

Run Example

cd $GOPATH/src/gopkg.in/orivil/orivil.v2/example

go run main.go

Test Example

Browser visit: http://localhost:8080

Community

Contributors

https://github.com/orivil/orivil/graphs/contributors

License

Released under the MIT License.

Documentation

Overview

Package orivil organizes all of the server components to be one runnable server, and also provides some useful methods.

Index

Constants

View Source
const (
	SvcMemorySession    = "session.MemorySession"
	SvcPermanentSession = "session.PermanentSession"
	SvcSessionContainer = "orivil.SessionContainer"
	SessionContainerKey = "orivil.SessionContainerKey"
)
View Source
const (
	SvcApp    = "orivil.App"
	SvcServer = "orivil.Server"
)
View Source
const SvcI18nFilter = "orivil.I18nFilter"
View Source
const (
	VERSION = "v2.0"
)

Variables

View Source
var (
	DirBase       = getBaseDir()
	DirStaticFile = filepath.Join(DirBase, "public")
	DirBundle     = filepath.Join(DirBase, "bundle")
	DirConfig     = filepath.Join(DirBase, "config")
	DirCache      = filepath.Join(DirBase, "cache")
)

dirs

View Source
var Cfg = config.NewConfig(DirConfig)
View Source
var CfgApp = &struct {
	DEBUG                     bool
	KEY                       string
	VIEW_FILE_EXT             string
	MEMORY_SESSION_KEY        string
	MEMORY_SESSION_MAX_AGE    int // minute
	MEMORY_GC_CHECK_NUM       int
	PERMANENT_SESSION_KEY     string
	PERMANENT_SESSION_MAX_AGE int // minute
	PERMANENT_GC_CHECK_NUM    int
	READ_TIMEOUT              int // second
	WRITE_TIMEOUT             int // second
}{

	DEBUG:                     true,
	KEY:                       "--------------------------------------",
	VIEW_FILE_EXT:             ".tmpl",
	MEMORY_SESSION_KEY:        "orivil-memory-session",
	MEMORY_SESSION_MAX_AGE:    45,
	MEMORY_GC_CHECK_NUM:       3,
	PERMANENT_SESSION_KEY:     "orivil-permanent-session",
	PERMANENT_SESSION_MAX_AGE: 45,
	PERMANENT_GC_CHECK_NUM:    3,
	READ_TIMEOUT:              30,
	WRITE_TIMEOUT:             30,
}

server config

View Source
var ErrDataExist = errors.New("view data alreay exist")
View Source
var ErrExitGorountine = errors.New("exit current gorountine")
View Source
var ErrUploadFileTooLarge = errors.New("upload file too large")

Check error

View Source
var (
	// the unique key for server, Orivil will read the value from config file "app.yml"
	Key string
)

Functions

func GetAllViewPages

func GetAllViewPages(a *App) (ps []view.Page)

func GetIp

func GetIp(r *http.Request) (net.IP, error)

func GetMergedFile

func GetMergedFile(a *App) (file []byte, err error)

func GetSysInfo

func GetSysInfo() []string

func GetViewPages

func GetViewPages(a *App) (ps []view.Page)

func SendEmail

func SendEmail(to string, title, body string) error

func ViewDebug

func ViewDebug(app *App, bundle, file string) (ok bool)

ViewDebug will send the view file, but when pares files got error, the debug file will be ignored from file trace message. This function is built for debug component, it should be used in last middleware, because the debug page will be sent only if normal view page has been set.

Types

type App

type App struct {
	Response   http.ResponseWriter
	Request    *http.Request
	Container  *service.Container // private container
	VContainer *view.Container
	Params     router.Param
	Action     string // looks like "bundle.Controller.Action"
	Start      time.Time
	Server     *Server
	// contains filtered or unexported fields
}

func (*App) AddCache

func (app *App) AddCache(name string, service interface{})

func (*App) Danger

func (app *App) Danger(msg string)

func (*App) Defer

func (app *App) Defer(f func())

func (*App) FilterI18n

func (app *App) FilterI18n(msg string) (i18nMsg string)

func (*App) Form

func (app *App) Form() url.Values

Form reads the cache or parses values from three sources: raw query in the URL; parameters from router; the request body if request method is POST, PUT or PATCH;

func (*App) FormFiles

func (app *App) FormFiles(maxFile, maxMemory int64, store FileStorage) error

FormFiles reads and stores upload files.

Usage:

Step 1: define the 1st & 2nd parameters:

var maxFileSize int64 = 600 << 10 // 600KB var maxMemorySize int64 = 60 << 20 // 60MB

Step 2: define the 3rd parameter(a custom function for storing the upload files):

var dir = "./uploads" var store orivil.FileStorage = func (srcFile multipart.File, name string) error {

	 fileName := filepath.Join(dir, name)

	 dstFile, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0666)
	 if err != nil {
		 return err
	 }
	 defer dstFile.Close()

	 _, err = io.Copy(dstFile, srcFile)
	 if err != nil {
		 return err
	 }
	 return nil
}

Step 3: check errors:

err := app.FormFiles(maxFileSize, maxMemorySize, store)

if orivil.ErrUploadFileTooLarge == err {

	fmt.Println("upload file too large")
} else if err != nil {

	fmt.Printf("upload file got error: %v", err)
}

func (*App) Get

func (app *App) Get(service string) interface{}

func (*App) GetCache

func (app *App) GetCache(service string) interface{}

func (*App) GetNew

func (app *App) GetNew(service string) interface{}

func (*App) Info

func (app *App) Info(msg string)

func (*App) IsGet

func (app *App) IsGet() bool

func (*App) IsPost

func (app *App) IsPost() bool

func (*App) JsonEncode

func (app *App) JsonEncode(data interface{})

func (*App) Msg

func (app *App) Msg(msg, typ string)

func (*App) PSession

func (app *App) PSession() PSession

func (*App) Query

func (app *App) Query() url.Values

Query reads the cache or parses values from two sources: raw query in the URL; parameters from router;

func (*App) Redirect

func (app *App) Redirect(url string, code ...int)

Redirect sends redirect header to client, then to terminate the HTTP goroutine.

func (*App) Session

func (app *App) Session() Session

func (*App) SessionContainer

func (app *App) SessionContainer() *service.Container

func (*App) SetCookie

func (app *App) SetCookie(key, value string, maxAge int)

func (*App) Success

func (app *App) Success(msg string)

func (*App) View

func (app *App) View(arg ...string) *App

View accepts 0-1 parameters, it reads the view file under the bundle directory which the router matched.

Most time we use this function in controllers, if you want to send view file in middleware, you should use ViewBundle to set which bundle directory you want to read.

If given: 0 param: will use the lowercase action name as the view file name. 1 param: will use the param as the view file name.

func (*App) ViewBundle

func (app *App) ViewBundle(bundle, file string) *App

ViewBundle reads the view file under the given bundle directory.

func (*App) Warning

func (app *App) Warning(msg string)

func (*App) With

func (app *App) With(name string, data interface{}) (err error)

func (*App) WriteString

func (app *App) WriteString(str string)

type BaseRegister

type BaseRegister struct{}

func (*BaseRegister) Boot

func (*BaseRegister) Boot(s *Server)

func (*BaseRegister) CfgMiddle

func (*BaseRegister) CfgMiddle(bag *middle.Bag)

func (*BaseRegister) Close

func (*BaseRegister) Close()

func (*BaseRegister) RegMiddle

func (*BaseRegister) RegMiddle(c *middle.Container)

func (*BaseRegister) RegRoute

func (*BaseRegister) RegRoute(c *router.Container)

func (*BaseRegister) RegService

func (*BaseRegister) RegService(c *service.Container)

type FileHandler

type FileHandler interface {
	// HandleFile to check if handle the url as static file
	HandleFile(*http.Request) bool
	// ServeFile for serve static file
	ServeFile(w http.ResponseWriter, r *http.Request, fileName string)
}

type FileStorage

type FileStorage func(srcFile multipart.File, name string) error

FileStorage defines the callback which how to store uploaded files.

type I18nFilter

type I18nFilter interface {
	FilterMsg(src string) (dst string)

	ViewSubDir() (dir string)
}

type Info

type Info struct {
	Version   string
	GoVersion string
	Process   int
	GoEnv     []string
}
var SysInfo *Info

func (*Info) Values

func (i *Info) Values() []string

type MiddlewareConfigure

type MiddlewareConfigure interface {
	CfgMiddle(bag *middle.Bag)
}

MiddlewareConfigure provide for controllers.

type NotFoundHandler

type NotFoundHandler interface {
	NotFound(w http.ResponseWriter, r *http.Request)
}

type PSession

type PSession interface {
	ID() string

	Set(key, value string)

	Get(key string) (value string)

	Flash(key string) (value string)

	Del(key string)
}

type Register

type Register interface {
	RegRoute(c *router.Container)

	RegService(c *service.Container)

	RegMiddle(c *middle.Container)

	CfgMiddle(bag *middle.Bag)

	Boot(s *Server)

	Close()
}

Every bundle register should implement Register interface.

type RequestHandler

type RequestHandler interface {
	Handle(app *App)
}

type RouteFilter

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

RouteFilter filters actions be registered to router

func NewRouteFilter

func NewRouteFilter() *RouteFilter

func (*RouteFilter) AddActions

func (f *RouteFilter) AddActions(actions []string)

AddActions filters actions

func (*RouteFilter) AddStructs

func (f *RouteFilter) AddStructs(structs []interface{})

AddStructs filters structs methods

func (*RouteFilter) FilterAction

func (f *RouteFilter) FilterAction(action string) bool

FilterAction used for router.NewContainer()

type Server

type Server struct {
	SContainer *service.Container
	MContainer *middle.Container
	RContainer *router.Container
	MiddleBag  *middle.Bag
	VContainer *view.Container

	*grace.GraceServer
	// contains filtered or unexported fields
}

func NewServer

func NewServer(addr string) *Server

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

func (*Server) ListenAndServeTLS

func (s *Server) ListenAndServeTLS(certFile, keyFile string) error

func (*Server) PrintInfo

func (s *Server) PrintInfo()

PrintInfo prints the server information to os.Stdout.

func (*Server) PrintInfoAt

func (s *Server) PrintInfoAt(w io.Writer)

PrintInfoAt prints the server information to the param w

func (*Server) RegisterBundle

func (s *Server) RegisterBundle(r ...Register)

RegisterBundle collects all bundle registers

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the incoming http request, every request goes through the function, including static file requests.

func (*Server) SetFileHandler

func (s *Server) SetFileHandler(h FileHandler)

SetFileHandler sets the static file handler.

func (*Server) SetNotFoundHandler

func (s *Server) SetNotFoundHandler(h NotFoundHandler)

SetNotFoundHandler sets the 404 not found handler.

func (*Server) Version

func (s *Server) Version() string

type Session

type Session interface {
	ID() string

	SetData(key string, data interface{})

	GetData(key string) interface{}

	FlashData(key string) (data interface{})

	DelData(key string)

	Set(key, value string)

	Get(key string) (value string)

	Flash(key string) (value string)

	Del(key string)
}

type TerminateHandler

type TerminateHandler interface {
	Terminate(app *App)
}

type Trace

type Trace struct {
	File string
	Line int
}

Directories

Path Synopsis
more information see: https://github.com/orivil/watcher
more information see: https://github.com/orivil/watcher

Jump to

Keyboard shortcuts

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