• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang fractals.MustWrap函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/influx6/fractals.MustWrap函数的典型用法代码示例。如果您正苦于以下问题:Golang MustWrap函数的具体用法?Golang MustWrap怎么用?Golang MustWrap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了MustWrap函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: WrapForMW

// WrapForMW takes a giving interface and asserts it into a DriveMiddleware or
// wraps it if needed, returning the middleware.
func WrapForMW(wm interface{}) DriveMiddleware {
	var localWM DriveMiddleware

	switch wm.(type) {
	case func(context.Context, error, interface{}) (interface{}, error):
		localWM = WrapMiddleware(wm.(func(context.Context, error, interface{}) (interface{}, error)))
	case []func(context.Context, error, interface{}) (interface{}, error):
		fm := wm.([]fractals.Handler)
		localWM = WrapMiddleware(fm...)
	case func(interface{}) fractals.Handler:
		localWM = WrapMiddleware(wm.(func(interface{}) fractals.Handler)(fractals.IdentityHandler()))
	case func(context.Context, *Request) error:
		elx := wm.(func(context.Context, *Request) error)
		localWM = func(ctx context.Context, rw *Request) (*Request, error) {
			if err := elx(ctx, rw); err != nil {
				return nil, err
			}

			return rw, nil
		}
	case func(ctx context.Context, rw *Request) (*Request, error):
		localWM = wm.(func(ctx context.Context, rw *Request) (*Request, error))
	default:
		mws := fractals.MustWrap(wm)
		localWM = WrapMiddleware(mws)
	}

	return localWM
}
开发者ID:influx6,项目名称:gu,代码行数:31,代码来源:servers.go


示例2: MimeWriterFor

// MimeWriterFor writes the mimetype for the provided file depending on the
// extension of the file.
func MimeWriterFor(file string) fractals.Handler {
	return fractals.MustWrap(func(rw *Request) *Request {
		ctn := mimes.GetByExtensionName(filepath.Ext(file))
		rw.Res.Header().Add("Content-Type", ctn)
		return rw
	})
}
开发者ID:influx6,项目名称:gu,代码行数:9,代码来源:middlewares.go


示例3: Headers

// Headers returns a fractals.Handler which hads the provided values into the
// response headers.
func Headers(h map[string]string) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, wm *Request) {
		for key, value := range h {
			wm.Res.Header().Set(key, value)
		}
	})
}
开发者ID:influx6,项目名称:gu,代码行数:9,代码来源:http.go


示例4: WalkDir

// WalkDir walks the giving path if indeed is a directory, else passing down
// an error down the provided pipeline. It extends the provided os.FileInfo
// with a structure that implements the ExtendedFileInfo interface. It sends the
// individual fileInfo instead of the slice of FileInfos.
func WalkDir(path string) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, _ interface{}) ([]ExtendedFileInfo, error) {
		file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE, 0700)
		if err != nil {
			return nil, err
		}

		defer file.Close()

		fdirs, err := file.Readdir(-1)
		if err != nil {
			return nil, err
		}

		var dirs []ExtendedFileInfo

		for _, dir := range fdirs {
			dirInfo := NewExtendedFileInfo(dir, path)

			// If this is a sysmbol link, then continue we won't read through it.
			if _, err := os.Readlink(dirInfo.Dir()); err == nil {
				continue
			}

			dirs = append(dirs, dirInfo)
		}

		return dirs, nil
	})

}
开发者ID:influx6,项目名称:gu,代码行数:35,代码来源:fs.go


示例5: ResolvePathIn

