web

package module
v0.0.0-...-4f81a5c Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: MIT Imports: 24 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidPassword = errors.New("invalid password")
View Source
var ErrInvalidRegistrationCode = errors.New("invalid registration code")
View Source
var ErrInvalidSession = errors.New("invalid session")
View Source
var ErrUserNotFound = errors.New("user not found")

Functions

func HandleCORS

func HandleCORS(w http.ResponseWriter, r *http.Request)

func NewID

func NewID() string

func PathParts

func PathParts(p string) []string

func ServeHTTPS

func ServeHTTPS(h http.Handler, acmeConfig ACMEConfig) error

Types

type ACMEConfig

type ACMEConfig struct {
	Hosts      []string
	AdminEmail string
	CertDir    string
}

func (*ACMEConfig) CertManager

func (c *ACMEConfig) CertManager() autocert.Manager

func (*ACMEConfig) HTTPSServer

func (c *ACMEConfig) HTTPSServer(mux http.Handler, certManager autocert.Manager) http.Server

type Admin

type Admin struct {
	PhoneNumber string

	TwillioAccountSID  string
	TwillioAuthToken   string
	TwillioPhoneNumber string
}

Admin is a web administrator.

func (*Admin) Notify

func (a *Admin) Notify(message string) error

Notify sends a text message to the admin.

type App

type App struct {
	Auth *AuthDB
}

func (*App) ServeHTTP

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

type AuthClient

type AuthClient struct {
	AuthServerAddr string
}

func (*AuthClient) CreateInviteCode

func (c *AuthClient) CreateInviteCode()

func (*AuthClient) Login

func (c *AuthClient) Login()

func (*AuthClient) Logout

func (c *AuthClient) Logout()

func (*AuthClient) Register

func (c *AuthClient) Register()

func (*AuthClient) ValidateSession

func (c *AuthClient) ValidateSession(s *Session) error

type AuthDB

type AuthDB struct {
	// Users are the users of the service.
	Users map[string]*User `json:"users"`
	// PasswordSalt is used to salt passwords.
	PasswordSalt []byte `json:"password_salt"`
	// RegistrationCodes are needed to create new users.
	// Codes can be created by sending a POST request to /register with a valid registrar key.
	RegistrationCodes map[string]bool `json:"registration_codes"`
	// RegistrarKey is a secret key that is needed to generate registration codes.
	// If your service is open to new signups, you can share this string publicly.
	RegistrarKey string `json:"registrar_key"`
	// contains filtered or unexported fields
}

AuthDB is a service for authentication. It is intended to be set up as a service that is used by other services and not end users. It is intended to be used on the open internet.

func (*AuthDB) Invite

func (s *AuthDB) Invite() string

Invite creates a new registration code and returns it.

func (*AuthDB) Login

func (s *AuthDB) Login(userID, password string) (string, error)

Login returns a new session token for the given user ID and password. If it returns an error, it will be of type ErrInvalidPassword or ErrUserNotFound.

func (*AuthDB) Register

func (s *AuthDB) Register(registrationCode, password string) (string, error)

Register creates a new user with the given password and returns the new user's ID. If it returns an error, it will be of type ErrInvalidRegistrationCode.

func (*AuthDB) ServeHTTP

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

ServeHTTP serves the authentication server. There are 5 endpoints: /invite, /register, /login, /logout, and /user.

func (*AuthDB) User

func (s *AuthDB) User(id string) (*User, bool)

User returns the User with the given ID.

type ChecksumServer

type ChecksumServer struct {
	Dir string
}

ChecksumServer is a server for checksums.

func (*ChecksumServer) ServeHTTP

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

ServeHTTP serves HTTP requests.

type Command

type Command struct {
	Assignments []string `json:"assignments"`
	Func        ID       `json:"func"`
	Args        []string `json:"args"`
}

type Comment

type Comment struct{}

type Counter

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

func (*Counter) Dec

func (c *Counter) Dec()

func (*Counter) Inc

func (c *Counter) Inc()

func (*Counter) Value

func (c *Counter) Value() int

type DB

type DB struct {
	LocalPath string
	Locks     map[string]*sync.Mutex
}

func (*DB) CreateTable

func (db *DB) CreateTable(id string, typ golang.Ident) error

func (*DB) Table

func (db *DB) Table(id string) *Table

type ExecRequest

type ExecRequest struct {
	Token  string         `json:"token"`
	Pkg    string         `json:"pkg"`
	Func   string         `json:"func"`
	Inputs map[string]any `json:"inputs"`
}

type Field

type Field struct {
	Name Name `json:"name"`
	Type Type `json:"type"`
}

type File

