Documentation
¶
Overview ¶
Package cooking contains all the cooking server code.
Index ¶
- Constants
- Variables
- func SendError(writer http.ResponseWriter, request *http.Request, err error, errCode int)
- func SendTemplate(writer http.ResponseWriter, request *http.Request, server *Server, ...)
- type AddTemplateData
- type BaseTemplateData
- type Config
- type Freshness
- type HandleFunc
- type Recipe
- type RootTemplateData
- type Server
- func (server *Server) GetMessage(writer http.ResponseWriter, request *http.Request) (message StatusMessage, err error)
- func (server *Server) GetSession(writer http.ResponseWriter, request *http.Request) (*sessions.Session, error)
- func (server *Server) GetUser(username string) User
- func (server *Server) HashPassword(password string) ([]byte, error)
- func (server *Server) Serve() error
- func (server *Server) SetMessage(writer http.ResponseWriter, request *http.Request, message string, ...) error
- type StatusMessage
- type Storage
- func (storage *Storage) Add(recipe Recipe) error
- func (storage *Storage) AddUser(user User) error
- func (storage *Storage) Close()
- func (storage *Storage) Connect() (err error)
- func (storage *Storage) Delete(id uint64) error
- func (storage *Storage) Get(id uint64) (Recipe, error)
- func (storage *Storage) GetAllUsers() ([]User, error)
- func (storage *Storage) GetOrCreateSessionCookieKey() ([]byte, error)
- func (storage *Storage) GetRecipes() ([]Recipe, error)
- func (storage *Storage) GetUser(username string) (User, error)
- func (storage *Storage) SetPassword(user User) error
- func (storage *Storage) Update(recipe Recipe) error
- type TemplateData
- type User
Constants ¶
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) )
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" )
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 ¶
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) )
var ( // EmptyUser is the empty user EmptyUser = User{} )
Functions ¶
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 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 ¶
ParseFreshness parses the freshness value and adds it to the recipe.
func (*Recipe) SourceHTML ¶
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.
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 (*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) HashPassword ¶
HashPassword hashes a users password
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 ¶
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) GetAllUsers ¶
GetAllUsers returns all users.
func (*Storage) GetOrCreateSessionCookieKey ¶
GetOrCreateSessionCookieKey returns the session cookie key from database or creates it.
func (*Storage) GetRecipes ¶
GetRecipes returns all saved recipes.
func (*Storage) SetPassword ¶
SetPassword sets the password for a user.
type TemplateData ¶
type TemplateData interface { Message() StatusMessage SetMessage(message StatusMessage) }
TemplateData contains the map for templates data.