Revel
package revel
const (
REVEL_IMPORT_PATH = "github.com/robfig/revel"
)
func (r *revelLogs) Write(p []byte) (n int, err error) {
return r.w.Write([]byte(r.c.Paint(string(p))))
}
var (
AppName string
BasePath string
AppPath string
ViewsPath string
ImportPath string
SourcePath string
Config *MergedConfig
RunMode string
DevMode bool
RevelPath string
CodePaths []string
ConfPaths []string
TemplatePaths []string
Modules []Module
HttpPort int
HttpAddr string
HttpSsl bool
HttpSslCert string
HttpSslKey string
CookiePrefix string
CookieHttpOnly bool
CookieSecure bool
TemplateDelims string
TRACE = log.New(ioutil.Discard, "TRACE ", log.Ldate|log.Ltime|log.Lshortfile)
INFO = log.New(ioutil.Discard, "INFO ", log.Ldate|log.Ltime|log.Lshortfile)
WARN = log.New(ioutil.Discard, "WARN ", log.Ldate|log.Ltime|log.Lshortfile)
ERROR = log.New(&error_log, "ERROR ", log.Ldate|log.Ltime|log.Lshortfile)
Initialized bool
)
func Init(mode, importPath, srcPath string) {
ImportPath = strings.TrimRight(importPath, "/")
SourcePath = srcPath
RunMode = mode
if runtime.GOOS == "windows" {
gocolorize.SetPlain(true)
}
var revelSourcePath string
if SourcePath == "" {
revelSourcePath, SourcePath = findSrcPaths(importPath)
} else {
SourcePath = path.Clean(SourcePath)
revelSourcePath = SourcePath
packaged = true
}
RevelPath = path.Join(revelSourcePath, filepath.FromSlash(REVEL_IMPORT_PATH))
BasePath = path.Join(SourcePath, filepath.FromSlash(importPath))
AppPath = path.Join(BasePath, "app")
ViewsPath = path.Join(AppPath, "views")
CodePaths = []string{AppPath}
ConfPaths = []string{
path.Join(BasePath, "conf"),
path.Join(RevelPath, "conf"),
}
TemplatePaths = []string{
ViewsPath,
path.Join(RevelPath, "templates"),
}
var err error
Config, err = LoadConfig("app.conf")
if err != nil || Config == nil {
log.Fatalln("Failed to load app.conf:", err)
}
if mode == "" {
mode = config.DEFAULT_SECTION
}
if !Config.HasSection(mode) {
log.Fatalln("app.conf: No mode found:", mode)
}
Config.SetSection(mode)
DevMode = Config.BoolDefault("mode.dev", false)
HttpPort = Config.IntDefault("http.port", 9000)
HttpAddr = Config.StringDefault("http.addr", "")
HttpSsl = Config.BoolDefault("http.ssl", false)
HttpSslCert = Config.StringDefault("http.sslcert", "")
HttpSslKey = Config.StringDefault("http.sslkey", "")
if HttpSsl {
if HttpSslCert == "" {
log.Fatalln("No http.sslcert provided.")
}
if HttpSslKey == "" {
log.Fatalln("No http.sslkey provided.")
}
}
AppName = Config.StringDefault("app.name", "(not set)")
CookiePrefix = Config.StringDefault("cookie.prefix", "REVEL")
CookieHttpOnly = Config.BoolDefault("cookie.httponly", false)
CookieSecure = Config.BoolDefault("cookie.secure", false)
TemplateDelims = Config.StringDefault("template.delimiters", "")
if secretStr := Config.StringDefault("app.secret", ""); secretStr != "" {
secretKey = []byte(secretStr)
}
TRACE = getLogger("trace")
INFO = getLogger("info")
WARN = getLogger("warn")
ERROR = getLogger("error")
loadModules()
Initialized = true
}
type Module struct {
Name, ImportPath, Path string
}
func ResolveImportPath(importPath string) (string, error) {
if packaged {
return path.Join(SourcePath, importPath), nil
}
modPkg, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
return "", err
}
return modPkg.Dir, nil
}
func ModuleByName(name string) (m Module, found bool) {
for _, module := range Modules {
if module.Name == name {
return module, true
}
}
return Module{}, false
}
func CheckInit() {
if !Initialized {
panic("Revel has not been initialized!")
}
}