【问题标题】:Relative paths in revel frameworkrevel 框架中的相对路径
【发布时间】:2015-07-07 09:42:05
【问题描述】:

如何将文件真正导入 revel 框架中的 revel 基本文件夹。目前我执行以下操作来获取一些配置值。

file, err := ioutil.ReadFile("conf/config.conf")
...

这导致我的服务器只有在我开始狂欢时站在应用程序目录中才能工作

revel run myapp

有没有办法访问基础文件夹?

【问题讨论】:

  • 我找到了 revel.BasePath 变量,但它似乎是空的。

标签: go revel


【解决方案1】:

revel 包中有导出的全局变量,你可以使用其中任何一个:

var (
    // App details
    AppName    string // e.g. "sample"
    BasePath   string // e.g. "/Users/revel/gocode/src/corp/sample"
    AppPath    string // e.g. "/Users/revel/gocode/src/corp/sample/app"
    ViewsPath  string // e.g. "/Users/revel/gocode/src/corp/sample/app/views"
    ImportPath string // e.g. "corp/sample"
    SourcePath string // e.g. "/Users/revel/gocode/src"

    // Revel installation details
    RevelPath string // e.g. "/Users/revel/gocode/src/revel"

    // Where to look for templates and configuration.
    // Ordered by priority.  (Earlier paths take precedence over later paths.)
    CodePaths     []string
    ConfPaths     []string
    TemplatePaths []string
)

如果它对您来说是空的,那很可能是因为您从基本文件夹启动了您的应用程序。

请注意,这些路径由Init(mode, importPath, srcPath string) 函数设置。它的文档指出:

srcPath - the path to the source directory, containing Revel and the app.
  If not specified (""), then a functioning Go installation is required.

另请查看:how to reference a relative file from code and tests

【讨论】:

    【解决方案2】:

    我使用这种方法: 在 conf/app.conf 中添加一行这样的配置路径:

    projectname.path = "/foldersnames/"

    并在控制器中写一个这样的方法:

    func info(field string) string {                                                                                                                                                                               
      config, err := revel.LoadConfig("app.conf")                                                                                                                                                                  
      if err != nil || config == nil {                                                                                                                                                                             
        log.Fatalln("Failed to load configuration file", err)                                                                                                                                                      
       }                                                                                                                                                                                                            
      return config.StringDefault(field, "empty")                                                                                                                                                                  
    } 
    

    您可以使用此代码构建帮助程序并从所有应用程序中获取配置变量。

    你必须这样调用:

    info("projectname.path")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 2015-06-23
      相关资源
      最近更新 更多