type File struct {
	// Type is the type of the file.
	// It can be one of the following:
	// - any builtin Go type
	// - any type defined in the Go standard library
	// - any exported type defined in a public MIT licensed library on pkg.go.dev
	Type string `json:"type"`
	// Owner is the ID of the organization that owns the file.
	Owner string `json:"owner"`
	// Owners is a map of user IDs that own the file.
	// Deprecated: Use Owner instead.
	Owners map[string]bool `json:"owners"`
	// Name is the name of the file.
	// Deprecated: Use EnglishName instead.
	Name string `json:"name"`
	// EnglishName is the English-language name of the file.
	EnglishName english.Name `json:"english_name"`
	// Doc is the documentation for the file.
	Doc string `json:"doc"`
	// Comments are a list of comments on the file.
	Comments []Comment `json:"comments"`
	// Public decides whether the file is publicly available or not.
	// This means it can be viewed by anyone.
	// It can still only be edited by members of the owner organization.
	Public bool `json:"public"`
	// CreatedAt is the Unix timestamp of when the file was created.
	CreatedAt int64 `json:"created_at"`
	// UpdatedAt is the Unix timestamp of when the file was last updated.
	UpdatedAt int64 `json:"updated_at"`
	// SHA256 is the SHA256 hash of the file.
	SHA256 string `json:"sha256"`
	// Size is the size of the file in bytes.
	Size int64 `json:"size"`
}

File is core concept in the web library. It is used throughout the package to represent files and directories. It doesn't actually contain the file's contents, but a SHA256 hash of the file's contents.

type FileServer

type FileServer struct {
	AuthServerAddr string
}

FileServer is a data structure that implements the http.Handler interface. It serves files and directories on the open web. Clients can GET and PUT files and directories.

func (*FileServer) ServeHTTP

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

ServeHTTP serves HTTP requests for files. GET /path/to/file returns the file. PUT /path/to/file creates or updates the file. GET /path/to/dir returns the directory as a special type of file.

type Folder

type Folder struct {
	Contents []string
}

type Function

type Function struct {
	Name     string    `json:"name"`
	Inputs   []Field   `json:"inputs"`
	Outputs  []Field   `json:"outputs"`
	Validate []Command `json:"validate"`
	Execute  []Command `json:"execute"`
}

type ID

type ID struct {
	Path []Name `json:"path"`
}

type LoginRequeset

type LoginRequeset struct {
	UserID   string `json:"user_id"`
	Password string `json:"password"`
}

type Metadata

type Metadata struct {
	// Type is the type of the file.
	// It can be one of the following:
	// - any builtin Go type
	// - any type defined in the Go standard library
	// - any exported type defined in a public MIT licensed library on pkg.go.dev
	Type string `json:"type"`
	// Owner is the ID of the organization that owns the file.
	Owner string `json:"owner"`
	// Owners is a map of user IDs that own the file.
	// Deprecated: Use Owner instead.
	Owners map[string]bool `json:"owners"`
	// Name is the name of the file.
	// Deprecated: Use EnglishName instead.
	Name string `json:"name"`
	// EnglishName is the English-language name of the file.
	EnglishName english.Name `json:"english_name"`
	// Doc is the documentation for the file.
	Doc string `json:"doc"`
	// Comments are a list of comments on the file.
	Comments []Comment `json:"comments"`
	// Public decides whether the file is publicly available or not.
	// This means it can be viewed by anyone.
	// It can still only be edited by members of the owner organization.
	Public bool `json:"public"`
	// CreatedAt is the Unix timestamp of when the file was created.
	CreatedAt int64 `json:"created_at"`
	// UpdatedAt is the Unix timestamp of when the file was last updated.
	UpdatedAt int64 `json:"updated_at"`
	// SHA256 is the SHA256 hash of the file.
	SHA256 string `json:"sha256"`
	// Size is the size of the file in bytes.
	Size int64 `json:"size"`
}

Metadata is the metadata for a file.

type MetadataServer

type MetadataServer map[string]*Metadata

MetadataServer is a server for global metadata.

func (MetadataServer) Owner

func (s MetadataServer) Owner(path Path) string

Owner returns the owner of the given path.

func (MetadataServer) Owners

func (s MetadataServer) Owners(path Path) map[string]bool

Owners returns the owners of the given path. Deprecated.

func (MetadataServer) ServeHTTP

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

ServeHTTP serves HTTP requests for metadata.

type Model

type Model struct {
	Name    string     `json:"name"`
	Fields  []Field    `json:"fields"`
	Methods []Function `json:"methods"`
}

type Name

type Name struct {
	Words []string `json:"words"`
}

type OrgServer

type OrgServer struct {
}

OrgServer is a server for managing organizations.

type Organization

type Organization struct {
	// EnglishName is the English-language name of the organization.
	EnglishName english.Name `json:"english_name"`
	// Memebers is a list of UserIDs of the members of the organization.
	Members []string `json:"members"`
}

