otiai10
otiai10

Reputation: 4879

In revel.Controller, How to get port set in app.conf

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

Upvotes: 1

Views: 1004

Answers (2)

Ari Seyhun
Ari Seyhun

Reputation: 12521

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

Upvotes: 0

erthalion
erthalion

Reputation: 3244

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

Upvotes: 2

Related Questions