config

package module
v0.0.0-...-ee974bb Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2016 License: MIT Imports: 9 Imported by: 2

README

Config

GoDoc Build Status

Small struct types for resusable configurations in Go.

Happy hacking!

aodin, 2016

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = Config{
	Cookie:    DefaultCookie,
	Port:      8080,
	StaticURL: "/static/",
	Metadata:  Metadata{},
}

Default is a basic configuration with insecure values. It will return the Address localhost:8080

View Source
var DefaultCookie = Cookie{
	Age:      14 * 24 * time.Hour,
	Domain:   "",
	HttpOnly: false,
	Name:     "sessionid",
	Path:     "/",
	Secure:   false,
}

DefaultCookie is a default CookieConfig implementation. It expires after two weeks and is not very secure.

Functions

This section is empty.

Types

type Config

type Config struct {
	HTTPS       bool     `json:"https"`
	Domain      string   `json:"domain"`
	ProxyDomain string   `json:"proxy_domain"`
	Port        int      `json:"port"`
	ProxyPort   int      `json:"proxy_port"`
	TemplateDir string   `json:"templates"`
	AbsPath     string   `json:"abs_path"`
	MediaDir    string   `json:"media"`
	MediaURL    string   `json:"media_url"`
	StaticDir   string   `json:"static"`
	StaticURL   string   `json:"static_url"`
	SecretKey   string   `json:"secret_key"`
	Version     string   `json:"version"`
	Database    Database `json:"database"`
	Cookie      Cookie   `json:"cookie"`
	SMTP        SMTP     `json:"smtp"`
	Metadata    Metadata `json:"metadata"`
}

Config is the parent configuration struct and includes fields for single configurations of a database, cookie, and SMTP connection.

func Parse

func Parse() (Config, error)

Parse will create a Config using the file settings.json in the current directory.

func ParsePath

func ParsePath(filename string) (Config, error)

ParsePath will create a Config using the file at the given path.

func (Config) Address

func (c Config) Address() string

Address returns the domain:port pair.

func (Config) FullAddress

func (c Config) FullAddress() string

FullAddress returns the scheme, domain, port, and host - including proxy info

func (Config) StaticAddress

func (c Config) StaticAddress() string

StaticAddress adds the static URL to the full address TODO If staticURL is already a valid URL just return that

func (Config) URL

func (c Config) URL() (u *url.URL)

URL returns the domain:port scheme. Port is omitted if 80.

type Cookie struct {
	Age      time.Duration `json:"age"`
	Domain   string        `json:"domain"`
	HttpOnly bool          `json:"http_only"`
	Name     string        `json:"name"`
	Path     string        `json:"path"`
	Secure   bool          `json:"secure"`
}

Cookie contains the fields needed to set and retrieve cookies. Cookie names are valid tokens as defined by RFC 2616 section 2.2: http://tools.ietf.org/html/rfc2616#section-2.2 TL;DR: Any non-control or non-separator character.

func ParseCookiePath

func ParseCookiePath(path string) (c Cookie, err error)

ParseCookiePath will create a Cookie using the given filepath.

func (Cookie) Set

func (c Cookie) Set(w http.ResponseWriter, value string, expires time.Time)

Set calls http.SetCookie using the current Cookie config

type Database

type Database struct {
	Driver   string `json:"driver"`
	Host     string `json:"host"`
	Port     int64  `json:"port"`
	Name     string `json:"name"`
	User     string `json:"user"`
	Password string `json:"password"`
	SSLMode  string `json:"sslmode"`
}

Database contains the fields needed to connect to a database.

func ParseDatabasePath

func ParseDatabasePath(path string) (c Database, err error)

ParseDatabasePath will create a Database using the given filepath.

func ParseDatabaseURL

func ParseDatabaseURL(rawurl string) (db Database, err error)

ParseDatabaseURL will create a Database from the given raw URL

func (Database) Address

func (db Database) Address() string

Address returns a domain:port pair

func (Database) Credentials

func (db Database) Credentials() (string, string)

Credentials with return the driver and credentials appropriate for Go's sql.Open function as strings TODO Return the full address instead of a connection string?

func (Database) FullAddress

func (db Database) FullAddress() string

FullAddress returns the String output of the db URL

func (Database) URL

func (db Database) URL() (u *url.URL)

URL returns the db URL as a net.URL

type Metadata

type Metadata map[string]string

Metadata holds arbitrary strings as key - value pairs

func (Metadata) Get

func (m Metadata) Get(key string) string

Get returns the value of the given key. If the key does not exist in the metadata, a blank string will be returned

func (Metadata) Has

func (m Metadata) Has(key string) (exists bool)

Has returns true if the metadata contains the key. Keys with blank values will return true.

func (Metadata) Keys

func (m Metadata) Keys() []string

Keys returns all keys of the metadata

func (Metadata) Values

func (m Metadata) Values() []string

Values returns all values of the metadata

type SMTP

type SMTP struct {
	Port     int64  `json:"port"`
	User     string `json:"user"`
	Password string `json:"password"`
	Host     string `json:"host"`
	From     string `json:"from"`
	Alias    string `json:"alias"`
}

SMTP contains the fields needed to connect to a SMTP server.

func ParseSMTPPath

func ParseSMTPPath(path string) (c SMTP, err error)

ParseSMTPPath will create an SMTP using the given filepath.

func (SMTP) Address

func (c SMTP) Address() string

Address will return a string of the host and port separated by a colon.

func (SMTP) FromAddress

func (c SMTP) FromAddress() string

FromAddress creates a string suitable for use in an Email's From header.

Jump to

Keyboard shortcuts

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