mego

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2020 License: Apache-2.0 Imports: 28 Imported by: 0

README

mego

mego is a powerful mvc framework written in go language

Get started with mego

  1. Make sure you have go 1.13+ installed.
  2. Run command go get gitee.com/simbory/mego/cmd/mego or go get gitee.com/simbory/mego && go install gitee.com/simbory/mego/cmd/mego to the mego tool
  3. Run command mego new mego-demo to create a new project
  4. Run the command cd mego-demo to Enter in to folder, and the following run commands to compile the project
    go generate controllers/starter.go
    go build
    
  5. Start the web server by running the command ./mego-demo. And then open the URL http://localhost:8888/ to see the result.

Documentation

Index

Constants

View Source
const Version = "v1.1.1"

Version the current version of the mego framework

Variables

This section is empty.

Functions

func Bytes2Str

func Bytes2Str(b []byte) string

Bytes2Str convert the byte array to string quickly

func ClearFilePath

func ClearFilePath(pathStr string) string

ClearFilePath clear the pathStr and return the shortest path.

func GetExeDir

func GetExeDir() string

GetExeDir get the current directory that the executable file is located in.

func GetWD

func GetWD() string

GetWD get the current working directory

func RegisterType

func RegisterType(value interface{})

func RegisterTypeName

func RegisterTypeName(name string, value interface{})

func Str2Bytes

func Str2Bytes(s string) []byte

Str2Bytes convert the string to byte array quickly

func StrJoin

func StrJoin(arr ...string) string

StrJoin combine some strings to a single string

Types

type ActionDef

type ActionDef struct {
	Name     string
	FuncName string
	Methods  []string
}

ActionDef the action definition struct for controller

type Authorize

type Authorize interface {
	IsAuthorized() bool
	HandleUnauthorized() interface{}
}

type BufResult

type BufResult struct {
	ContentType string
	Encoding    string

	StatusCode int
	// contains filtered or unexported fields
}

BufResult the buffered result

func NewBufResult

func NewBufResult(buf *bytes.Buffer) *BufResult

NewBufResult create a new buffer result with default value buf

func (*BufResult) AddHeader

func (b *BufResult) AddHeader(key, value string)

AddHeader write http header to the result

func (*BufResult) Execute

func (b *BufResult) Execute(w http.ResponseWriter, r *http.Request)

Execute write the data in the result buffer to the response original

func (*BufResult) ReadFrom

func (b *BufResult) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom read the data from reader r and write to the result buffer

func (*BufResult) Write

func (b *BufResult) Write(p []byte) (n int, err error)

Write write byte array p to the result buffer

func (*BufResult) WriteByte

func (b *BufResult) WriteByte(c byte) error

WriteByte write byte c to the result buffer

func (*BufResult) WriteRune

func (b *BufResult) WriteRune(r rune) (n int, err error)

WriteRune write rune r to the result buffer

func (*BufResult) WriteString

func (b *BufResult) WriteString(s string) (n int, err error)

WriteString write string s to the result buffer

type CatchFunc added in v1.1.1

type CatchFunc func(ex interface{})

type Configure

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

Configure the mego configuration struct

func (*Configure) AddFilter added in v1.1.1

func (config *Configure) AddFilter(urlPrefix string, filter Filter)

func (*Configure) AddRouteFunc

func (config *Configure) AddRouteFunc(name string, fun RouteFunc)

AddRouteFunc add custom route handler func to the server

func (*Configure) ConfigSession

func (config *Configure) ConfigSession(conf *SessionConfig, provider SessionProvider)

ConfigSession set the session state. conf: the session configuration; provider: the session provider

func (*Configure) ConfigViewDir

func (config *Configure) ConfigViewDir(viewDir string)

ConfigViewDir init the view engine directory. The filesystem template reader will be used

func (*Configure) ConfigViewReader added in v1.1.1

func (config *Configure) ConfigViewReader(reader TplReader)

ConfigViewReader init the view engine via the template reader

func (*Configure) IocSingleton

func (config *Configure) IocSingleton(i interface{})

IocSingleton add a singleton object to the dependency injection container

