Documentation ¶
Overview ¶
Package orivil organizes all of the server components to be one runnable server, and also provides some useful methods.
Index ¶
- Constants
- Variables
- func GetAllViewPages(a *App) (ps []view.Page)
- func GetIp(r *http.Request) (net.IP, error)
- func GetMergedFile(a *App) (file []byte, err error)
- func GetSysInfo() []string
- func GetViewPages(a *App) (ps []view.Page)
- func SendEmail(to string, title, body string) error
- func ViewDebug(app *App, bundle, file string) (ok bool)
- type App
- func (app *App) AddCache(name string, service interface{})
- func (app *App) Danger(msg string)
- func (app *App) Defer(f func())
- func (app *App) FilterI18n(msg string) (i18nMsg string)
- func (app *App) Form() url.Values
- func (app *App) FormFiles(maxFile, maxMemory int64, store FileStorage) error
- func (app *App) Get(service string) interface{}
- func (app *App) GetCache(service string) interface{}
- func (app *App) GetNew(service string) interface{}
- func (app *App) Info(msg string)
- func (app *App) IsGet() bool
- func (app *App) IsPost() bool
- func (app *App) JsonEncode(data interface{})
- func (app *App) Msg(msg, typ string)
- func (app *App) PSession() PSession
- func (app *App) Query() url.Values
- func (app *App) Redirect(url string, code ...int)
- func (app *App) Session() Session
- func (app *App) SessionContainer() *service.Container
- func (app *App) SetCookie(key, value string, maxAge int)
- func (app *App) Success(msg string)
- func (app *App) View(arg ...string) *App
- func (app *App) ViewBundle(bundle, file string) *App
- func (app *App) Warning(msg string)
- func (app *App) With(name string, data interface{}) (err error)
- func (app *App) WriteString(str string)
- type BaseRegister
- type FileHandler
- type FileStorage
- type I18nFilter
- type Info
- type MiddlewareConfigure
- type NotFoundHandler
- type PSession
- type Register
- type RequestHandler
- type RouteFilter
- type Server
- func (s *Server) ListenAndServe() error
- func (s *Server) ListenAndServeTLS(certFile, keyFile string) error
- func (s *Server) PrintInfo()
- func (s *Server) PrintInfoAt(w io.Writer)
- func (s *Server) RegisterBundle(r ...Register)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) SetFileHandler(h FileHandler)
- func (s *Server) SetNotFoundHandler(h NotFoundHandler)
- func (s *Server) Version() string
- type Session
- type TerminateHandler
- type Trace
Constants ¶
const ( SvcMemorySession = "session.MemorySession" SvcPermanentSession = "session.PermanentSession" SvcSessionContainer = "orivil.SessionContainer" SessionContainerKey = "orivil.SessionContainerKey" )
const ( SvcApp = "orivil.App" SvcServer = "orivil.Server" )
const SvcI18nFilter = "orivil.I18nFilter"
const (
VERSION = "v2.0"
)
Variables ¶
var ( DirBase = getBaseDir() DirStaticFile = filepath.Join(DirBase, "public") DirBundle = filepath.Join(DirBase, "bundle") DirConfig = filepath.Join(DirBase, "config") DirCache = filepath.Join(DirBase, "cache") )
dirs
var Cfg = config.NewConfig(DirConfig)
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
var ErrDataExist = errors.New("view data alreay exist")
var ErrExitGorountine = errors.New("exit current gorountine")
var ErrUploadFileTooLarge = errors.New("upload file too large")
Check error
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 GetMergedFile ¶
func GetSysInfo ¶
func GetSysInfo() []string
func GetViewPages ¶
func GetViewPages(a *App) (ps []view.Page)
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) FilterI18n ¶
func (*App) Form ¶
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) JsonEncode ¶
func (app *App) JsonEncode(data interface{})
func (*App) Query ¶
Query reads the cache or parses values from two sources: raw query in the URL; parameters from router;
func (*App) Redirect ¶
Redirect sends redirect header to client, then to terminate the HTTP goroutine.
func (*App) SessionContainer ¶
func (app *App) SessionContainer() *service.Container
func (*App) View ¶
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 ¶
ViewBundle reads the view file under the given bundle directory.
func (*App) WriteString ¶
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 FileStorage ¶
FileStorage defines the callback which how to store uploaded files.
type I18nFilter ¶
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 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 (*Server) ListenAndServe ¶
func (*Server) ListenAndServeTLS ¶
func (*Server) PrintInfo ¶
func (s *Server) PrintInfo()
PrintInfo prints the server information to os.Stdout.
func (*Server) PrintInfoAt ¶
PrintInfoAt prints the server information to the param w
func (*Server) RegisterBundle ¶
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.
type TerminateHandler ¶
type TerminateHandler interface {
Terminate(app *App)
}
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
more information see: https://github.com/orivil/watcher
|
more information see: https://github.com/orivil/watcher |