func BindFS(L *lua.State) {
	luar.Register(L, "carbon", luar.Map{  
		"_physfs_mount":       physfs.Mount,
		"_physfs_exists":      physfs.Exists,
		"_physfs_isDir":       physfs.IsDirectory,
		"_physfs_getFS":       physfs.FileSystem,
		"_physfs_mkdir":       physfs.Mkdir,
		"_physfs_umount":      physfs.RemoveFromSearchPath,
		"_physfs_delete":      physfs.Delete,
		"_physfs_setWriteDir": physfs.SetWriteDir,
		"_physfs_getWriteDir": physfs.GetWriteDir,
		"_physfs_list": func(name string) (fl []string, err error) {
			if physfs.Exists(name) {
				if physfs.IsDirectory(name) {
					return physfs.EnumerateFiles(name)
				}
				return nil, errors.New("readdirent: not a directory")
			}
			return nil, errors.New("open " + name + ": no such file or directory")
		},
		"_physfs_readfile": func(filename string) (string, error) {
			if physfs.Exists(filename) {
				physfs_needfile(filename)
				f, err := physfs_openfile(filename)
				if err != nil {
					return "", err
				}
				defer physfs_closefile(filename)
				fi, err := f.Stat()
				if err != nil {
					return "", err
				}
				r := bufio.NewReader(f)
				buf := make([]byte, fi.Size())
				_, err = r.Read(buf)
				if err != nil {
					if err.Error() == "EOF" {  
						physfs_needfile(filename)
						f2, _ := physfs_openfile(filename)
						buf := bytes.NewBuffer(nil)
						io.Copy(buf, f2)
						physfs_closefile(filename)
						return string(buf.Bytes()), nil
					}
					return "", err
				}
				return string(buf), err
			}
			return "", errors.New(filename + ": No such file or directory")
		},
		"_physfs_readat": func(filename string, at, count int64) (string, error, int) {
			if physfs.Exists(filename) {
				f, err := physfs_openfile(filename)
				if err != nil {
					return "", err, -1
				}
				buf := make([]byte, count)
				f.Seek(at, os.SEEK_SET)
				read, _ := f.Read(buf)
				return string(buf[:read]), err, read
			}
			return "", errors.New(filename + ": No such file or directory"), -1
		},
		"_physfs_readn": func(filename string, count int64) (string, error, int) {
			if physfs.Exists(filename) {
				f, err := physfs_openfile(filename)
				if err != nil {
					return "", err, -1
				}
				buf := make([]byte, count)
				read, _ := f.Read(buf)
				return string(buf[:read]), err, read
			}
			return "", errors.New(filename + ": No such file or directory"), -1
		},
		"_physfs_modtime": func(name string) (int64, error) {
			mt, err := physfs.GetLastModTime(name)
			if err != nil {
				return -1, err
			}
			return mt.UTC().Unix(), nil
		},
		"_physfs_size": func(path string) (int64, error) {
			physfs_needfile(path)
			f, err := physfs_openfile(path)
			defer physfs_closefile(path)
			if err != nil {
				return -1, err
			}
			info, err := f.Stat()
			if err != nil {
				return -1, err
			}
			return info.Size(), nil
		},
		"_physfs_needfile": physfs_needfile,
		"_physfs_close":    physfs_closefile,
	})
	luar.Register(L, "carbon", luar.Map{
		"_filesystem_readfile": func(fs http.FileSystem, filename string) (string, error) {
			f, err := fs.Open(filename)
			if err != nil {
				return "", err
			}
			defer f.Close()
			fi, err := f.Stat()
			if err != nil {
				return "", err
			}
			r := bufio.NewReader(f)
			buf := make([]byte, fi.Size())
			_, err = r.Read(buf)
			if err != nil {
				if err.Error() == "EOF" {  
					f2, _ := fs.Open(filename)
					buf := bytes.NewBuffer(nil)
					io.Copy(buf, f2)
					f2.Close()
					return string(buf.Bytes()), nil
				}
				return "", err
			}
			return string(buf), err
		},
		"_filesystem_readat": func(fs http.FileSystem, filename string, at, count int64) (string, error, int) {
			f, err := fs.Open(filename)
			if err != nil {
				return "", err, -1
			}
			defer f.Close()
			buf := make([]byte, count)
			if s, ok := f.(io.Seeker); ok {
				s.Seek(at, os.SEEK_SET)
			} else {
				io.CopyN(ioutil.Discard, f, at)
			}
			read, _ := f.Read(buf)
			return string(buf[:read]), err, read
		},
		"_filesystem_exists": func(fs http.FileSystem, filename string) bool {
			f, err := fs.Open(filename)
			if err != nil {
				return false
			}
			f.Close()
			return true
		},
		"_filesystem_isdir": func(fs http.FileSystem, filename string) bool {
			f, err := fs.Open(filename)
			if err != nil {
				return false
			}
			defer f.Close()
			fi, err := f.Stat()
			if err != nil {
				return false
			}
			return fi.IsDir()
		},
		"_filesystem_modtime": func(fs http.FileSystem, filename string) (int64, error) {
			f, err := fs.Open(filename)
			if err != nil {
				return -1, err
			}
			fi, err := f.Stat()
			if err != nil {
				return -1, err
			}
			return fi.ModTime().UTC().Unix(), nil
		},
		"_filesystem_size": func(fs http.FileSystem, filename string) (int64, error) {
			f, err := fs.Open(filename)
			if err != nil {
				return -1, err
			}
			defer f.Close()
			fi, err := f.Stat()
			if err != nil {
				return -1, err
			}
			return fi.Size(), nil
		},
		"_filesystem_list": func(fs http.FileSystem, dirname string) ([]string, error) {
			f, err := fs.Open(dirname)
			if err != nil {
				return make([]string, 0), err
			}
			defer f.Close()
			fi, err := f.Stat()
			if err != nil {
				fmt.Println("Stat")
				return make([]string, 0), err
			}
			if fi.IsDir() == false {
				fmt.Println("isdir")
				return make([]string, 0), errors.New("dirname is not dir")
			}
			files, err := f.Readdir(-1)
			if err != nil {
				fmt.Println("readdir")
				return make([]string, 0), err
			}
			list := make([]string, len(files))
			for i := range files {
				list[i] = files[i].Name()
			}
			return list, nil
		},
	})
}
BindFS binds the physfs library and more generic http.FileSystem functions.
package middleware
import (
	"bufio"
	"bytes"
	"crypto/tls"
	"encoding/base64"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"math/rand"
	"mime"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"reflect"
	"regexp"
	"time"
	"github.com/carbonsrv/carbon/modules/glue"
	"github.com/carbonsrv/carbon/modules/helpers"
	"github.com/carbonsrv/carbon/modules/scheduler"
	"github.com/carbonsrv/carbon/modules/static"
	"github.com/vifino/golua/lua"
	"github.com/vifino/luar"
	"github.com/DeedleFake/Go-PhysicsFS/physfs"
	"github.com/GeertJohan/go.linenoise"
	"github.com/fzzy/radix/redis"
	"github.com/gin-gonic/gin"
	"github.com/mattn/go-runewidth"
	"github.com/nsf/termbox-go"
	"github.com/pmylund/go-cache"
	"github.com/shurcooL/github_flavored_markdown"
	"github.com/vifino/contrib/gzip"
	"database/sql"
	_ "github.com/cznic/ql/driver"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/lib/pq"
	_ "github.com/mattn/go-sqlite3"
	_ "github.com/vifino/go-yql"
)
var webroot string
func Bind(L *lua.State, root string) {
	webroot = root
	luar.Register(L, "var", luar.Map{  
		"root": root,
	})
	BindCarbon(L)
	BindMiddleware(L)
	BindRedis(L)
	BindSQL(L)
	BindKVStore(L)
	BindFS(L)
	BindIOEnhancements(L)
	BindOSEnhancements(L)
	BindThread(L)
	BindNet(L)
	BindConversions(L)
	BindMime(L)
	BindComs(L)
	BindPipe(L)
	BindExec(L)
	BindEncoding(L)
	BindMarkdown(L)
	BindLinenoise(L)
	BindTermbox(L)
	BindOther(L)
	BindRandomString(L)
}
func BindCarbon(L *lua.State) {
	luar.Register(L, "carbon", luar.Map{  
		"glue": glue.GetGlue,
	})
}
func BindEngine(L *lua.State) {
	luar.Register(L, "carbon", luar.Map{
		"_gin_new": gin.New,
	})
}
func BindMiddleware(L *lua.State) {
	luar.Register(L, "mw", luar.Map{
		"Logger":   gin.Logger,
		"Recovery": gin.Recovery,
		"Lua":       Lua,
		"DLR_NS":    DLR_NS,
		"DLR_RUS":   DLR_RUS,
		"DLRWS_NS":  DLRWS_NS,
		"DLRWS_RUS": DLRWS_RUS,
		"ExtRoute": (func(plan map[string]interface{}) func(*gin.Context) {
			newplan := make(Plan, len(plan))
			for k, v := range plan {
				newplan[k] = v.(func(*gin.Context))
			}
			return ExtRoute(newplan)
		}),
		"VHOST": (func(plan map[string]interface{}) func(*gin.Context) {
			newplan := make(Plan, len(plan))
			for k, v := range plan {
				newplan[k] = v.(func(*gin.Context))
			}
			return VHOST(newplan)
		}),
		"VHOST_Middleware": (func(plan map[string]interface{}) gin.HandlerFunc {
			newplan := make(Plan, len(plan))
			for k, v := range plan {
				newplan[k] = v.(gin.HandlerFunc)
			}
			return VHOST_Middleware(newplan)
		}),
		"if_regex":       If_Regexp,
		"if_written":     If_Written,
		"if_status":      If_Status,
		"if_not_regex":   If_Not_Regexp,
		"if_not_written": If_Not_Written,
		"if_not_status":  If_Not_Status,
		"GZip": func() func(*gin.Context) {
			return gzip.Gzip(gzip.DefaultCompression)
		},
		"Echo":     EchoHTML,
		"EchoText": Echo,
	})
	luar.Register(L, "carbon", luar.Map{
		"_mw_CGI":         CGI,          
		"_mw_CGI_Dynamic": CGI_Dynamic,  
		"_mw_combine": (func(middlewares []interface{}) func(*gin.Context) {  
			newmiddlewares := make([]func(*gin.Context), len(middlewares))
			for k, v := range middlewares {
				newmiddlewares[k] = v.(func(*gin.Context))
			}
			return Combine(newmiddlewares)
		}),
	})
	L.DoString(glue.RouteGlue())
}
func BindStatic(L *lua.State, cfe *cache.Cache) {
	luar.Register(L, "carbon", luar.Map{
		"_staticserve": (func(path, prefix string) func(*gin.Context) {
			return staticServe.ServeCached(prefix, staticServe.PhysFS(path, prefix, true, true), cfe)
		}),
	})
}
var physfs_fhandles = make(map[string]*physfs.File)
var physfs_fhandles_used = make(map[string]int)
func physfs_openfile(filename string) (*physfs.File, error) {
	fh, ok := physfs_fhandles[filename]
	if ok {
		return fh, nil
	}
	fh, err := physfs.Open(filename)
	if err != nil {
		physfs_fhandles[filename] = fh
		return fh, nil
	}
	return fh, err
}
func physfs_needfile(filename string) {
	physfs_fhandles_used[filename] += 1
}
func physfs_closefile(filename string) error {
	fh, ok := physfs_fhandles[filename]
	if ok {
		if physfs_fhandles_used[filename] == 1 {
			delete(physfs_fhandles, filename)
			delete(physfs_fhandles_used, filename)
			return fh.Close()
		}
	}
	return errors.New("File not opened.")
}