func (*Configure) IocTransient

func (config *Configure) IocTransient(i interface{})

IocSingleton add a transient object type to the dependency injection container

func (*Configure) RegisterController

func (config *Configure) RegisterController(controller interface{}, actions ...*ActionDef)

RegisterController register controller definition to the web server

func (*Configure) RegisterControllerName

func (config *Configure) RegisterControllerName(controller interface{}, name string, actions ...*ActionDef)

RegisterController register controller definition with name to the web server

func (*Configure) ResponseConfig added in v1.1.1

func (config *Configure) ResponseConfig() *ResponseConfig

ResponseConfig get the ResponseConfig entity

func (*Configure) RouteMvc

func (config *Configure) RouteMvc(routePath, defaultController, defaultAction string)

RouteMvc add the mvc handler to the route

func (*Configure) Server

func (config *Configure) Server() *Server

ResponseConfig get the Server entity

func (*Configure) SetConfigSettings added in v1.1.1

func (config *Configure) SetConfigSettings(settingsMap map[string]string)

SetConfigSettings add the config settings to server

func (*Configure) SetErrorHandler

func (config *Configure) SetErrorHandler(statusCode int, handler ErrHandler)

SetErrorHandler set the status code handler that will be used in the http response

func (*Configure) SetStaticHandler

func (config *Configure) SetStaticHandler(handler http.HandlerFunc)

SetStaticHandler set the static files handler

func (*Configure) SetStaticPrefixes added in v1.1.1

func (config *Configure) SetStaticPrefixes(prefixes []string)

type Controller

type Controller struct {
	Request  *http.Request
	Response http.ResponseWriter
	Server   *Server
	Items    map[string]interface{}
	// contains filtered or unexported fields
}

func (*Controller) FileResult added in v1.1.1

func (c *Controller) FileResult(path, contentType string) Result

func (*Controller) FormFile

func (c *Controller) FormFile(name string) *UploadFile

func (*Controller) FormVar

func (c *Controller) FormVar(key string) string

func (*Controller) JsonResult added in v1.1.1

func (c *Controller) JsonResult(data interface{}) Result

func (*Controller) MapContentPath

func (c *Controller) MapContentPath(path string) string

func (*Controller) MapRootPath

func (c *Controller) MapRootPath(path string) string

func (*Controller) QueryVar

func (c *Controller) QueryVar(key string) string

func (*Controller) Redirect

func (c *Controller) Redirect(url string, permanent bool) Result

func (*Controller) RouteVar

func (c *Controller) RouteVar(key string) string

func (*Controller) Session

func (c *Controller) Session() SessionStore

func (*Controller) SetViewData added in v1.1.1

func (c *Controller) SetViewData(key string, data interface{})

func (*Controller) TextResult added in v1.1.1

func (c *Controller) TextResult(content, contentType string) Result

func (*Controller) View

func (c *Controller) View(model interface{}) Result

func (*Controller) ViewPath

func (c *Controller) ViewPath(viewPath string, model interface{}) Result

func (*Controller) XmlResult added in v1.1.1

func (c *Controller) XmlResult(data interface{}) Result

type EmptyResult

type EmptyResult struct{}

EmptyResult the empty buffer result

func (*EmptyResult) Execute

func (er *EmptyResult) Execute(w http.ResponseWriter, r *http.Request)

Execute do nothing

type ErrHandler

type ErrHandler func(http.ResponseWriter, *http.Request, interface{})

ErrHandler define the internal s error handler func

type FileResult

type FileResult struct {
	ContentType string
	FilePath    string
}

FileResult the file result

func (*FileResult) Execute

func (fr *FileResult) Execute(w http.ResponseWriter, r *http.Request)

Execute execute the result

type Filter added in v1.1.1

type Filter interface {
	Init()
	DoFilter(ctx *HttpCtx, chain FilterChain)
	Destroy()
}

type FilterChain added in v1.1.1

type FilterChain interface {
	DoFilter(ctx *HttpCtx)
}

type HandlerFunc added in v1.1.1

type HandlerFunc func(http.ResponseWriter, *http.Request) interface{}

