Documentation ¶
Overview ¶
Package dyd provides runtime support for web servers created by the dyd command.
Index ¶
- type App
- type Context
- func (c *Context) DBConn(ctx context.Context) (*sql.Conn, error)
- func (c *Context) Include(url string) (err error)
- func (c *Context) KV() *KV
- func (c *Context) URLInfoFor(url string) *URLInfo
- func (c *Context) W3CSS() string
- func (c *Context) WithCancel(parent context.Context) context.Context
- func (c *Context) WithDeadline(parent context.Context, d time.Time) context.Context
- func (c *Context) WithTimeout(parent context.Context, timeout time.Duration) context.Context
- type KV
- func (s *KV) BatchSet(ctx context.Context, pairs []KVPair) (err error)
- func (s *KV) Delete(ctx context.Context, key string) (err error)
- func (s *KV) DeleteLike(ctx context.Context, pattern string) (err error)
- func (s *KV) Get(ctx context.Context, key string, dest any) (err error)
- func (s *KV) GetLike(ctx context.Context, pattern string) (pairs []KVPair, err error)
- func (s *KV) GetTime(ctx context.Context, key string, dest any) (err error)
- func (s *KV) Inc(ctx context.Context, key string) (r int64, err error)
- func (s *KV) Set(ctx context.Context, key string, value any) (err error)
- type KVPair
- type PreRequestHandler
- type RequestHandler
- type URLInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type App ¶
type App interface { AddMeta(url string, attr []html.Attribute) error AddPreRequestHandler(handler PreRequestHandler) (id int) AddTitle(url string, title string) error Bind(url string, handler RequestHandler) DB() (*sql.DB, error) HasHandler(url string) bool KV() *KV ListenAndServe(addr string) error }
App represents a dyd web app.
type Context ¶
type Context struct { ReqSequence int64 Request *http.Request ResponseWriter http.ResponseWriter URLInfo *URLInfo // contains filtered or unexported fields }
Context provides access to the state of the server/session/request/database etc. Every web request creates a new Context instance which is automatically destroyed when the outermost serving function returns.
Context is not safe for concurrent use by multiple goroutines.
func (*Context) DBConn ¶ added in v0.0.11
DBConn returns a new *sql.Conn or an error, if any. All connections are automatically closed when the serving function returns.
func (*Context) Include ¶ added in v0.0.13
Include will call the serving function bound to url, if any. For example
ctx.Include("/path/to/file.html")
func (*Context) KV ¶ added in v0.1.4
KV returns the key value store associated with the server instance or an error, if any.
func (*Context) URLInfoFor ¶ added in v0.1.17
URLInfoFor returns the URLInfo for 'url' or nil, if 'url' is not registered.
func (*Context) W3CSS ¶ added in v0.0.14
W3CSS returns the content of
https://www.w3schools.com/w3css/4/w3.css
without using an internet connection. The latest version at the above URL is, as of 2023-05-23
/* W3.CSS 4.15 December 2020 by Jan Egil and Borge Refsnes */
It can be used like
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style><? write("%s", ctx.W3CSS()) ?></style> </head> <body> </body> </html>
func (*Context) WithCancel ¶ added in v0.1.15
WithCancel returns context.WithCancel(parent). The cancel function is run automatically when 'c' is destroyed.
func (*Context) WithDeadline ¶ added in v0.1.15
WithDeadline returns context.WithDeadline(parent, d). The cancel function is run automatically when 'c' is destroyed.
type KV ¶ added in v0.1.4
type KV struct {
// contains filtered or unexported fields
}
KV models a simple key/value store backed by a SQLite table with this schema
create table <table name> (key text primary key on conflict replace, value);
Note the 'value' column has no type a thus no affinity. Quoting https://www.sqlite.org/datatype3.html#datetime:
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values: ...
This means KV.Get(key, &dest) where dest is *time.Time or *sql.NullTime will _not_ work. The proper destinations of Get reflect the SQLite only data types listed at https://www.sqlite.org/datatype3.html#affinity:
- TEXT
- NUMERIC
- INTEGER
- REAL
- BLOB
To read time-representing values, use the GetTime method.
func (*KV) BatchSet ¶ added in v0.1.4
BatchSet sets the values associated with keys. The method is safe for concurrent use by multiple goroutines, all operations are serialized. Meaning all other operations on 's' are blocked until BatchSet completes.
BatchSet atomically sets all pairs or none of them.
func (*KV) Delete ¶ added in v0.1.4
Delete removes the value associated with key. The method is safe for concurrent use by multiple goroutines, all operations are serialized. Meaning all other operations on 's' are blocked until Delete completes.
func (*KV) DeleteLike ¶ added in v0.1.4
DeleteLike removes all value associated with keys matching 'pattern' using the SQLite operator LIKE (https://www.sqlite.org/lang_expr.html#like). The method is safe for concurrent use by multiple goroutines, all operations are serialized. Meaning all other operations on 's' are blocked until Delete completes.
func (*KV) Get ¶ added in v0.1.4
Get retrieves the value associated with key, if any. The method is safe for concurrent use by multiple goroutines, all operations are fully isolated/serialized. Meaning all other operations on 's' are blocked until Get completes.
To detect null values, pass an instance of one of the sql.Null* types or any other type implementing the sql.Scan interface{}. More info about acceptable 'dest' types available at https://pkg.go.dev/database/sql#Rows.Scan.
func (*KV) GetLike ¶ added in v0.1.4
GetLike retrieves all values associated with keys matching 'pattern' using the SQLite operator LIKE (https://www.sqlite.org/lang_expr.html#like). The method is safe for concurrent use by multiple goroutines, all operations are fully isolated/serialized. Meaning all other operation on 's' are blocked until GetLike completes.
When GetLike returns an error, it still returns also any pairs already produced.
Returning no pairs is not an error.
func (*KV) GetTime ¶ added in v0.1.4
GetTime is the is the same as Get, but expects the produced value to be NULL or to be a string representation of a datetime value. The 'dest' should have type *any, *time.Time or *sql.NullTime.
func (*KV) Inc ¶ added in v0.1.10
Inc atomically increments an int64 value at key. If no value for the key exists or if the value is NULL it is considered to be zero. Inc returns the new value or an error, if any.
type PreRequestHandler ¶ added in v0.1.15
PreRequestHandler handles user defined actions before looking for a RequestHandler using ctx.URL.Path. If the function return a non nil error no request handler will be run.
type RequestHandler ¶ added in v0.1.15
RequestHandler handles a http request, or a part of if, using 'ctx' and 'write'.