Org is an organization. Organizations are

type Path

type Path []string

func ParsePath

func ParsePath(p string) Path

ParsePath parses a url.URL.Path into a web.Path.

func (Path) Parts

func (p Path) Parts() []string

Parts returns the parts of the path.

func (Path) String

func (p Path) String() string

String returns the path as a string.

type Platform

type Platform struct {
	// LetsEncryptEmail is the email address used to register with Let's Encrypt.
	LetsEncryptEmail string `json:"lets_encrypt_email"`

	// DataDir is a path to the directory where the platform stores its data.
	// The following subdirectories are used:
	// - src: source code
	// - logs: request logs
	// - certs: TLS certificates
	// - public: public files
	// - types: type metadata
	// - functions: function metadata
	// - bin: compiled binaries
	// - user_data: user data
	DataDir string `json:"data_dir"`
	// contains filtered or unexported fields
}

Platform is the main type for the web platform. It implements the http.Handler interface. DataDir is the directory where the platform stores its data.

func (*Platform) BuildAll

func (p *Platform) BuildAll() error

BuildAll builds all commands in /cmd.

func (*Platform) BuildGoFunc

func (p *Platform) BuildGoFunc(pkg, fn string) error

func (*Platform) Install

func (p *Platform) Install() error

Install installs the platform. It copies the systemd files and starts the platform service.

func (*Platform) ServeHTTP

func (p *Platform) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the main request handler for the platform.

func (*Platform) ServeHTTPNext

func (p *Platform) ServeHTTPNext(w http.ResponseWriter, r *http.Request)

ServeHTTPNext is the next version of the ServeHTTPNext function. It is a work in progress.

func (*Platform) Start

func (p *Platform) Start() error

Start starts the web platform.

func (*Platform) Upgrade

func (p *Platform) Upgrade() error

Upgrade upgrades the platform.

func (*Platform) UserID

func (p *Platform) UserID(sessionToken string) string

UserID returns the user ID for the given session token.

func (*Platform) WriteFile

func (p *Platform) WriteFile(host, path string, file *File) error

WriteFile writes the file for the given host and path.

type Platform2

type Platform2 struct {
	// Apps maps app IDs to Apps.
	Apps map[string]*App
	// Routes maps paths to app IDs.
	// Paths are r.Host + r.URL.Path.
	Routes map[string]string
}

func (*Platform2) App

func (p *Platform2) App(host, path string) (*App, bool)

App returns the app for the given host and path.

type Request

type Request struct {
	Timestamp int64               `json:"timestamp"`
	FromIP    string              `json:"from_ip"`
	Method    string              `json:"method"`
	Host      string              `json:"host"`
	Path      string              `json:"path"`
	Query     map[string][]string `json:"query"`
	Headers   map[string][]string `json:"headers"`
	Body      []byte              `json:"body"`
}

func BuildRequest

func BuildRequest(r *http.Request) (*Request, error)

Build request returns a newly created Request object. It leaves the request body open for further processing.

type Server

type Server func(p *Platform, w http.ResponseWriter, r *http.Request)

type Session

type Session struct {
	UserID string `json:"user_id"`
	Token  string `json:"token"`
}

type Table

type Table struct {
	LocalPath string
	Lock      *sync.Mutex
}

func (*Table) Delete

func (t *Table) Delete(id string) error

Delete removes an existing file from the table.

func (*Table) Get

func (t *Table) Get(id string) (*File, error)

Get returns a file from the table.

func (*Table) List

func (t *Table) List(filter func(*File) bool) ([]*File, error)

List returns all files in the table that match the filter.

func (*Table) Post

func (t *Table) Post(owners map[string]bool, data []byte) (string, error)

Post adds a new file to the table. A random name is given to the file and returned.

func (*Table) Put

func (t *Table) Put(id string, value []byte) error

Put updates an existing file in the table.

func (*Table) Search

func (t *Table) Search(search string) ([]*File, error)

Search returns all files in the table that match the search string.

func (*Table) Type

func (t *Table) Type() golang.Ident

type Type

type Type struct {
	IsPointer bool `json:"is_pointer"`
	IsArray   bool `json:"is_array"`
	IsMap     bool `json:"is_map"`
	BaseType  ID   `json:"base_type"`
}

type User

type User struct {
	PasswordHash  string          `json:"password_hash"`
	SessionTokens map[string]bool `json:"session_tokens"`
	Emails        []string        `json:"emails"`
	Orgs          map[string]bool `json:"orgs"`
}

func (*User) PrimaryEmail

func (u *User) PrimaryEmail() string

PrimaryEmail returns the primary email address of the user. It panic if the user has no email addresses.

Jump to

Keyboard shortcuts

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