type HttpCtx

type HttpCtx struct {
	Server *Server
	Items  map[string]interface{}
	// contains filtered or unexported fields
}

HttpCtx the mego http context struct

func (*HttpCtx) End

func (ctx *HttpCtx) End()

End end the mego context and stop the rest request function

func (*HttpCtx) FileResult

func (ctx *HttpCtx) FileResult(path string, contentType string) Result

FileResult generate the mego result as file result

func (*HttpCtx) FormFile

func (ctx *HttpCtx) FormFile(formName string) *UploadFile

FormFile get the post file info

func (*HttpCtx) FormVar

func (ctx *HttpCtx) FormVar(key string) string

FormVar get the form value from request. It's the same as ctx.Request().FormVar(key)

func (*HttpCtx) JsonResult

func (ctx *HttpCtx) JsonResult(data interface{}) Result

JsonResult generate the mego result as JSON string

func (*HttpCtx) JsonpResult

func (ctx *HttpCtx) JsonpResult(data interface{}, callback string) Result

JsonpResult generate the mego result as jsonp string

func (*HttpCtx) MapContentPath

func (ctx *HttpCtx) MapContentPath(urlPath string) string

MapContentPath Returns the physical file path that corresponds to the specified virtual path.

func (*HttpCtx) MapRootPath

func (ctx *HttpCtx) MapRootPath(path string) string

MapRootPath Returns the physical file path that corresponds to the specified virtual path.

func (*HttpCtx) QueryVar

func (ctx *HttpCtx) QueryVar(key string) string

QueryVar get the value from the url query string

func (*HttpCtx) Redirect

func (ctx *HttpCtx) Redirect(urlStr string, permanent bool)

func (*HttpCtx) RedirectResult

func (ctx *HttpCtx) RedirectResult(urlStr string, permanent bool) Result

Redirect get the redirect result. if the value of 'permanent' is true , the status code is 301, else the status code is 302

func (*HttpCtx) Request

func (ctx *HttpCtx) Request() *http.Request

Request get the mego request

func (*HttpCtx) Response

func (ctx *HttpCtx) Response() http.ResponseWriter

Response get the mego response

func (*HttpCtx) RouteVar

func (ctx *HttpCtx) RouteVar(key string) string

RouteString get the route parameter value as string by key

func (*HttpCtx) Session

func (ctx *HttpCtx) Session() SessionStore

func (*HttpCtx) TextResult

func (ctx *HttpCtx) TextResult(content, contentType string) Result

TextResult generate the mego result as plain text

func (*HttpCtx) XmlResult

func (ctx *HttpCtx) XmlResult(data interface{}) Result

XmlResult generate the mego result as XML string

type RedirectResult

type RedirectResult struct {
	RedirectURL string
	StatusCode  int
}

RedirectResult the redirect result

func (*RedirectResult) Execute

func (rr *RedirectResult) Execute(w http.ResponseWriter, r *http.Request)

Execute execute the redirect result

type ResponseConfig added in v1.1.1

type ResponseConfig struct {
	// enable or disable the gzip compression in the server response. The default value is false
	GzipEnabled bool
	// contains filtered or unexported fields
}

func (*ResponseConfig) RemoveHeader added in v1.1.1

func (resp *ResponseConfig) RemoveHeader(key string)

func (*ResponseConfig) SetHeader added in v1.1.1

func (resp *ResponseConfig) SetHeader(key, value string)

type Result

type Result interface {
	Execute(w http.ResponseWriter, r *http.Request)
}

Result the request result interface

type RouteColl

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

type RouteFunc

type RouteFunc func(urlPath string, opt RouteOpt) string

AddRouteFunc define the route check function

type RouteOpt

type RouteOpt interface {
	Validation() string
	Setting() string
	MaxLength() int
	MinLength() int
}

RouteOpt define the route option interface

type Server

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

Server the mego server struct

func NewServer

func NewServer(webRoot, addr string) *Server

NewServer create a new server

webRoot: the root of this web server. and the content root is '${webRoot}/www'

addr: the address the server is listen on

func (*Server) HandleStatusCode

