Skip to content

Commit

Permalink
Use TOML for config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
HARUYAMA Seigo committed Jul 14, 2014
1 parent ed19499 commit 0c2a58f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ _testmain.go
*.exe
*.test

/config.json
/config.toml
*~
.*.swp
11 changes: 0 additions & 11 deletions config.json.example

This file was deleted.

12 changes: 12 additions & 0 deletions config.toml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[general]
public_path = "public"
template_path = "views"

[cookie]
mac_secret= "secret string which has over 256-bit entropy"

[database]
user = "root"
password = ""
hostname = ""
database = "goji"
9 changes: 5 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func main() {
filename := flag.String("config", "config.json", "Path to configuration file")
filename := flag.String("config", "config.toml", "Path to configuration file")

flag.Parse()
defer glog.Flush()
Expand All @@ -27,7 +27,8 @@ func main() {

// Setup static files
static := web.New()
static.Get("/assets/*", http.StripPrefix("/assets/", http.FileServer(http.Dir(application.Configuration.PublicPath))))
publicPath := application.Config.Get("general.public_path").(string)
static.Get("/assets/*", http.StripPrefix("/assets/", http.FileServer(http.Dir(publicPath))))

http.Handle("/assets/", static)

Expand All @@ -41,8 +42,8 @@ func main() {
controller := &controllers.MainController{}

// Couple of files - in the real world you would use nginx to serve them.
goji.Get("/robots.txt", http.FileServer(http.Dir(application.Configuration.PublicPath)))
goji.Get("/favicon.ico", http.FileServer(http.Dir(application.Configuration.PublicPath+"/images")))
goji.Get("/robots.txt", http.FileServer(http.Dir(publicPath)))
goji.Get("/favicon.ico", http.FileServer(http.Dir(publicPath+"/images")))

// Home page
goji.Get("/", application.Route(controller, "Index"))
Expand Down
38 changes: 16 additions & 22 deletions system/core.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package system

import (
"encoding/json"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand All @@ -17,42 +15,38 @@ import (
"github.com/golang/glog"
"github.com/gorilla/sessions"
"github.com/haruyama/golang-goji-sample/models"
"github.com/pelletier/go-toml"
"github.com/zenazn/goji/web"
)

type Application struct {
Configuration *Configuration
Template *template.Template
Store *sessions.CookieStore
DbMap *gorp.DbMap
Config *toml.TomlTree
Template *template.Template
Store *sessions.CookieStore
DbMap *gorp.DbMap
}

func (application *Application) Init(filename *string) {
data, err := ioutil.ReadFile(*filename)

config, err := toml.LoadFile(*filename)
if err != nil {
glog.Fatalf("Can't read configuration file: %s", err)
panic(err)
}

application.Configuration = &Configuration{}

err = json.Unmarshal(data, &application.Configuration)

if err != nil {
glog.Fatalf("Can't parse configuration file: %s", err)
panic(err)
glog.Fatalf("TOML load failed: %s\n", err)
}

hash := sha256.New()
hash.Write([]byte(application.Configuration.Secret))
hash.Write([]byte(config.Get("cookie.mac_secret").(string)))
application.Store = sessions.NewCookieStore(hash.Sum(nil))
application.Store.Options = &sessions.Options{
HttpOnly: true,
// Secure: true,
}
dbConfig := application.Configuration.Database
application.DbMap = models.GetDbMap(dbConfig.User, dbConfig.Password, dbConfig.Hostname, dbConfig.Database)
dbConfig := config.Get("database").(*toml.TomlTree)
application.DbMap = models.GetDbMap(dbConfig.Get("user").(string),
dbConfig.Get("password").(string),
dbConfig.Get("hostname").(string),
dbConfig.Get("database").(string))

application.Config = config
}

func (application *Application) LoadTemplates() error {
Expand All @@ -65,7 +59,7 @@ func (application *Application) LoadTemplates() error {
return nil
}

err := filepath.Walk(application.Configuration.TemplatePath, fn)
err := filepath.Walk(application.Config.Get("general.template_path").(string), fn)

if err != nil {
return err
Expand Down

0 comments on commit 0c2a58f

Please sign in to comment.