Question

Using revel framework (golang), how can I get port I set in app.conf?

// in app.conf
http.port=9090

And I have to use my server's port number in controllers (for example OAuth coding) such as

requestToken, url, err := TWITTER.GetRequestTokenAndUrl(
    "http://127.0.0.1:9090/Application/Authenticate",
)

I don't want to write port number by hard coding. Using revel, is there any good way to get port number (or any other conf values) set in app.conf?

Thank you in advance

Was it helpful?

Solution

May be this documentation will be useful - http://robfig.github.io/revel/docs/godoc/config.html

For example

// Load app.conf
var err error
Config, err = LoadConfig("app.conf")
if err != nil || Config == nil {
    log.Fatalln("Failed to load app.conf:", err)
}
HttpPort = Config.IntDefault("http.port", 9000)

An another approach may be used instead direct loading (see modules/db/app/db.go)

import (
    "github.com/robfig/revel"
)

if option, found = revel.Config.String("option"); !found {
    revel.ERROR.Fatal("No option found.")
}

OTHER TIPS

Updated Answer

In your file app/init.go you must retrieve config values within a custom-made function.

This custom made function must be registered in func init() with revel.OnAppStart(MyFunc).


If you attempt to retrieve config values directly from the init() func, you will recieve an error that looks something like this:

panic: runtime error: invalid memory address or nil pointer dereference
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top