func (s *Server) HandleStatusCode(statusCode int, w http.ResponseWriter, r *http.Request, data interface{})

func (*Server) MapContentPath

func (s *Server) MapContentPath(virtualPath string) string

MapContentPath Returns the physical file path that corresponds to the specified virtual path. @param virtualPath: the virtual path starts with @return the absolute file path

func (*Server) MapRootPath

func (s *Server) MapRootPath(virtualPath string) string

MapRootPath Returns the physical file path that corresponds to the specified virtual path. @param virtualPath: the virtual path starts with @return the absolute file path

func (*Server) Run

func (s *Server) Run()

Run run the application as http

func (*Server) RunTLS

func (s *Server) RunTLS(certFile, keyFile string)

RunTLS run the application as https

func (*Server) ServeHTTP

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

func (*Server) StaticRequestHandler added in v1.1.1

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

StaticRequestHandler the default static request handler. you can replace it by calling config.SetStaticHandler method

func (*Server) UseStarter

func (s *Server) UseStarter(starter Starter)

UseStarter attach an starter to the server

type SessionConfig

type SessionConfig struct {
	// CookieName the name of the session cookie
	CookieName string `xml:"cookieName" json:"cookieName"`
	// Timeout the session cookie timeout in mili-seconds
	Timeout  int64  `xml:"timeout,attr" json:"timeout"`
	HTTPOnly bool   `xml:"httpOnly,attr" 		json:"http_only"`
	Secure   bool   `xml:"secure,attr" 		json:"secure"`
	Domain   string `xml:"domain,attr" 		json:"domain"`
}

SessionConfig the session configuration struct

type SessionManager

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

SessionManager the session manager struct

func (*SessionManager) Destroy

func (manager *SessionManager) Destroy(w http.ResponseWriter, r *http.Request)

Destroy Destroy session by its id in http request cookie.

func (*SessionManager) Start

Start generate or read the session id from http request. if session id exists, return SessionStore with this id.

type SessionProvider

type SessionProvider interface {
	Init(timeout time.Duration) error
	Read(sid string) SessionStore
	Destroy(sid string) error
	GC()
}

SessionProvider contains global session methods and saved SessionStores. it can operate a SessionStore by its id.

func NewMemSessionProvider

func NewMemSessionProvider() SessionProvider

NewMemSessionProvider create a new memory session provider with default config

func NewSQLSessionProvider added in v1.1.1

func NewSQLSessionProvider(driver, conn string) SessionProvider

NewSQLSessionProvider create a new sql session provider

type SessionStore

type SessionStore interface {
	Set(key string, value interface{}) error //set session Value
	Get(key string) interface{}              //get session Value
	Delete(key string) error                 //delete session Value
	Clear() error                            //delete all data
	ID() string                              //current session ID
	Flush() error                            //release the resource & save data to provider & return the data
}

SessionStore the session store interface

type Starter

type Starter interface {
	Config(config *Configure)
}

Starter the mego configuration starter interface

type TplReader

type TplReader interface {
	ReadFile(name string) ([]byte, error)
	Walk(walkFn func(path string, f os.FileInfo, err error) error) error
}

TplReader the template reader interface for the view engine

type TryCache added in v1.1.1

type TryCache interface {
	Catch(f CatchFunc) TryCache
	Finally(f func())
}

func Try added in v1.1.1

func Try(try func()) TryCache

type UploadFile

type UploadFile struct {
	FileName string
	Size     int64
	Error    error
	File     multipart.File
	Header   *multipart.FileHeader
}

UploadFile the uploaded file struct

func (*UploadFile) Save

func (file *UploadFile) Save(path string) error

Save save the posted file data as a file.

func (*UploadFile) SaveAndClose

func (file *UploadFile) SaveAndClose(path string) error

SaveAndClose save the posted file as a file and then Close the posted data stream

type ViewEngine

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

ViewEngine the mego view engine struct

func (*ViewEngine) Extend

func (e *ViewEngine) Extend(name string, viewFunc interface{})

Extend extend the view helper functions with 'name' and 'tplFuncMap'

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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