Пример #1
0
func newConfig(conf *config.URL) (*Cache, error) {
	cache := &Cache{
		Logger: log.Std,
	}

	if codecName := conf.Fragment.Get("codec"); codecName != "" {
		cache.codec = codec.Get(codecName)
		if cache.codec == nil {
			if imp := codec.RequiredImport(codecName); imp != "" {
				return nil, fmt.Errorf("please import %q to use the codec %q", imp, codecName)
			}
			return nil, fmt.Errorf("unknown codec %q, maybe you forgot an import?", codecName)
		}
	} else {
		cache.codec = codec.Get("gob")
	}

	cache.prefix = conf.Fragment.Get("prefix")
	cache.prefixLen = len(cache.prefix)
	if pipeName := conf.Fragment.Get("pipe"); pipeName != "" {
		cache.pipe = pipe.Get(pipeName)
		if cache.pipe == nil {
			if imp := pipe.RequiredImport(pipeName); imp != "" {
				return nil, fmt.Errorf("please import %q to use the pipe %q", imp, pipeName)
			}
			return nil, fmt.Errorf("unknown pipe %q, maybe you forgot an import?", pipeName)
		}
	}
	var opener driver.Opener
	if conf.Scheme != "" {
		opener = driver.Get(conf.Scheme)
		if opener == nil {
			if imp := imports[conf.Scheme]; imp != "" {
				return nil, fmt.Errorf("please import %q to use the cache driver %q", imp, conf.Scheme)
			}
			return nil, fmt.Errorf("unknown cache driver %q, maybe you forgot an import?", conf.Scheme)
		}
	} else {
		opener = driver.Get("dummy")
	}
	var err error
	if cache.driver, err = opener(conf); err != nil {
		return nil, err
	}
	return cache, nil
}
Пример #2
0
import (
	"encoding/gob"
	"errors"
	"net/http"
	"os"

	"gnd.la/app"
	"gnd.la/cache"
	"gnd.la/encoding/codec"
	"gnd.la/internal"
	"gnd.la/log"
)

var (
	fromLayer     = []string{"true"}
	layerCodec    = codec.Get("gob")
	errNoCache    = errors.New("nil cache passed to cache layer")
	errNoMediator = errors.New("nil mediator passed to cache layer")
	noCacheLayer  = os.Getenv("GONDOLA_NO_CACHE_LAYER") != ""
)

type cachedResponse struct {
	Header     http.Header
	StatusCode int
	Data       []byte
}

// Layer allows caching complete responses to requests.
// Use New to initialize a Layer.
type Layer struct {
	cache    *cache.Cache
Пример #3
0
	// Tried to use signed or encrypted cookies without a Signer.
	ErrNoSigner = errors.New("no signer specified")
	// Tried to use encrypted cookies without an Encrypter.
	ErrNoEncrypter = errors.New("no encrypter specified")

	errNoRequest = errors.New("no request available")

	// Maximum representable UNIX time with a signed 32 bit integer. This
	// means that cookies won't be really permanent, but they will expire
	// on January 19th 2038. I don't know about you, but I hope to be around
	// by that time, so hopefully I'll find a solution for this issue in the
	// next few years. See http://en.wikipedia.org/wiki/Year_2038_problem for
	// more information.
	Permanent      = time.Unix(2147483647, 0).UTC()
	deleteExpires  = time.Unix(0, 0).UTC()
	defaultCodec   = codec.Get("gob")
	cookieDefaults = &Options{Path: "/", Expires: Permanent}
)

// Options specify the default cookie Options used when setting
// a Cookie only by its name and value, like in Cookies.Set(),
// Cookies.SetSecure(), and Cookies.SetEncrypted().
//
// For more information about the cookie fields, see net/http.Cookie.
type Options struct {
	Path     string
	Domain   string
	Expires  time.Time
	MaxAge   int
	Secure   bool
	HttpOnly bool
Пример #4
0
func (o *Orm) fields(table string, s *structs.Struct) (*driver.Fields, map[string]*reference, error) {
	methods, err := driver.MakeMethods(s.Type)
	if err != nil {
		return nil, nil, err
	}
	fields := &driver.Fields{
		Struct:     s,
		PrimaryKey: -1,
		Methods:    methods,
	}
	var references map[string]*reference
	for ii, v := range s.QNames {
		// XXX: Check if this quoting is enough
		fields.QuotedNames = append(fields.QuotedNames, fmt.Sprintf("\"%s\".\"%s\"", table, s.MNames[ii]))
		t := s.Types[ii]
		ftag := s.Tags[ii]
		// Check encoded types
		if cn := ftag.CodecName(); cn != "" {
			if codec.Get(cn) == nil {
				if imp := codec.RequiredImport(cn); imp != "" {
					return nil, nil, fmt.Errorf("please import %q to use the codec %q", imp, cn)
				}
				return nil, nil, fmt.Errorf("can't find codec %q. Perhaps you missed an import?", cn)
			}
		} else {
			switch t.Kind() {
			case reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map:
				return nil, nil, fmt.Errorf("field %q in struct %s has invalid type %s", v, s.Type, t)
			}
		}
		if pn := ftag.PipeName(); pn != "" {
			// Check if the field has a codec and the pipe exists
			if ftag.CodecName() == "" {
				return nil, nil, fmt.Errorf("field %q has pipe %s but no codec - only encoded types can use pipes", v, pn)
			}
			if pipe.FromTag(ftag) == nil {
				return nil, nil, fmt.Errorf("can't find ORM pipe %q. Perhaps you missed an import?", pn)
			}
		}
		// Struct has flattened types, but we need to original type
		// to determine if it should be nullempty or omitempty by default
		field := s.Type.FieldByIndex(s.Indexes[ii])
		fields.OmitEmpty = append(fields.OmitEmpty, ftag.Has("omitempty") || (defaultsToOmitEmpty(field.Type, ftag) && !ftag.Has("notomitempty")))
		fields.NullEmpty = append(fields.NullEmpty, ftag.Has("nullempty") || (defaultsToNullEmpty(field.Type, ftag) && !ftag.Has("notnullempty")))
		if ftag.Has("primary_key") {
			if fields.PrimaryKey >= 0 {
				return nil, nil, fmt.Errorf("duplicate primary_key in struct %v (%s and %s)", s.Type, s.QNames[fields.PrimaryKey], v)
			}
			fields.PrimaryKey = ii
		}
		if ftag.Has("auto_increment") {
			if k := types.Kind(t.Kind()); k != types.Int && k != types.Uint {
				return nil, nil, fmt.Errorf("auto_increment field %q in struct %s must be of integer type (signed or unsigned", v, s.Type)
			}
			fields.AutoincrementPk = fields.PrimaryKey == ii
		}
		if ref := ftag.Value("references"); ref != "" {
			m := referencesRe.FindStringSubmatch(ref)
			if len(m) != 4 {
				return nil, nil, fmt.Errorf("field %q has invalid references %q. Must be in the form references=Model or references=Model(Field)", v, ref)
			}
			if references == nil {
				references = make(map[string]*reference)
			}
			references[v] = &reference{model: m[1], field: m[3]}
		}
	}
	if err := o.setFieldsDefaults(fields); err != nil {
		return nil, nil, err
	}
	return fields, references, nil
}