dyd

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package dyd provides runtime support for web servers created by the dyd command.

Index

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.

func NewApp

func NewApp() (App, error)

NewApp returns a newly created App or an error, if any.

type Context

type Context struct {
	IncludeArgs    []any
	ReqContext     context.Context
	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

func (c *Context) DBConn(ctx context.Context) (*sql.Conn, error)

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

func (c *Context) Include(url string, args ...any) (err error)

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

func (c *Context) KV() *KV

KV returns the key value store associated with the server instance or an error, if any.

func (*Context) URLInfoFor added in v0.1.17

func (c *Context) URLInfoFor(url string) *URLInfo

URLInfoFor returns the URLInfo for 'url' or nil, if 'url' is not registered.

func (*Context) W3CSS added in v0.0.14

func (c *Context) W3CSS() string

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

func (c *Context) WithCancel(parent context.Context) context.Context

WithCancel returns context.WithCancel(parent). The cancel function is run automatically when 'c' is destroyed.

func (*Context) WithDeadline added in v0.1.15

func (c *Context) WithDeadline(parent context.Context, d time.Time) context.Context

WithDeadline returns context.WithDeadline(parent, d). The cancel function is run automatically when 'c' is destroyed.

func (*Context) WithTimeout added in v0.1.15

func (c *Context) WithTimeout(parent context.Context, timeout time.Duration) context.Context

WithTimeout returns context.WithTimeout(parent, timeout). 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

func (s *KV) BatchSet(ctx context.Context, pairs []KVPair) (err error)

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

func (s *KV) Delete(ctx context.Context, key string) (err error)

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

func (s *KV) DeleteLike(ctx context.Context, pattern string) (err error)

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

func (s *KV) Get(ctx context.Context, key string, dest any) (err error)

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

func (s *KV) GetLike(ctx context.Context, pattern string) (pairs []KVPair, err error)

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

func (s *KV) GetTime(ctx context.Context, key string, dest any) (err error)

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

func (s *KV) Inc(ctx context.Context, key string) (r int64, err error)

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.

func (*KV) Set added in v0.1.4

func (s *KV) Set(ctx context.Context, key string, value any) (err error)

Set sets 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 Set completes.

type KVPair added in v0.1.4

type KVPair struct {
	Key   string
	Value any
}

KVPair represent a Value associated with Key.

type PreRequestHandler added in v0.1.15

type PreRequestHandler func(ctx *Context) error

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

type RequestHandler func(ctx *Context, write func(s string, args ...any) error)

RequestHandler handles a http request, or a part of if, using 'ctx' and 'write'.

type URLInfo added in v0.0.17

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

URLInfo provides information about a server page.

func (*URLInfo) Meta added in v0.0.17

func (n *URLInfo) Meta() [][]html.Attribute

Meta returns html.Attributes of <meta> elements.

func (*URLInfo) Title added in v0.1.17

func (n *URLInfo) Title() string

Title returns the text of the <title> element, if any.

Jump to

Keyboard shortcuts

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