sqlx

package
v0.0.0-...-254fdf3 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2017 License: MIT Imports: 22 Imported by: 8

Documentation

Overview

Package sqlx is PostgreSQL and SQLite database access layer (DAL) for users and posts.

The package contains three files: * connection.go, which handles the actual database connection as singleton * posts.go, which handles CRUD methods for posts * users.go, which handles CRUD methods for users * email.go, which handles method for sending email to users * settings.go, which handles CU methods for settings

All methods defined this package should be implemented in other drivers as well, unless specifically said otherwise.

Index

Constants

This section is empty.

Variables

View Source
var Driver = flag.String("driver", "sqlite3", "Database driver to use (sqlite3, mysql, postgres)")
View Source
var RecoveryTemplate = `` /* 200-byte string literal not displayed */
View Source
var Source = flag.String("source", "vertigo.db", "Database data source")

Functions

func CompareHash

func CompareHash(digest []byte, password string) bool

CompareHash compares bcrypt password with a plaintext one. Returns true if passwords match and false if they do not.

func Drop

func Drop()

func GenerateHash

func GenerateHash(password string) ([]byte, error)

GenerateHash generates bcrypt hash from plaintext password

Types

type Email

type Email struct {
	Sender    string
	Host      string
	Recipient RecipientStruct
}

Email holds data of email sender and recipient for easier handling in templates.

type Post

type Post struct {
	ID         int64  `json:"id"`
	Title      string `json:"title" form:"title" binding:"required"`
	Content    string `json:"content"`
	Markdown   string `json:"markdown" form:"markdown"`
	Slug       string `json:"slug"`
	Author     int64  `json:"author"`
	Excerpt    string `json:"excerpt"`
	Viewcount  uint   `json:"viewcount"`
	Published  bool   `json:"-"`
	Created    int64  `json:"created"`
	Updated    int64  `json:"updated"`
	TimeOffset int    `json:"timeoffset"`
}

Post struct contains all relevant data when it comes to posts. Most fields are automatically filled when inserting new object into the database. JSON field after type refer to JSON key which martini will use to render data. Form field refers to frontend POST form `name` fields which martini uses to read data from. Binding defines whether the field is required when inserting or updating the object.

func (Post) Delete

func (post Post) Delete() error

Delete or post.Delete deletes a post according to post.Slug. Requires session cookie. Returns error object.

func (Post) Get

func (post Post) Get() (Post, error)

Get or user.Get returns user according to given user.Slug. Requires session session as a parameter. Returns Ad and error object.

func (Post) GetAll

func (post Post) GetAll() ([]Post, error)

GetAll or user.GetAll returns all user in database. Returns []User and error object.

func (Post) Increment

func (post Post) Increment()

Update or user.Update updates parameter "entry" with data given in parameter "user". Returns updated Ad object and an error object.

func (Post) Insert

func (post Post) Insert(user User) (Post, error)

Insert or post.Insert inserts Post object into database. Requires active session cookie Fills post.Author, post.Created, post.Edited, post.Excerpt, post.Slug and post.Published automatically. Returns Post and error object.

func (Post) Unpublish

func (post Post) Unpublish() error

func (Post) Update

func (post Post) Update(entry Post) (Post, error)

Update or post.Update updates parameter "entry" with data given in parameter "post". Requires active session cookie. Returns updated Post object and an error object.

type RecipientStruct

type RecipientStruct struct {
	ID          string
	Name        string
	Address     string
	RecoveryKey string
}

RecipientStruct holds data of email recipient for easier handling in templates.

type User

type User struct {
	ID       int64  `json:"id"`
	Name     string `json:"name" form:"name"`
	Password string `json:"password,omitempty" form:"password" sql:"-"`
	Recovery string `json:"-"`
	Digest   []byte `json:"-"`
	Email    string `json:"email" form:"email" binding:"required"`
	Posts    []Post `json:"posts"`
	Location string `json:"location" form:"location"`
}

