furl - options.go
1 package furl
2
3 import (
4 "math/rand"
5 "net/http"
6 "net/url"
7 )
8
9 // The Option type is used to specify optional params to the New function call
10 type Option func(*Furl)
11
12 // The URLValidator Option allows a Furl instance to validate URLs against a
13 // custom set of criteria.
14 //
15 // If the passed function returns false the URL passed to it will be considered
16 // invalid and will not be stored and not be assigned a key.
17 func URLValidator(fn func(url string) bool) Option {
18 return func(f *Furl) {
19 f.urlValidator = fn
20 }
21 }
22
23 // The HTTPURL function can be used with URLValidator to set a simple URL
24 // checker that will check for either an http or https scheme, a hostname and
25 // no user credentials.
26 func HTTPURL(uri string) bool {
27 u, err := url.Parse(uri)
28 if err != nil {
29 return false
30 }
31 return (u.Scheme == "http" || u.Scheme == "https") && u.Hostname() != "" && u.User == nil
32 }
33
34 // The KeyValidator Option allows a Furl instance to validate both generated
35 // and suggested keys against a set of custom criteria.
36 //
37 // If the passed function returns false the Key passed to it will be considered
38 // invalid and will either generate a new one, if it was generated to begin
39 // with, or simply reject the suggested key.
40 func KeyValidator(fn func(key string) bool) Option {
41 return func(f *Furl) {
42 f.keyValidator = fn
43 }
44 }
45
46 // The KeyLength Option sets the minimum key length on a Furl instance.
47 //
48 // NB: The key length is the length of the generated key before base64
49 // encoding, which will increase the size. The actual key length will be
50 // the result of base64.RawURLEncoding.EncodedLen(length).
51 func KeyLength(length uint) Option {
52 return func(f *Furl) {
53 f.keyLength = length
54 }
55 }
56
57 // The CollisionRetries Option sets how many tries a Furl instance will retry
58 // generating keys at a given length before increasing the length in order to
59 // find a unique key.
60 func CollisionRetries(retries uint) Option {
61 return func(f *Furl) {
62 f.retries = retries
63 }
64 }
65
66 // The SetStore options allows for setting both starting data and the options to
67 // persist the collected data. See the Store interface and NewStore function for
68 // more information about Stores.
69 func SetStore(s Store) Option {
70 return func(f *Furl) {
71 f.store = s
72 }
73 }
74
75 // The RandomSource Option allows the specifying of a custom source of
76 // randomness.
77 func RandomSource(source rand.Source) Option {
78 return func(f *Furl) {
79 f.rand = rand.New(source)
80 }
81 }
82
83 // The Index Option allows for custom error and success output.
84 //
85 // For a POST request with code http.StatusOK (200), the output will be the
86 // generated or specified key. In all other times, the output is the error
87 // string corresponding to the error code.
88 //
89 // NB: The index function won't be called for JSON, XML, or Text POST requests.
90 func Index(index func(w http.ResponseWriter, r *http.Request, code int, output string)) Option {
91 return func(f *Furl) {
92 f.index = index
93 }
94 }
95