本文整理匯總了Golang中github.com/influx6/fractals.MustWrap函數的典型用法代碼示例。如果您正苦於以下問題:Golang MustWrap函數的具體用法?Golang MustWrap怎麽用?Golang MustWrap使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了MustWrap函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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
}
示例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
})
}
示例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)
}
})
}
示例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
})
}
示例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
})
}
示例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")
})
}
示例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
})
}
示例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
})
}
示例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
})
}
示例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
})
}
示例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
})
}
示例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
})
}
示例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
})
}
示例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
})
}
示例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
})
}