rest

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserKey string = "user"
)

Variables

View Source
var HashPasswd = func(passwd, salt string) string {
	sum := sha256.Sum256([]byte(salt + passwd))
	return base32.StdEncoding.EncodeToString(sum[:])
}
View Source
var ID = func() string { return rand.Text() }
View Source
var Salt = func() string { return rand.Text() }
View Source
var SessionKey = Salt()

Functions

func NewCSVDB

func NewCSVDB(path string) (*csvDB, error)

func SignSession

func SignSession(username string) string

func VerifySession

func VerifySession(session string) (string, bool)

Types

type Broker

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

func (*Broker) Publish

func (b *Broker) Publish(resource string, evt Event)

Publish an event to a resource.

func (*Broker) Subscribe

func (b *Broker) Subscribe(resource string, ch chan Event)

Subscribe to a resource.

func (*Broker) Unsubscribe

func (b *Broker) Unsubscribe(resource string, ch chan Event)

Unsubscribe from a resource.

type DB

type DB interface {
	Create(r Record) error
	Update(r Record) error
	Get(id string) (Record, error)
	Delete(id string) error
	Iter() func(yield func(Record, error) bool)
	Close() error
}

type Event

type Event struct {
	Action string   `json:"action"`
	ID     string   `json:"id"`
	Data   Resource `json:"data"`
}

type FieldSchema

type FieldSchema struct {
	Resource string
	Field    string
	Type     FieldType
	Min      float64
	Max      float64
	Regex    string
}

func (FieldSchema) Validate

func (field FieldSchema) Validate(v any) bool

type FieldType

type FieldType string
const (
	Number FieldType = "number"
	Text   FieldType = "text"
	List   FieldType = "list"
)

type Hook

type Hook func(trigger, resource string, user, r Resource) error

type Record

type Record []string

type Resource

type Resource map[string]any

type Schema

type Schema []FieldSchema

func (Schema) Record

func (s Schema) Record(res Resource) (Record, error)

func (Schema) Resource

func (s Schema) Resource(rec Record) (Resource, error)

type Server

type Server struct {
	Store  *Store
	Broker *Broker
	Mux    *http.ServeMux
	Hook   Hook
}

func NewServer

func NewServer(dataDir, tmplDir, staticDir string) (*Server, error)

func (*Server) ServeHTTP

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

type Store

type Store struct {
	Dir       string
	Schemas   map[string]Schema
	Resources map[string]DB
}

func NewStore

func NewStore(dir string) (*Store, error)

func (*Store) Authenticate

func (s *Store) Authenticate(r *http.Request) (Resource, error)

func (*Store) AuthenticateBasic

func (s *Store) AuthenticateBasic(username, password string) (Resource, error)

func (*Store) Authorize

func (s *Store) Authorize(resource, id, action string, user Resource) error

func (*Store) Close

func (s *Store) Close() error

func (*Store) Create

func (s *Store) Create(resource string, r Resource) (string, error)

func (*Store) Delete

func (s *Store) Delete(resource, id string) error

func (*Store) Get

func (s *Store) Get(resource, id string) (Resource, error)

func (*Store) List

func (s *Store) List(resource, sortBy string) ([]Resource, error)

func (*Store) Update

func (s *Store) Update(resource string, r Resource) error

Jump to

Keyboard shortcuts

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