aries

package module
v0.0.0-...-43bb807 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2022 License: AGPL-3.0 Imports: 24 Imported by: 4

Documentation

Index

Constants

View Source
const DefaultStaticPath = "lib/site"

DefaultStaticPath is the default path for static files.

View Source
const DefaultTemplatePath = "_/tmpl"

DefaultTemplatePath is the default template path.

View Source
const TemplatesJSON = "!JSON"

TemplatesJSON tells the Templates to print JSON data rather than render the template.

Variables

View Source
var Miss error = errcode.NotFoundf(nothingHere)

Miss is returned when a mux or router does not hit anything in its path lookup.

View Source
var NeedSignIn error = errcode.Unauthorizedf("please sign in")

NeedSignIn is returned when sign in is required for visiting a particular page.

View Source
var NotFound error = errcode.NotFoundf(nothingHere)

NotFound is a true not found error.

View Source
var TheLogger = StdLogger()

TheLogger is the default logger that logs to default golang log.

Functions

func AltInternal

func AltInternal(err error, s string) error

AltInternal prints error to TheLogger. It is an alias to TheLogger.AltInteral

func AltInternalf

func AltInternalf(err error, f string, args ...interface{}) error

AltInternalf printe the formatted error to TheLogger.

func Bearer

func Bearer(c *C) string

Bearer returns the authorization token.

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

func DeclareAddrFlag(def string) *string

DeclareAddrFlag declares the -addr flag.

func DiscardURLServerParts

func DiscardURLServerParts(u *url.URL) *url.URL

DiscardURLServerParts discards the server parts of an URL, including scheme, host, port and user info.

func ListenAndServe

func ListenAndServe(addr string, s Service) error

ListenAndServe serves on the address. If the address ends with .sock, it ListenAndServe's on the unix domain socket.

func Log

func Log(l *Logger, s string)

Log logs the message to the logger if the logger is not nil.

func NeverCache

func NeverCache(c *C)

NeverCache sets the Cache-Control header to "max-age=0; no-store".

func PrintJSON

func PrintJSON(c *C, v interface{}) error

PrintJSON replies a JSON marshaable object over the reponse with pretty printing.

func RemoteIP

func RemoteIP(c *C) net.IP

RemoteIP returns the remote IP address.

func RemoteIPString

func RemoteIPString(c *C) string

RemoteIPString returns the string form of the remote IP address. It returns empty string when IP cannot be determined.

func ReplyJSON

func ReplyJSON(c *C, v interface{}) error

ReplyJSON replies a JSON marshable object over the response.

func Serve

func Serve(s Service) http.Handler

Serve wraps a service into an HTTP handler.

func UnmarshalJSONBody

func UnmarshalJSONBody(c *C, v interface{}) error

UnmarshalJSONBody unmarshals the body into a JSON object.

Types

type Auth

type Auth interface {
	Service

	// Setup sets up the authentication in context.
	Setup(c *C) error
}

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.

	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) ClearCookie

func (c *C) ClearCookie(name string)

ClearCookie clears a cookie.

func (*C) Current

func (c *C) Current() string

Current returns the next part in the current relative route.

func (*C) ErrCode

func (c *C) ErrCode(err error) bool

ErrCode returns an error based on its error code.

func (*C) IsMobile

func (c *C) IsMobile() bool

IsMobile checks if the user agent of the request is mobile or not.

func (*C) Method

func (c *C) Method() string

Method returns the c.Req.Method.

func (*C) PathIsDir

func (c *C) PathIsDir() bool

PathIsDir return true if the path ends with a slash.

func (*C) ReadCookie

func (c *C) ReadCookie(name string) string

ReadCookie reads the cookie from the context.

func (*C) Redirect

func (c *C) Redirect(url string)

Redirect redirects the request to another URL.

func (*C) Rel

func (c *C) Rel() string

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.

func (*C) RelRoute

func (c *C) RelRoute() []string

RelRoute returns the current relative route string array.

func (*C) ShiftRoute

func (c *C) ShiftRoute(inc int)

ShiftRoute shift the routing pointer by inc.

func (*C) WriteCookie

func (c *C) WriteCookie(name, v string, expires time.Time)

WriteCookie sets a cookie.

type Func

type Func func(c *C) error

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

func RedirectTo(u string) Func

RedirectTo creates a Func that always redirects to u

func StringFunc

func StringFunc(s string) Func

StringFunc creates a Func that always reply the given string.

func (Func) ListenAndServe

func (f Func) ListenAndServe(addr string) error

ListenAndServe launches the handler as an HTTP service.

func (Func) Serve

func (f Func) Serve(c *C) error

Serve implements the service interface.

