cooking

package module
v0.0.0-...-7648c87 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package cooking contains all the cooking server code.

Index

Constants

View Source
const (
	// Fresh if the reciped needs fresh ingredients.
	Fresh Freshness = 1
	// MiddleFresh if the reciped needs middle fresh ingredients.
	MiddleFresh Freshness = 2
	// NotFresh if no fresh ingredients are needed.
	NotFresh Freshness = 3
	// MaxFreshness is the currently highest value for freshness.
	MaxFreshness int64 = int64(NotFresh)
)
View Source
const (
	// StatementAdd adds a recipe to database.
	StatementAdd = "INSERT INTO recipes(name, description, length, freshness, source) VALUES(:name, :description, :length, :freshness, :source)"
	// StatementUpdate updates a recipe.
	StatementUpdate = "" /* 134-byte string literal not displayed */
	// StatementDelete deletes a recipe from database.
	StatementDelete = "DELETE FROM recipes WHERE id = :id"
	// StatementGetAll returns all recipes from database.
	StatementGetAll = "SELECT id, name, description, length, freshness, source FROM recipes"
	// StatementGet returns the recipe with the given id from database.
	StatementGet = "SELECT id, name, description, length, freshness, source FROM recipes WHERE id = :id"
	// StatementGetUser returns the user with the given username from database.
	StatementGetUser = "SELECT username, password FROM user WHERE username = :username"
	// StatementAddUser adds a user to the database.
	StatementAddUser = "INSERT INTO user (username, password) VALUES(:username, :password)"
	// StatementSetPassword sets the password for a user.
	StatementSetPassword = "UPDATE user SET password = :password WHERE username = :username"
	// StatementGetAllUsers returns all users.
	StatementGetAllUsers = "SELECT username, password FROM user"
)
View Source
const (
	// StatusClassSuccess should be returned if the message is positive.
	StatusClassSuccess = "toast success"
	// StatusClassWarning should be returned if the message is not critical.
	StatusClassWarning = "toast warning"
	// StatusClassCritical should be returned if the message is critical.
	StatusClassCritical = "toast error"
)

Variables

View Source
var (
	// Handlers contains all known http handlers.
	// This will be filled with init functions.
	Handlers = make(map[string]HandleFunc)
	// PublicHandlers are handler which will not have an authentication check.
	// this will be filled with init functions.
	PublicHandlers = make(map[string]HandleFunc)
)
View Source
var (
	// EmptyUser is the empty user
	EmptyUser = User{}
)

Functions

func SendError

func SendError(writer http.ResponseWriter, request *http.Request, err error, errCode int)

SendError over http.

func SendTemplate

func SendTemplate(writer http.ResponseWriter, request *http.Request, server *Server, templateName string, data TemplateData)

SendTemplate sends the given template.

Types

type AddTemplateData

type AddTemplateData struct {
	BaseTemplateData
	Link   string
	Recipe Recipe
}

AddTemplateData contains the template data for the add template.

type BaseTemplateData

type BaseTemplateData struct {
	NoHeader bool
	Printer  bool
	// contains filtered or unexported fields
}

BaseTemplateData is the base implementation for template data.

func (*BaseTemplateData) Message

func (data *BaseTemplateData) Message() StatusMessage

Message returns the message saved.

func (*BaseTemplateData) SetMessage

func (data *BaseTemplateData) SetMessage(message StatusMessage)

SetMessage sets the message.

type Config

type Config struct {
	// Addr is defined as <bind>:<port>
	Addr string `yaml:"addr"`
	// Templates is the path to the templates folder.
	Templates   string `yaml:"templates"`
	StoragePath string `yaml:"storage-path"`
	StaticDir   string `yaml:"static-dir"`
}

Config contains the configuration for the server.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a config struct with default values.

type Freshness

type Freshness int

Freshness defines the freshness.

func (Freshness) HTML

func (f Freshness) HTML() template.HTML

HTML return the freshness value as html formatted scala.

type HandleFunc

type HandleFunc = func(server *Server) http.HandlerFunc

HandleFunc defines a function which returns a http.HandleFunc

type Recipe

