goblet

package module
v0.0.0-...-4069d2e Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: Apache-2.0 Imports: 40 Imported by: 6

README

goblet -- another golang web framework

Documentation

Overview

Package goblet is a golang web framework

Index

Constants

View Source
const (
	REST_READ       = "read"
	REST_READMANY   = "readmany"
	REST_DELETE     = "delete"
	REST_EDIT       = "edit"
	REST_NEW        = "new"
	REST_CREATE     = "create"
	REST_UPDATE     = "update"
	REST_UPDATEMANY = "updatemany"
	REST_DELETEMANY = "deletemany"
)
View Source
const (
	DevelopEnv = config.DevelopEnv
	ProductEnv = config.ProductEnv
)
View Source
const (
	SAVEFILE_SUCCESS              = iota
	SAVEFILE_STATE_DIR_ERROR      = iota
	SAVEFILE_CREATE_DIR_ERROR     = iota
	SAVEFILE_FORMFILE_ERROR       = iota
	SAVEFILE_RENAME_ERROR_BY_USER = iota
	SAVEFILE_COPY_ERROR           = iota
)
View Source
const BasicConfig = `
basic:
  env: development
  db_engine: none
`

Variables

View Source
var CookieIsMissing error = errors.New("Cookie is missing")

CookieIsMissing is returned when a cookie is missing.

View Source
var CookieNotValid error = errors.New("Cookie and signed cookie do not match")

CookieNotValid is returned when the cookie and its signed counterpart do not match.

I.e. the cookie value has been tampered with.

View Source
var Interrupted = errors.New("interrupted error")
View Source
var LogFile *os.File
View Source
var NoSuchRecord = errors.New("no such record")
View Source
var NotImplemented = fmt.Errorf("this method is not implemented")
View Source
var (
	// SignedCookieFormat is the format string used to decide the name of the
	// signed cookie.
	SignedCookieFormat string = "%s_signed"
)
View Source
var SignedCookieIsMissing error = errors.New("Signed cookie is missing")

SignedCookieIsMissing is returned when the signed cookie is missing.

View Source
var USERCOOKIENAME = "user"

Functions

func FileWithName

func FileWithName(file *os.File, name string) *fileWithName

FileWithName make file can by download for another name

func ResetDB

func ResetDB() error

func SafeGo

func SafeGo()

Use for defer unsafe go runtime

func TestMatcher

func TestMatcher(url string, ctrls ...interface{}) (string, string)

Types

type AutoHide

type AutoHide byte

type BasicBlockOption

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

func (*BasicBlockOption) AutoHidden

func (h *BasicBlockOption) AutoHidden() bool

func (*BasicBlockOption) ErrorRender

func (b *BasicBlockOption) ErrorRender() string

func (*BasicBlockOption) GetRender

func (h *BasicBlockOption) GetRender() []string

func (*BasicBlockOption) GetRouting

func (b *BasicBlockOption) GetRouting() []string

func (*BasicBlockOption) Layout

func (b *BasicBlockOption) Layout() string

func (*BasicBlockOption) SetRender

func (h *BasicBlockOption) SetRender(renders []string)

func (*BasicBlockOption) String

func (h *BasicBlockOption) String() string

func (*BasicBlockOption) TemplatePath

func (b *BasicBlockOption) TemplatePath() string

func (*BasicBlockOption) UpdateRender

func (h *BasicBlockOption) UpdateRender(o string, ctx *Context)

type Block

type Block interface {
}

Block is a group of http request

type BlockOption

type BlockOption interface {
	UpdateRender(string, *Context)
	GetRouting() []string
	MatchSuffix(string) bool
	//Get the render by user require, if required render is not allow, pass RenderNotAllowed
	GetRender() (render []string)
	//Reset the allowed renders
	SetRender([]string)
	//Call the function in object and Parse data, this function used before
	//the render prepared. So you can change function and render in here
	Parse(*Context) error
	Layout() string
	TemplatePath() string
	ErrorRender() string
	AutoHidden() bool
	//Get the type of block
	String() string
}