func (Func) ServeAt

func (f Func) ServeAt(lis net.Listener) error

ServeAt launches the handler as an HTTP service at the given listener.

func (Func) ServeHTTP

func (f Func) ServeHTTP(w http.ResponseWriter, req *http.Request)

type HostMux

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

HostMux routes request to different services based on the incoming host.

func NewHostMux

func NewHostMux() *HostMux

NewHostMux creates a new host mux.

func (*HostMux) Serve

func (m *HostMux) Serve(c *C) error

Serve serves an incoming request.

func (*HostMux) Set

func (m *HostMux) Set(host string, s Service)

Set binds a host domain to a particular service.

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) AltError

func (l *Logger) AltError(err error, code, s string) error

AltError logs the error and returns an alternative error with code.

func (*Logger) AltInternal

func (l *Logger) AltInternal(err error, s string) error

AltInternal logs the error and returns an alternative internal error.

func (*Logger) AltInvalidArg

func (l *Logger) AltInvalidArg(err error, s string) error

AltInvalidArg logs the error and returns an alternative invalid arg error.

func (*Logger) Exit

func (l *Logger) Exit(err error) error

Exit prints the error and exit the service.

func (*Logger) Print

func (l *Logger) Print(args ...interface{})

Print prints a message to the logger.

func (*Logger) Printf

func (l *Logger) Printf(f string, args ...interface{})

Printf prints a formatted message to the logger.

type Mux

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

Mux is a router for a given context

func NewMux

func NewMux() *Mux

NewMux creates a new mux for the incoming request.

func (*Mux) Dir

func (m *Mux) Dir(s string, f Func) error

Dir add is a shortcut of Exact(s) and Prefix(s + "/").

func (*Mux) Exact

func (m *Mux) Exact(s string, f Func) error

Exact adds an exact matching rule.

func (*Mux) Prefix

func (m *Mux) Prefix(s string, f Func) error

Prefix adds a prefix matching rule.

func (*Mux) Route

func (m *Mux) Route(c *C) Func

Route returns the serving function for the given context.

func (*Mux) Serve

func (m *Mux) Serve(c *C) error

Serve serves an incoming request based on c.Path. It returns true when it hits something. And it returns false when it hits nothing.

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) Call

func (r *Router) Call(p string, f interface{})

Call is an alias of JSONCallMust

func (*Router) Default

func (r *Router) Default(f Func)

Default sets a default handler for handling routes that does not hit anything in the routing tree.

func (*Router) Dir

func (r *Router) Dir(p string, f Func) error

Dir adds a routing directory node into the routing tree.

func (*Router) DirService

func (r *Router) DirService(p string, s Service) error

DirService adds a service into the router tree under a directory node.

func (*Router) File

func (r *Router) File(p string, f Func) error

File adds a routing file node into the routing tree.

func (*Router) Get

func (r *Router) Get(p string, f Func) error

Get adds a routing file node into the routing tree that handles GET requests.

func (*Router) Index

func (r *Router) Index(f Func)

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

func (r *Router) JSONCall(p string, f interface{}) error

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

func (r *Router) JSONCallMust(p string, f interface{})

JSONCallMust is the same as JSONCall, but panics if there is an error.

func (*Router) MethodFile

func (r *Router) MethodFile(m, p string, f Func) error

MethodFile adds a routing file node into the routing tree that accepts only the given method.

func (*Router) Post

func (r *Router) Post(p string, f Func) error

Post adds a routing file node into the routing tree that handles POST requests.

func (*Router) Serve

func (r *Router) Serve(c *C) error

Serve serves the incoming context. It returns Miss if the path hits nothing and Default() is not set.

type Service

type Service interface {
	Serve(c *C) error
}

Service is a interface similar to Func

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

func NewTemplates(p string, logger *Logger) *Templates

NewTemplates creates a collection of templates in a particular folder.

func (*Templates) Serve

func (ts *Templates) Serve(c *C, p string, dat interface{}) error

Serve serves a data page using a particular template.

Directories

Path Synopsis
daemon
Package gometa provides a simple HTTP server that supports `go get` meta command.
Package gometa provides a simple HTTP server that supports `go get` meta command.
Package mailman provides an HTTP Oauth2 based module that sends email using Gmail API.
Package mailman provides an HTTP Oauth2 based module that sends email using Gmail API.
Package redhttp provides a service that routes all incoming http requests to https.
Package redhttp provides a service that routes all incoming http requests to https.
Package tasks provides a way to implement a set of simple subcommand-like API for user to invoke.
Package tasks provides a way to implement a set of simple subcommand-like API for user to invoke.

Jump to

Keyboard shortcuts

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