type Recipe struct {
	ID          uint64
	Name        string
	Description string
	Length      string
	Freshness   Freshness
	Source      string
}

Recipe contains all infos about a recipe.

func (*Recipe) ParseFreshness

func (recipe *Recipe) ParseFreshness(freshness string) error

ParseFreshness parses the freshness value and adds it to the recipe.

func (*Recipe) SourceHTML

func (recipe *Recipe) SourceHTML() template.HTML

SourceHTML returns the source field as html code. If it is a url it will be converted to qrcode. Otherwise it will be returned as text.

func (*Recipe) Validate

func (recipe *Recipe) Validate() error

Validate validates the struct for missing or broken values.

type RootTemplateData

type RootTemplateData struct {
	BaseTemplateData
	Recipes []Recipe
}

RootTemplateData contains the template data for the root template.

type Server

type Server struct {
	Config       *Config
	Templates    *template.Template
	Storage      *Storage
	SessionStore *sessions.CookieStore
	// contains filtered or unexported fields
}

Server is the server.

func NewServer

func NewServer(config *Config) (*Server, error)

NewServer creates a new server with the given configuration.

func (*Server) GetMessage

func (server *Server) GetMessage(writer http.ResponseWriter, request *http.Request) (message StatusMessage, err error)

GetMessage returns the message from the session if exists.

func (*Server) GetSession

func (server *Server) GetSession(writer http.ResponseWriter, request *http.Request) (*sessions.Session, error)

GetSession returns the user session object.

func (*Server) GetUser

func (server *Server) GetUser(username string) User

GetUser returns the user with

func (*Server) HashPassword

func (server *Server) HashPassword(password string) ([]byte, error)

HashPassword hashes a users password

func (*Server) Serve

func (server *Server) Serve() error

Serve creates a http socket and serves the server.

func (*Server) SetMessage

func (server *Server) SetMessage(writer http.ResponseWriter, request *http.Request, message string, messageClass string) error

SetMessage sets the message for the next template call.

type StatusMessage

type StatusMessage struct {
	Message string
	Class   string
}

StatusMessage is a message to return to the requester via template.

func (StatusMessage) HTML

func (message StatusMessage) HTML() template.HTML

HTML returns the html encoded status message.

type Storage

type Storage struct {
	Path string

	Statements map[string]*sql.Stmt
	// contains filtered or unexported fields
}

Storage handles access to the storage.

func (*Storage) Add

func (storage *Storage) Add(recipe Recipe) error

Add adds a recipe to storage.

func (*Storage) AddUser

func (storage *Storage) AddUser(user User) error

AddUser adds a user to the database.

func (*Storage) Close

func (storage *Storage) Close()

Close closes the database connection.

func (*Storage) Connect

func (storage *Storage) Connect() (err error)

Connect connects to the database.

func (*Storage) Delete

func (storage *Storage) Delete(id uint64) error

Delete deletes a recipe.

func (*Storage) Get

func (storage *Storage) Get(id uint64) (Recipe, error)

Get returns the recipe with the given id.

func (*Storage) GetAllUsers

func (storage *Storage) GetAllUsers() ([]User, error)

GetAllUsers returns all users.

func (*Storage) GetOrCreateSessionCookieKey

func (storage *Storage) GetOrCreateSessionCookieKey() ([]byte, error)

GetOrCreateSessionCookieKey returns the session cookie key from database or creates it.

func (*Storage) GetRecipes

func (storage *Storage) GetRecipes() ([]Recipe, error)

GetRecipes returns all saved recipes.

func (*Storage) GetUser

func (storage *Storage) GetUser(username string) (User, error)

GetUser returns the user with the given username.

func (*Storage) SetPassword

func (storage *Storage) SetPassword(user User) error

SetPassword sets the password for a user.

func (*Storage) Update

func (storage *Storage) Update(recipe Recipe) error

Update updates a recipe.

type TemplateData

type TemplateData interface {
	Message() StatusMessage
	SetMessage(message StatusMessage)
}

TemplateData contains the map for templates data.

type User

type User struct {
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	// contains filtered or unexported fields
}

User contains

func (*User) Authenticate

func (user *User) Authenticate(username, password string) bool

Authenticate authenticate a user in

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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