type ChangeSuffixOfConfig

type ChangeSuffixOfConfig interface {
	GetConfigSuffix() string
}

ChangeSuffixOfConfig Change the config file suffix, default is conf

type Configer

type Configer interface {
	GetConfigSource(s *Server) (io.Reader, error)
}

type Context

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

func (*Context) AddCookie

func (c *Context) AddCookie(cookie *http.Cookie) error

func (*Context) AddInfo

func (c *Context) AddInfo(key string, value interface{})

func (*Context) AddLoginId

func (c *Context) AddLoginId(id interface{}, setter ...LoginInfoSetter)

func (*Context) AddLoginIdAs

func (c *Context) AddLoginIdAs(id interface{}, name string, setter ...LoginInfoSetter)

func (*Context) AddRespond

func (c *Context) AddRespond(datas ...interface{})

Respond with multi data, data will tread as a key-value map for example: AddRespond("key1","value1",key2","value2") You can use AddRespond multi time in controller

func (*Context) AddSignedCookie

func (c *Context) AddSignedCookie(cookie *http.Cookie) (*http.Cookie, error)

AddSignedCookie adds the specified cookie to the response and also adds an additional 'signed' cookie that is used to validate the cookies value when SignedCookie is called.

func (*Context) AllowRender

func (c *Context) AllowRender(renders ...string)

AllowRender Allow some temporary render

func (*Context) BasicAuth

func (c *Context) BasicAuth() (string, string, bool)

BasicAuth 返回Basic Auth

func (*Context) BlockOptionType

func (c *Context) BlockOptionType() string

/////////for renders/////////////

func (*Context) Body

func (c *Context) Body() io.ReadCloser

func (*Context) Callback

func (c *Context) Callback() string

func (*Context) DelLogin

func (c *Context) DelLogin() error

Delete the login cookie saved

func (*Context) DelLoginAs

func (c *Context) DelLoginAs(name string) error

Delete the login cookie as specified name

func (*Context) EnableCache

func (c *Context) EnableCache()

EnableCache 手工启用缓存,默认Last-Modified为当前时间,Cache-Control为一年

func (*Context) EnableCacheFrom

func (c *Context) EnableCacheFrom(lastModified time.Time, duration ...time.Duration)

EnableCache 手工启用缓存,如果不传递时间参数,则默认Last-Modified为当前时间,Cache-Control为一年 如果传递一个时间参数,则Last-Modified为该时间,Cache-Control为一年, 如果传递两个时间参数,则Last-Modified为第一个时间,Cache-Control为第二个时间和第一个时间的差值

func (*Context) Fill

func (cx *Context) Fill(v interface{}, fills ...bool) error

goweb.Context Helper function to fill a variable with the contents of the request body. The body will be decoded based on the content-type and an apropriate RequestDecoder automatically selected If you want to use md5 function for the specified field, please add md5 tag for it. AND the md5 tag must be the last one of the tags, so if you have no other tag, please add ',' before md5

func (*Context) FillAs

func (cx *Context) FillAs(v interface{}, autofill bool, ct string) error

func (*Context) Form

func (c *Context) Form() url.Values

func (*Context) FormValue

func (c *Context) FormValue(key string) string

func (*Context) Format

func (c *Context) Format() string

func (*Context) FromAddr

func (c *Context) FromAddr() net.Addr

返回对端地址,请注意,如果是负载均衡过来的请求,会直接返回负载均衡地址,不会重新获取客户端地址,如果要 获取这种情况下的客户端正式IP,请调用 RemoteAddr方法

func (*Context) GetCookie

func (c *Context) GetCookie(name string) (*http.Cookie, error)

func (*Context) GetInfo

func (c *Context) GetInfo(key string) (interface{}, bool)

func (*Context) GetLoginId

func (c *Context) GetLoginId() (string, bool)

func (*Context) GetLoginIdAs

func (c *Context) GetLoginIdAs(name string) (string, bool)

func (*Context) GetLoginInfo

func (c *Context) GetLoginInfo() (*LoginContext, bool)

func (*Context) GetLoginInfoAs

func (c *Context) GetLoginInfoAs(name string) (*LoginContext, bool)

func (*Context) GetRender

func (cx *Context) GetRender() (render string, err error)

GetRender,返回渲染类型,该返回需要判断是否允许相关渲染类型,如果不需要判断,请使用Format函数

func (*Context) IntFormValue

func (c *Context) IntFormValue(key string) int64

func (*Context) Layout

func (c *Context) Layout() string

func (*Context) Method

func (c *Context) Method() string

返回当前的Method,json/html等

func (*Context) PathToURL

func (c *Context) PathToURL(path string) (*url.URL, error)

func (*Context) PostForm

func (c *Context) PostForm() url.Values

func (*Context) QueryString

func (c *Context) QueryString(key string) string

func (*Context) RedirectTo

func (c *Context) RedirectTo(url string)

func (*Context) Referer

func (c *Context) Referer() string

func (*Context) RemoteAddr

func (c *Context) RemoteAddr() net.Addr

该方法返回对端连接地址,返回有两种情况,如果头部没有X-Forwarded-For,表示是直接从客户端直接连接的。 此时,返回的addr,Network()方法返回“tcp"。如果头部有X-Forwarded-For,表示是从负载均衡跳转过来的。 此时,返回的addr,Network()方法返回“ip”

func (*Context) RemoteIP

func (c *Context) RemoteIP() (net.IP, error)

func (*Context) RenderAs

func (c *Context) RenderAs(name string)

RenderAs 设置渲染的模型文件,注意和UseRender的区别,需要修改json/html等用UseRender

func (*Context) ReqHeader

func (c *Context) ReqHeader() http.Header

ReqHeader 返回用户请求的Header

func (*Context) ReqHost

func (c *Context) ReqHost() string

ReqHost 返回用户请求的host

func (*Context) ReqMethod

func (c *Context) ReqMethod() string

返回用户请求的Method, GET/POST/HEAD等

func (*Context) ReqURL

func (c *Context) ReqURL() *url.URL

ReqURL 返回用户请求的URL

func (*Context) ResetDB

func (c *Context) ResetDB() error

func (*Context) Respond

func (c *Context) Respond(data interface{})

Respond 向用户返回内容,三种数据会进行特别处理: error类型:标记状态为内部错误 []byte类型:使用raw进行内容渲染,即原样输出,不进行json等格式转化 reader:使用raw进行内容渲染,即原样从输入中读取输出,不进行json等格式转化

func (*Context) RespondError

func (c *Context) RespondError(err error, context ...string)

RespondError 返回错误,如果错误为空,返回成功

func (*Context) RespondField

func (c *Context) RespondField(data interface{}, fields ...string)

func (*Context) RespondOK

func (c *Context) RespondOK()

func (*Context) RespondReader

func (c *Context) RespondReader(reader io.Reader)

func (*Context) RespondStatus

func (c *Context) RespondStatus(status int)

func (*Context) RespondWithRender

func (c *Context) RespondWithRender(data interface{}, render string)

func (*Context) RespondWithStatus

func (c *Context) RespondWithStatus(data interface{}, status int)

func (*Context) RestRedirectToRead

func (c *Context) RestRedirectToRead(id interface{})

func (*Context) SaveFileAt

func (cx *Context) SaveFileAt(path ...string) *filerSaver

func (*Context) SetHeader

func (c *Context) SetHeader(key, value string)

func (*Context) SetLayout

func (c *Context) SetLayout(l string)

func (*Context) ShowHidden

func (c *Context) ShowHidden()

func (*Context) SignedCookie

func (c *Context) SignedCookie(name string) (*http.Cookie, error)

Gets the cookie specified by name and validates that its value has not been tampered with by checking the signed cookie too. Will return CookieNotValid error if it has been tampered with, otherwise, it will return the actual cookie.

func (*Context) StatusCode

func (c *Context) StatusCode() int

func (*Context) StrFormValue

func (c *Context) StrFormValue(key string) string

func (*Context) String

func (c *Context) String() string

func (*Context) Suffix

func (c *Context) Suffix() string

func (*Context) TemplatePath

func (c *Context) TemplatePath() string

func (*Context) UseRender

func (c *Context) UseRender(render string)

Reset the context renders

func (*Context) UseStandErrPage

func (c *Context) UseStandErrPage() bool

func (*Context) UserAgent

func (c *Context) UserAgent() string

UserAgent 返回用户Agent

func (*Context) Version

func (c *Context) Version() string

Version 返回用户配置的代码版本

func (*Context) WrapError

func (ctx *Context) WrapError(err error, info string) error

func (*Context) Writer

func (c *Context) Writer() http.ResponseWriter

type ControllerNeedInit

type ControllerNeedInit interface {
	Init(*Server)
}

type ControllerNeedInitAndReturnError

type ControllerNeedInitAndReturnError interface {
	Init(*Server) error
}

type CookieLoginInfoStorer

type CookieLoginInfoStorer struct{}

func (*CookieLoginInfoStorer) AddLoginAs

func (c *CookieLoginInfoStorer) AddLoginAs(ctx *Context, lctx *LoginContext)

func (*CookieLoginInfoStorer) DeleteLoginAs

func (c *CookieLoginInfoStorer) DeleteLoginAs(ctx *Context, key string) error

func (*CookieLoginInfoStorer) GetLoginIdAs

func (c *CookieLoginInfoStorer) GetLoginIdAs(ctx *Context, key string) (*LoginContext, error)

type DbPwdPlugin

type DbPwdPlugin interface {
	SetPwd(origin string) string
}

DbPwdPlugin Change the db connection password

type DefaultRenderSetter

type DefaultRenderSetter interface {
	DefaultRender() string
}

type DelimSetter

type DelimSetter interface {
	SetDelim() [2]string
}

type ErrFuncSetter

type ErrFuncSetter interface {
	RespondError(*Context, error, ...string)
}

type ErrorRender

type ErrorRender byte

type File

type File struct {
	//Name the filename uploaded with file
	Name string
	//Path the filepath after saved in server
	Path   string
	Header textproto.MIMEHeader `json:"-"`
	// contains filtered or unexported fields
}

File the input file type, if you want to response a file, just response(*os.File)

func Open

func Open(path string) (f *File, err error)

Open open file in any location of server, if want to open file relate to www dir, please use OpenInPrivate

func (*File) Close

func (f *File) Close() error

func (*File) GetName

func (f *File) GetName() string

func (*File) GetSize

func (f *File) GetSize() int64

func (*File) OpenInPrivate

func (f *File) OpenInPrivate(s *Server) error

func (*File) Read

func (f *File) Read(p []byte) (n int, err error)

func (*File) ReadAt

func (f *File) ReadAt(p []byte, off int64) (n int, err error)

func (*File) SaveInPrivate

func (f *File) SaveInPrivate(dir string, s *Server) error

func (*File) SaveInPublic

func (f *File) SaveInPublic(dir string, s *Server) error

func (*File) SaveInTemp

func (f *File) SaveInTemp(dir string, s *Server) error

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

type FileGetter

type FileGetter func(string) (multipart.File, *multipart.FileHeader, error)

type Fn

type Fn struct {
	Name string
	Fn   interface{}
}

type FormFillFn

type FormFillFn func(content string) (interface{}, error)

type FormRequestDecoder

type FormRequestDecoder struct{}

a form-enc decoder for request body

func (*FormRequestDecoder) Unmarshal

func (d *FormRequestDecoder) Unmarshal(cx *Context, v interface{}, autofill bool) error

type GroupController

type GroupController byte

Controller which match full path and path with any suffix, eg. a GroupController with name Test will match /test and /test/1 and /test/a/b/c

type HtmlBlockOption

type HtmlBlockOption struct {
	BasicBlockOption
}

func (*HtmlBlockOption) MatchSuffix

func (h *HtmlBlockOption) MatchSuffix(suffix string) bool

func (*HtmlBlockOption) Parse

func (h *HtmlBlockOption) Parse(c *Context) error

func (*HtmlBlockOption) String

func (h *HtmlBlockOption) String() string

type JsonRequestDecoder

type JsonRequestDecoder struct{}

a JSON decoder for request body (just a wrapper to json.Unmarshal)

func (*JsonRequestDecoder) Unmarshal

func (d *JsonRequestDecoder) Unmarshal(cx *Context, v interface{}, autofill bool) (err error)

type Kv

type Kv interface {
	Get(name string, pointer interface{}) error
	Set(name string, pointer interface{}) error
	Del(name string) error
	Keys() []string
}

用于获取数值的接口

type KvDriver

type KvDriver interface {
	Collection(string) Kv //specified the table name and return the collection
}

用户指定KV驱动的接口

type Layout

type Layout byte

type LocalSaver

type LocalSaver struct {
}

func (*LocalSaver) Delete

func (l *LocalSaver) Delete(path string, force bool) error

func (*LocalSaver) Save

func (l *LocalSaver) Save(path string, f io.Reader) error

type LoginContext

type LoginContext struct {
	Name     string
	Id       string
	Deadline *time.Time
	Attrs    map[string]interface{}
}

func (*LoginContext) HasAttr

func (l *LoginContext) HasAttr(key string, content interface{}) bool

type LoginInfoSetter

type LoginInfoSetter func(*LoginContext)

func WithAttribute

func WithAttribute(key string, value interface{}) LoginInfoSetter

func WithDuration

func WithDuration(t time.Duration) LoginInfoSetter

type LoginInfoStorer

type LoginInfoStorer interface {
	AddLoginAs(ctx *Context, lctx *LoginContext)
	GetLoginIdAs(ctx *Context, key string) (*LoginContext, error)
	DeleteLoginAs(ctx *Context, key string) error
}

type MultiFormFillFn

type MultiFormFillFn func(ctx *Context, id string) (interface{}, error)

type MultiFormRequestDecoder

type MultiFormRequestDecoder struct{}

a form-enc decoder for request body

func (*MultiFormRequestDecoder) Unmarshal

func (d *MultiFormRequestDecoder) Unmarshal(cx *Context, v interface{}, autofill bool) error

type NewPlugin

type NewPlugin interface {
	AddCfgAndInit(server *Server) error
}

type OkFuncSetter

type OkFuncSetter interface {
	RespendOk(*Context)
}

type Option

type Option struct {
	DB string
}

type RemoteAddr

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

func (*RemoteAddr) Network

func (r *RemoteAddr) Network() string

func (*RemoteAddr) String

func (r *RemoteAddr) String() string

type Render

type Render byte

type RequestDecoder

type RequestDecoder interface {
	Unmarshal(cx *Context, v interface{}, autofill bool) error
}

types that impliment RequestDecoder can unmarshal the request body into an apropriate type/struct

type Response

type Response struct {
}

type RestBlockOption

type RestBlockOption struct {
	BasicBlockOption
}

func (*RestBlockOption) MatchSuffix

func (r *RestBlockOption) MatchSuffix(suffix string) bool

func (*RestBlockOption) Parse

func (r *RestBlockOption) Parse(c *Context) error

func (*RestBlockOption) String

func (h *RestBlockOption) String() string

func (*RestBlockOption) UpdateRender

func (r *RestBlockOption) UpdateRender(obj string, ctx *Context)

type RestController

type RestController byte

Controller which match full path according to RESTful rules, eg. a RestController with name Test will match /test and /test/1

type Route

type Route byte

type Saver

type Saver interface {
	Save(fullpath string, f io.Reader) error
	Delete(fullpath string, force bool) error
}

type Server

type Server struct {
	ConfigFile string

	Basic config.Basic
	Cache config.Cache
	Log   config.Log
	Db    config.Db

	Renders map[string]render.Render

	Name string
	// contains filtered or unexported fields
}

Server 服务器类型

var DefaultServer *Server

func Organize

func Organize(name string, plugins ...interface{}) *Server

func (*Server) AddConfig

func (s *Server) AddConfig(name string, obj interface{}) error

func (*Server) AddFillForTypeInForm

func (s *Server) AddFillForTypeInForm(typ string, fn FormFillFn)

func (*Server) AddFillForTypeInMultiForm

func (s *Server) AddFillForTypeInMultiForm(typ string, fn MultiFormFillFn)

func (*Server) AddFunc

func (s *Server) AddFunc(name string, fn interface{})

func (*Server) AddModel

func (s *Server) AddModel(models interface{}, syncs ...bool)

func (*Server) ControlBy

func (s *Server) ControlBy(block interface{})

ControlBy Use Member of struct of type goblet.Router to redefine the path

func (*Server) Debug

func (s *Server) Debug(fn func())

Debug 当服务器环境为调试环境时,执行相应的匿名函数,用于编写调试环境专用的代码块

func (*Server) DelFileInPrivate

func (s *Server) DelFileInPrivate(path string, force ...bool) error

func (*Server) DelFileInPublic

func (s *Server) DelFileInPublic(path string, force ...bool) error

func (*Server) Env

func (s *Server) Env() string

func (*Server) GetDelims

func (s *Server) GetDelims() []string

func (*Server) GetPlugin

func (s *Server) GetPlugin(key string) NewPlugin

GetPlugin 获得对应名称的插件

func (*Server) GetServerPathByCtrl

func (s *Server) GetServerPathByCtrl(ctrl interface{}) []string

func (*Server) Hash

func (s *Server) Hash(str string) string

Hash 获得一个字符串的加密版本

func (*Server) KV

func (s *Server) KV(name string) Kv

func (*Server) Organize

func (s *Server) Organize(name string, plugins []interface{})

Organize 进行服务器环境的初始化配置,初始化所有plugin,对于plugin的所有操作,在Organize之后都可以视为已经初始化

func (*Server) Pre

func (s *Server) Pre(fn interface{}, conds ...string)

func (*Server) PublicDir

func (s *Server) PublicDir() string

PublicDir 获得服务器对应的公共文件夹的地址

func (*Server) Run

func (s *Server) Run() error

Run 运营一个服务器

func (*Server) SaveInPrivate

func (s *Server) SaveInPrivate(path string, f io.Reader) error

将文件保存在私有目录,不可以使用http访问到

func (*Server) SaveInPublic

func (s *Server) SaveInPublic(path string, f io.Reader) error

将文件保存在公开目录,可以使用http访问到

func (*Server) ServeFile

func (s *Server) ServeFile(w http.ResponseWriter, r *http.Request, file string)

func (*Server) ServeHTTP

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

func (*Server) SetConfigSuffix

func (s *Server) SetConfigSuffix(suffix string)

func (*Server) SetDefaultError

func (s *Server) SetDefaultError(fn func(*Context, error, ...string))

func (*Server) SetDefaultOk

func (s *Server) SetDefaultOk(fn func(*Context))

func (*Server) WwwRoot

func (s *Server) WwwRoot() string

type SilenceUrlSetter

type SilenceUrlSetter interface {
	SetSilenceUrls() map[string]bool
}

type SingleController

type SingleController byte

Controller which only match full path eg. a SingleController with name Test will just match /test, not /test/1

type StringConfiger

type StringConfiger struct {
	Content string
}

StringConfiger is a configer that read config from a string

func (*StringConfiger) GetConfigSource

func (c *StringConfiger) GetConfigSource(s *Server) (io.Reader, error)

type XmlRequestDecoder

type XmlRequestDecoder struct{}

an XML decoder for request body

func (*XmlRequestDecoder) Unmarshal

func (d *XmlRequestDecoder) Unmarshal(cx *Context, v interface{}, autofill bool) (err error)

type YamlConfiger

type YamlConfiger struct {
}

func (*YamlConfiger) GetConfigSource

func (c *YamlConfiger) GetConfigSource(s *Server) (io.Reader, error)

Directories

Path Synopsis
oss

Jump to

Keyboard shortcuts

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