展示框架中的相对路径

如何在revel框架中将实际文件导入revel basefolder。 目前,我正在执行以下操作以获取一些配置值。</ p>

 文件,错误:= ioutil.ReadFile(“ conf / config.conf”)
... \ n </ code> </ pre>

这导致我的服务器仅在以</ p>

  revel run开始启动狂欢时站在应用程序目录中时才能运行 myapp 
</ code> </ pre>

是否可以访问基本文件夹?</ p>
</ div>

展开原文

原文

How can import a file realtive to the revel basefolder in revel framework. Currently I do the following to get hold of some config values.

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

This results in my server only working if i stand in the app directory when starting revel with

revel run myapp

Is there a way to access the base folder?

doulao2128
doulao2128 我找到了revel.BasePath变量,但它似乎是空的。
5 年多之前 回复

2个回答



rev </ code> 包中,您可以使用以下任意一种:</ p>

  var(
// 应用详细信息
AppName字符串//例如“ sample”
BasePath字符串//例如“ / Users / revel / gocode / src / corp / sample”
AppPath字符串//例如“ / Users / revel / gocode / src / corp / sample / app“
ViewsPath字符串//例如” / Users / revel / gocode / src / corp / sample / app / views“
ImportPath字符串//例如” corp / sample“
SourcePath字符串//例如 “ / Users / revel / gocode / src”

// Revel安装详细信息
RevelPath字符串//例如,“ / Users / revel / gocode / src / revel”

//在哪里查找模板和 配置。
//按优先级排序(较早的路径优先于较新的路径。)
CodePaths [] string
ConfPaths [] string
TemplatePaths [] string

</ code> </ pre >

如果是空的哟 u,这很可能是因为您是从基本文件夹中启动应用的。</ p>

请注意,这些路径是由 Init(模式,importPath,srcPath字符串)</ code> 函数。 它的文档状态为:</ p>


  srcPath-包含Revel和应用程序的源目录的路径。
如果未指定(“”),则 必须运行Go安装。
</ code> </ pre>
</ blockquote>

还请检出:如何从代码和测试中引用相对文件 </ p>
</ div>

展开原文

原文

There are exported global variables in the revel package, you can use any of these:

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
)

If it is empty for you, that is most likely because you started your app from the base folder.

Note that these paths are set by the Init(mode, importPath, srcPath string) function. It's documentation states:

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

Also check out: how to reference a relative file from code and tests

dpj96988
dpj96988 非常感谢@icza
5 年多之前 回复

I use this method: In conf/app.conf add a line with the configuration path of this way:

projectname.path = "/foldersnames/"

and in the controller Write a method like this:

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")                                                                                                                                                                  
} 

You can build a helper with this code and take configurations variables from all applications.

You must call of this way:

info("projectname.path")
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