User struct holds all relevant data for representing user accounts on Vertigo. A complete User struct also includes Posts field (type []Post) which includes all posts made by the user.

func (User) ExpireRecovery

func (user User) ExpireRecovery(t time.Duration)

ExpireRecovery or user.ExpireRecovery sets a TTL according to t to a recovery hash. This function is supposed to be run as goroutine to avoid blocking exection for t.

func (User) Get

func (user User) Get() (User, error)

Get or user.Get returns user according to given user.Slug. Requires session session as a parameter. Returns Ad and error object.

func (User) GetAll

func (user User) GetAll() ([]User, error)

GetAll or user.GetAll fetches all users with post data merged from the database.

func (User) GetByEmail

func (user User) GetByEmail() (User, error)

GetByEmail or user.GetByEmail returns User object according to given .Email with post information merged.

func (User) Insert

func (user User) Insert() (User, error)

Insert or user.Insert inserts a new User struct into the database. The function creates .Digest hash from .Password.

func (User) Login

func (user User) Login() (User, error)

Login or user.Login is a function which retrieves user according to given .Email field. The function then compares the retrieved object's .Digest field with given .Password field. If the .Password and .Digest match, the function returns the requested User struct, but with the .Password and .Digest omitted.

func (User) PasswordReset

func (user User) PasswordReset(entry User) (User, error)

func (User) Recover

func (user User) Recover() error

Recover or user.Recover is used to recover User's password according to user.Email The function will insert user.Recovery field with generated UUID string and dispatch an email to the corresponding user.Email address. It will also add TTL to Recovery field.

func (User) SendRecoveryEmail

func (user User) SendRecoveryEmail() error

SendRecoveryEmail dispatches predefined recovery email to recipient defined in parameters. Makes use of https://gist.github.com/andelf/5004821

func (User) Update

func (user User) Update(entry User) (User, error)

Update or user.Update updates data of "entry" parameter with the data received from "user". Can only used to update Name and Digest fields because of how user.Get works. Currently not used elsewhere than in password Recovery, that's why the Digest generation.

type Vertigo

type Vertigo struct {
	ID                 int    `json:"-,omitempty"`
	Name               string `json:"name" form:"name" binding:"required"`
	Hostname           string `json:"hostname" form:"hostname" binding:"required"`
	Firstrun           bool   `json:"firstrun,omitempty"`
	CookieHash         string `json:"cookiehash,omitempty"`
	AllowRegistrations bool   `json:"allowregistrations" form:"allowregistrations"`
	Description        string `json:"description" form:"description" binding:"required"`
	MailerLogin        string `json:"mailerlogin" form:"mailerlogin"`
	MailerPort         int    `json:"mailerport" form:"mailerport"`
	MailerPassword     string `json:"mailerpassword" form:"mailerpassword"`
	MailerHostname     string `json:"mailerhostname" form:"mailerhostname"`
}

Vertigo struct is used as a site wide settings structure. Firstrun and CookieHash are generated and controlled by the application and should not be rendered or made editable anywhere on the site.

var Settings *Vertigo

func VertigoSettings

func VertigoSettings() *Vertigo

VertigoSettings populates the global Settings object with data from database. If no records exist, it creates one.

func (Vertigo) Get

func (settings Vertigo) Get() (Vertigo, error)

Get or settings.Get returns settings saved to database. Returns Vertigo and error object.

func (Vertigo) Insert

func (settings Vertigo) Insert() (*Vertigo, error)

Insert or settings.Insert inserts Vertigo settings object into database. Fills settings.ID, settings.CookieHash and settings.FirstRun automatically. Returns *Vertigo and error object.

func (Vertigo) Update

func (settings Vertigo) Update() (*Vertigo, error)

Update or settings.Update writes changes made to global settings variable into database. Returns *Vertigo and an error object.

Jump to

Keyboard shortcuts

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