Documentation ¶
Index ¶
- Constants
- Variables
- func AltInternal(err error, s string) error
- func AltInternalf(err error, f string, args ...interface{}) error
- func Bearer(c *C) string
- func CheckToken(c *C, s *signer.TimeSigner) error
- func DeclareAddrFlag(def string) *string
- func DefaultAddr(app string) string
- func DiscardURLServerParts(u *url.URL) *url.URL
- func Listen(addr string) (net.Listener, error)
- func ListenAndServe(addr string, s Service) error
- func Log(l *Logger, s string)
- func NeverCache(c *C)
- func PrintJSON(c *C, v interface{}) error
- func RemoteIP(c *C) net.IP
- func RemoteIPString(c *C) string
- func ReplyJSON(c *C, v interface{}) error
- func Serve(s Service) http.Handler
- func UnmarshalJSONBody(c *C, v interface{}) error
- type Auth
- type C
- func (c *C) ClearCookie(name string)
- func (c *C) Current() string
- func (c *C) ErrCode(err error) bool
- func (c *C) IsMobile() bool
- func (c *C) Method() string
- func (c *C) PathIsDir() bool
- func (c *C) ReadCookie(name string) string
- func (c *C) Redirect(url string)
- func (c *C) Rel() string
- func (c *C) RelRoute() []string
- func (c *C) ShiftRoute(inc int)
- func (c *C) WriteCookie(name, v string, expires time.Time)
- type Func
- type HostMux
- type LogPrinter
- type Logger
- func (l *Logger) AltError(err error, code, s string) error
- func (l *Logger) AltInternal(err error, s string) error
- func (l *Logger) AltInvalidArg(err error, s string) error
- func (l *Logger) Exit(err error) error
- func (l *Logger) Print(args ...interface{})
- func (l *Logger) Printf(f string, args ...interface{})
- type Mux
- type Router
- func (r *Router) Call(p string, f interface{})
- func (r *Router) Default(f Func)
- func (r *Router) Dir(p string, f Func) error
- func (r *Router) DirService(p string, s Service) error
- func (r *Router) File(p string, f Func) error
- func (r *Router) Get(p string, f Func) error
- func (r *Router) Index(f Func)
- func (r *Router) JSONCall(p string, f interface{}) error
- func (r *Router) JSONCallMust(p string, f interface{})
- func (r *Router) MethodFile(m, p string, f Func) error
- func (r *Router) Post(p string, f Func) error
- func (r *Router) Serve(c *C) error
- type Service
- type ServiceSet
- type StaticFiles
- type Templates
Constants ¶
const DefaultStaticPath = "lib/site"
DefaultStaticPath is the default path for static files.
const DefaultTemplatePath = "_/tmpl"
DefaultTemplatePath is the default template path.
const TemplatesJSON = "!JSON"
TemplatesJSON tells the Templates to print JSON data rather than render the template.
Variables ¶
var Miss error = errcode.NotFoundf(nothingHere)
Miss is returned when a mux or router does not hit anything in its path lookup.
var NeedSignIn error = errcode.Unauthorizedf("please sign in")
NeedSignIn is returned when sign in is required for visiting a particular page.
var NotFound error = errcode.NotFoundf(nothingHere)
NotFound is a true not found error.
var TheLogger = StdLogger()
TheLogger is the default logger that logs to default golang log.
Functions ¶
func AltInternal ¶
AltInternal prints error to TheLogger. It is an alias to TheLogger.AltInteral
func AltInternalf ¶
AltInternalf printe the formatted error to TheLogger.
func CheckToken ¶
func CheckToken(c *C, s *signer.TimeSigner) error
CheckToken checks if the bearer token is properly signed by the same API key.
func DeclareAddrFlag ¶
DeclareAddrFlag declares the -addr flag.
func DefaultAddr ¶
DefaultAddr gets the default address for an application
func DiscardURLServerParts ¶
DiscardURLServerParts discards the server parts of an URL, including scheme, host, port and user info.
func Listen ¶
Listen listens on the address. If the address ends with .sock, it Listen's on the unix domain socket.
func ListenAndServe ¶
ListenAndServe serves on the address. If the address ends with .sock, it ListenAndServe's on the unix domain socket.
func NeverCache ¶
func NeverCache(c *C)
NeverCache sets the Cache-Control header to "max-age=0; no-store".
func RemoteIPString ¶
RemoteIPString returns the string form of the remote IP address. It returns empty string when IP cannot be determined.
func UnmarshalJSONBody ¶
UnmarshalJSONBody unmarshals the body into a JSON object.
Types ¶
type Auth ¶
Auth is an authentication service that sets up the authentication context before serving.
type C ¶
type C struct { Path string User string UserLevel int // 0 for normal user. 0 with empty User is anonymous. UserData interface{} Req *http.Request Resp http.ResponseWriter Context context.Context HTTPS bool Data map[string]interface{} // contains filtered or unexported fields }
C provides the request context for a web application.
func NewContext ¶
func NewContext(w http.ResponseWriter, req *http.Request) *C
NewContext creates a new context from the incomming request.
func (*C) ReadCookie ¶
ReadCookie reads the cookie from the context.
func (*C) Rel ¶
Rel returns the current relative route. The return value changes if the routing is using a router, otherwise, it will always return the full routing path.
type Func ¶
Func defines an HTTP handling function.
func JSONCall ¶
func JSONCall(f interface{}) Func
JSONCall wraps a function of form `func(c *aries.C, req *RequestType) (resp *ResponseType, error)` into a JSON marshalled RPC call.
func RedirectTo ¶
RedirectTo creates a Func that always redirects to u
func StringFunc ¶
StringFunc creates a Func that always reply the given string.
func (Func) ListenAndServe ¶
ListenAndServe launches the handler as an HTTP service.
type HostMux ¶
type HostMux struct {
// contains filtered or unexported fields
}
HostMux routes request to different services based on the incoming host.
type LogPrinter ¶
type LogPrinter interface {
Print(s string)
}
LogPrinter is the interface for printing server logs.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a logger for logging server logs
func NewLogger ¶
func NewLogger(p LogPrinter) *Logger
NewLogger creates a new logger that prints server logs to the given printer.
func StdLogger ¶
func StdLogger() *Logger
StdLogger returns the logger that logs to the default std log.
func (*Logger) AltInternal ¶
AltInternal logs the error and returns an alternative internal error.
func (*Logger) AltInvalidArg ¶
AltInvalidArg logs the error and returns an alternative invalid arg error.
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux is a router for a given context
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is a path router. Similar to mux, but routing base on a filesystem-like syntax.
func NewRouter ¶
func NewRouter() *Router
NewRouter creates a new router for filesystem like path routing.
func (*Router) Default ¶
Default sets a default handler for handling routes that does not hit anything in the routing tree.
func (*Router) DirService ¶
DirService adds a service into the router tree under a directory node.
func (*Router) Index ¶
Index sets the handler function for handling the index page when hitting this router, that is when hitting the root of it. One can only hit this route node when the path is ended with a slash '/'.
func (*Router) JSONCall ¶
JSONCall adds a JSON marshalled POST based RPC call node into the routing tree. The function must be in the form of `func(c *aries.C, req *RequestType) (resp *ResponseType, error)`, where RequestType and ResponseType are both JSON marshallable.
func (*Router) JSONCallMust ¶
JSONCallMust is the same as JSONCall, but panics if there is an error.
func (*Router) MethodFile ¶
MethodFile adds a routing file node into the routing tree that accepts only the given method.
type ServiceSet ¶
type ServiceSet struct { Auth Auth Resource Service Guest Service User Service Admin Service IsAdmin func(c *C) bool InternalSignIn Func }
ServiceSet is a set of muxes that
func (*ServiceSet) Serve ¶
func (s *ServiceSet) Serve(c *C) error
Serve serves the incoming request with the mux set.
func (*ServiceSet) ServeInternal ¶
func (s *ServiceSet) ServeInternal(c *C) error
ServeInternal serves the incoming request with the mux set, but only serves resource for normal users, and allows only admins (users with positive level) to visit the guest mux.
type StaticFiles ¶
type StaticFiles struct {
// contains filtered or unexported fields
}
StaticFiles is a module that serves static files.
func NewStaticFiles ¶
func NewStaticFiles(p string) *StaticFiles
NewStaticFiles creates a module that serves static files.
func (*StaticFiles) CacheAge ¶
func (s *StaticFiles) CacheAge(ageSecs int)
CacheAge sets the maximum age for the cache.
func (*StaticFiles) Serve ¶
func (s *StaticFiles) Serve(c *C) error
Serve serves incoming HTTP requests.
type Templates ¶
type Templates struct {
// contains filtered or unexported fields
}
Templates is a collection of templates.
func NewTemplates ¶
NewTemplates creates a collection of templates in a particular folder.