// ResolvePathIn returns an ExtendedFileInfo for paths recieved if they match
// a specific root directory once resolved using the root directory.
func ResolvePathIn(rootDir string) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, path string) (ExtendedFileInfo, error) {
		absRoot, err := filepath.Abs(rootDir)
		if err != nil {
			return nil, err
		}

		rootPath := filepath.Clean(absRoot)
		finalPath := filepath.Clean(filepath.Join(rootDir, path))

		if !strings.Contains(finalPath, rootPath) {
			return nil, fmt.Errorf("Path is outside of root {Root: %q, Path: %q, Wanted: %q}", rootDir, path, finalPath)
		}

		file, err := os.Open(finalPath)
		if err != nil {
			return nil, err
		}

		stat, err := file.Stat()
		if err != nil {
			return nil, err
		}

		return NewExtendedFileInfo(stat, filepath.Base(finalPath)), nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:29,代码来源:fs.go


示例6: CORS

// CORS setup a generic CORS hader within the response for recieved request response.
func CORS() fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, wm *Request) {
		wm.Res.Header().Set("Access-Control-Allow-Origin", "*")
		wm.Res.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
		wm.Res.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
		wm.Res.Header().Set("Access-Control-Max-Age", "86400")
	})
}
开发者ID:influx6,项目名称:gu,代码行数:9,代码来源:http.go


示例7: Close

// Close expects to receive a closer in its pipeline and closest the closer.
func Close() fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, w io.Closer) error {
		if err := w.Close(); err != nil {
			return err
		}

		return nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:10,代码来源:fs.go


示例8: RemoveAll

// RemoveAll deletes the giving path and its subpaths if its a directory
// and passes the path down
// the pipeline.
func RemoveAll(path string) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, _ interface{}) error {
		if err := os.RemoveAll(path); err != nil {
			return err
		}

		return nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:12,代码来源:fs.go


示例9: ReadReader

// ReadReader reads the data pulled from the received reader from the
// pipeline.
func ReadReader() fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, r io.Reader) ([]byte, error) {
		var buf bytes.Buffer
		_, err := io.Copy(&buf, r)
		if err != nil && err != io.EOF {
			return nil, err
		}

		return buf.Bytes(), nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:13,代码来源:fs.go


示例10: OpenFile

// OpenFile creates the giving file within the provided directly and
// writes the any recieved data into the file. It sends the file Handler,
// down the piepline.
func OpenFile(path string) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, _ interface{}) (*os.File, error) {
		file, err := os.Open(path)
		if err != nil {
			return nil, err
		}

		return file, nil
	})

}
开发者ID:influx6,项目名称:gu,代码行数:14,代码来源:fs.go


示例11: JSONEncoder

// JSONEncoder encodes the data it recieves into JSON and returns the values.
func JSONEncoder() fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, data interface{}) ([]byte, error) {
		var d bytes.Buffer

		if err := json.NewEncoder(&d).Encode(data); err != nil {
			return nil, err
		}

		return d.Bytes(), nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:12,代码来源:http.go


示例12: UnwrapStats

// UnwrapStats takes the provided ExtendedFileInfo and unwraps them into their
// full path, allows you to retrieve the strings path.
func UnwrapStats() fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, info []ExtendedFileInfo) []string {
		var dirs []string

		for _, dir := range info {
			dirs = append(dirs, dir.Path())
		}

		return dirs
	})
}
开发者ID:influx6,项目名称:gu,代码行数:13,代码来源:fs.go


示例13: LogWith

// LogWith returns a Handler which logs to the provided Writer details of the
// http request.
func LogWith(w io.Writer) fractals.Handler {
	return fractals.MustWrap(func(rw *Request) *Request {
		now := time.Now().UTC()
		content := rw.Req.Header.Get("Accept")

		if !rw.Res.StatusWritten() {
			fmt.Fprintf(w, "HTTP : %q : Content{%s} : Method{%s} : URI{%s}\n", now, content, rw.Req.Method, rw.Req.URL)
		} else {
			fmt.Fprintf(w, "HTTP : %q : Status{%d} : Content{%s} : Method{%s} : URI{%s}\n", now, rw.Res.Status(), rw.Res.Header().Get("Content-Type"), rw.Req.Method, rw.Req.URL)
		}
		return rw
	})
}
开发者ID:influx6,项目名称:gu,代码行数:15,代码来源:middlewares.go


示例14: Mkdir

