示例#1
0
}

func (fs *assetFS) AssetNames() []string {
	return fs.assetNames()
}

func (fs *assetFS) HasAsset(requested string) (string, bool) {
	for _, filename := range fs.AssetNames() {
		if path.Base(filename) == requested {
			return filename, true
		}
	}
	return "", false
}

var AssetUnavailable = xrr.NewXrror("Asset %s unavailable").Out

func (fs *assetFS) HttpAsset(requested string) (http.File, error) {
	if hasasset, ok := fs.HasAsset(requested); ok {
		f, err := fs.Open(hasasset)
		return f, err
	}
	return nil, AssetUnavailable(requested)
}

func (fs *assetFS) Open(name string) (http.File, error) {
	name = path.Join(fs.prefix, name)
	if len(name) > 0 && name[0] == '/' {
		name = name[1:]
	}
	if children, err := fs.AssetDir(name); err == nil {
示例#2
0
func cstatic(a *App) error {
	StaticorInit(a)
	return nil
}

func cblueprints(a *App) error {
	for _, b := range a.Blueprints() {
		if !b.registered {
			b.Register(a)
		}
	}
	return nil
}

var IllegalMode = xrr.NewXrror("mode must be Development, Testing, or Production; not %s").Out

func Mode(mode string, value bool) Configuration {
	return func(a *App) error {
		m := strings.Title(mode)
		if existsIn(m, []string{"Development", "Testing", "Production"}) {
			err := a.SetMode(m, value)
			if err != nil {
				return err
			}
			return nil
		}
		return IllegalMode(mode)
	}
}
示例#3
0
				existing.Manage(rt)
			}
		}
	}
}

func (a *App) ExistingBlueprint(prefix string) (*Blueprint, bool) {
	for _, b := range a.Blueprints() {
		if b.Prefix == prefix {
			return b, true
		}
	}
	return nil, false
}

var AlreadyRegistered = xrr.NewXrror("only unregistered blueprints may be mounted; %s is already registered").Out

// Mount attaches each provided Blueprint to the given string mount point, optionally
// inheriting from and setting the app primary Blueprint as parent to the given Blueprints.
func (a *App) Mount(point string, blueprints ...*Blueprint) error {
	var b []*Blueprint
	for _, blueprint := range blueprints {
		if blueprint.registered {
			return AlreadyRegistered(blueprint.Prefix)
		}

		newprefix := filepath.ToSlash(filepath.Join(point, blueprint.Prefix))

		nbp := NewBlueprint(newprefix)
		nbp.Managers = a.combineManagers(blueprint.Managers)
示例#4
0
	if err != nil {
		return err
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	err = s.parse(reader, filename)
	return err
}

func (s Store) LoadConfByte(b []byte, name string) (err error) {
	reader := bufio.NewReader(bytes.NewReader(b))
	err = s.parse(reader, name)
	return err
}

var StoreParseError = xrr.NewXrror("Store configuration parsing: syntax error at '%s:%d'.").Out

func (s Store) parse(reader *bufio.Reader, filename string) (err error) {
	lineno := 0
	section := ""
	for err == nil {
		l, _, err := reader.ReadLine()
		if err != nil {
			break
		}
		lineno++
		if len(l) == 0 {
			continue
		}
		line := strings.TrimFunc(string(l), unicode.IsSpace)
		for line[len(line)-1] == '\\' {
示例#5
0
}

func isFunc(fn interface{}) bool {
	return reflect.ValueOf(fn).Kind() == reflect.Func
}

func equalFunc(a, b interface{}) bool {
	if !isFunc(a) || !isFunc(b) {
		panic("flotilla : funcEqual -- type error!")
	}
	av := reflect.ValueOf(&a).Elem()
	bv := reflect.ValueOf(&b).Elem()
	return av.InterfaceData() == bv.InterfaceData()
}

var NotAFunction = xrr.NewXrror("Provided (%+v, type: %T), but it is not a function").Out

var BadFunc = xrr.NewXrror("Cannot use function %q with %d results\nreturn must be 1 value, or 1 value and 1 error value").Out

func valueFunc(fn interface{}) reflect.Value {
	v := reflect.ValueOf(fn)
	if v.Kind() != reflect.Func {
		panic(NotAFunction(fn, fn))
	}
	if !goodFunc(v.Type()) {
		panic(BadFunc(fn, v.Type().NumOut()))
	}
	return v
}

func goodFunc(typ reflect.Type) bool {
示例#6
0
// ListTemplates  lists templates in the flotilla Loader.
func (fl *Loader) ListTemplates() interface{} {
	var ret []string
	for _, p := range fl.env.TemplateDirs() {
		files, _ := ioutil.ReadDir(p)
		for _, f := range files {
			if fl.ValidFileExtension(filepath.Ext(f.Name())) {
				ret = append(ret, fmt.Sprintf("%s/%s", p, f.Name()))
			}
		}
	}
	ret = append(ret, fl.AssetTemplates()...)
	return ret
}

var TemplateDoesNotExist = xrr.NewXrror("Template %s does not exist.").Out

// Load a template by string name from the flotilla Loader.
func (fl *Loader) Load(name string) (string, error) {
	for _, p := range fl.env.TemplateDirs() {
		f := filepath.Join(p, name)
		if fl.ValidFileExtension(filepath.Ext(f)) {
			// existing template dirs
			if _, err := os.Stat(f); err == nil {
				file, err := os.Open(f)
				r, err := ioutil.ReadAll(file)
				return string(r), err
			}
			// binary assets
			if r, err := fl.env.Assets.Get(name); err == nil {
				r, err := ioutil.ReadAll(r)
示例#7
0
文件: env.go 项目: maiconio/flotilla
		customstatus  map[int]*status
		mkctx         MakeCtxFunc
	}
)

func newEnv(a *App) *Env {
	e := &Env{Mode: defaultModes(), Store: defaultStore()}
	e.AddFxtensions(BuiltInExtensions(a)...)
	return e
}

func defaultModes() *Modes {
	return &Modes{true, false, false}
}

var SetModeError = xrr.NewXrror("env could not be set to %s").Out

// SetMode sets the provided Modes witht the provided boolean value.
// e.g. env.SetMode("Production", true)
func (env *Env) SetMode(mode string, value bool) error {
	m := reflect.ValueOf(env.Mode).Elem().FieldByName(mode)
	if m.CanSet() {
		m.SetBool(value)
		return nil
	}
	return SetModeError(mode)
}

// CurrentMode returns Modes specific to the App the provided Ctx is running within.
func CurrentMode(c Ctx) *Modes {
	m, _ := c.Call("mode")
示例#8
0
	ctxargs = append(ctxargs, e.ctx)
	ctxargs = append(ctxargs, args...)
	return call(e.ext[name], ctxargs...)
}

type Fxtension interface {
	Name() string
	Set(map[string]reflect.Value)
}

type fxtension struct {
	name string
	fns  map[string]interface{}
}

var InvalidExtension = xrr.NewXrror("%q is not a valid Fxtension.").Out

func validFxtension(fx interface{}) error {
	if _, valid := fx.(Fxtension); !valid {
		InvalidExtension(fx)
	}
	return nil
}

func MakeFxtension(name string, fns map[string]interface{}) *fxtension {
	return &fxtension{
		name: name,
		fns:  fns,
	}
}