// Mkdir creates a directly returning the path down the pipeline. If the chain
// flag is on, then mkdir when it's pipeline receives a non-empty string as
// an argument, will join the string recieved with the path provided.
// This allows chaining mkdir paths down the pipeline.
func Mkdir(path string, chain bool) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, root string) error {
		if chain && root != "" {
			path = filepath.Join(root, path)
		}

		if err := os.MkdirAll(path, 0700); err != nil {
			return err
		}

		return nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:17,代码来源:fs.go


示例15: JSONDecoder

// JSONDecoder decodes the data it recieves into an map type and returns the values.
func JSONDecoder() fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, data []byte) (map[string]interface{}, error) {
		ms := make(map[string]interface{})

		var b bytes.Buffer
		b.Write(data)

		if err := json.NewDecoder(&b).Decode(&ms); err != nil {
			return nil, err
		}

		return ms, nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:15,代码来源:http.go


示例16: SkipStat

// SkipStat takes a function to filter out the FileInfo that are running through
// its pipeline. This allows you to define specific file paths you wish to treat.
// If the filter function returns true, then any FileInfo/ExtendedFileInfo that
// match its criteria are sent down its pipeline.
func SkipStat(filter func(ExtendedFileInfo) bool) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, info []ExtendedFileInfo) []ExtendedFileInfo {
		var filtered []ExtendedFileInfo

		for _, dir := range info {
			if !filter(dir) {
				continue
			}
			filtered = append(filtered, dir)
		}

		return filtered
	})
}
开发者ID:influx6,项目名称:gu,代码行数:18,代码来源:fs.go


示例17: WriteWriter

// WriteWriter expects to recieve []bytes as input and writes the provided
// bytes into the writer it recieves as argument. It returns error if the total
// written does not match the size of the bytes. It passes the incoming data
// down the pipeline.
func WriteWriter(w io.Writer) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, data []byte) error {
		written, err := w.Write(data)
		if err != nil {
			return err
		}

		if written != len(data) {
			return errors.New("Data written is not matching provided data")
		}

		return nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:18,代码来源:fs.go


示例18: CreateFile

// CreateFile creates the giving file within the provided directly and sends
// out the file handler.
func CreateFile(path string, useRoot bool) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, root string) (*os.File, error) {
		if useRoot && root != "" {
			path = filepath.Join(root, path)
		}

		file, err := os.Create(path)
		if err != nil {
			return nil, err
		}

		return file, nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:16,代码来源:fs.go


示例19: MkFile

// MkFile either creates or opens an existing file for appending. It passes
// the file object for this files down its pipeline. If it gets a string from
// the pipeline, it uses that string if not empty as its root path.
func MkFile(path string, useRoot bool) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, root string) (*os.File, error) {
		if useRoot && root != "" {
			path = filepath.Join(root, path)
		}

		file, err := os.OpenFile(path, os.O_APPEND|os.O_RDWR, os.ModeAppend)
		if err != nil {
			return nil, err
		}

		return file, nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:17,代码来源:fs.go


示例20: ResolvePathStringIn

// ResolvePathStringIn returns the full valid path for paths recieved if they match
// a specific root directory once resolved using the root directory.
func ResolvePathStringIn(rootDir string) fractals.Handler {
	return fractals.MustWrap(func(ctx context.Context, path string) (string, error) {
		absRoot, err := filepath.Abs(rootDir)
		if err != nil {
			return "", err
		}

		rootPath := filepath.Clean(absRoot)
		finalPath := filepath.Clean(filepath.Join(rootDir, path))

		if !strings.Contains(finalPath, rootPath) {
			return "", fmt.Errorf("Path is outside of root {Root: %q, Path: %q, Wanted: %q}", rootDir, path, finalPath)
		}

		return finalPath, nil
	})
}
开发者ID:influx6,项目名称:gu,代码行数:19,代码来源:fs.go



注:本文中的github.com/influx6/fractals.MustWrap函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang config.UnmarshalTable函数代码示例发布时间:2022-05-28
下一篇:
Golang flux.Reactive函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap