package main import ( "database/sql" "flag" "log" "net/http" "strings" _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" "github.com/pelletier/go-toml" ) var ( configPath = flag.String("config", "dim.toml", "path to the config file") ) type ( Config struct { Listen string `toml:"listen"` DB struct { Type string `toml:"type"` Connection string `toml:"conn"` } `toml:"db"` } ) func main() { flag.Parse() cfg := Config{} raw, err := toml.LoadFile(*configPath) if err != nil { log.Fatalf("could not load config file '%s': %s", *configPath, err) return } if err := raw.Unmarshal(&cfg); err != nil { log.Fatalf("could not parse config file '%s': %s", *configPath, err) return } if cfg.DB.Type != "mysql" && cfg.DB.Type != "postgres" { log.Fatalf( "unknown database type '%s' in config '%s'. Allowed is 'mysql' and 'postgresql'", cfg.DB.Type, *configPath, ) return } if strings.Trim(cfg.DB.Connection, " \t\n\r") == "" { log.Fatalf("no database connection string in config '%s' set", *configPath) return } db, err := sql.Open(cfg.DB.Type, cfg.DB.Connection) if err != nil { log.Fatalf("could not open database connection: %s", err) return } s, err := NewServer(db) if err != nil { log.Fatalf("could not create server instance: %s", err) return } s.Register("foobar", func(c *Context, req Request, res *Response) error { c.Logf(LevelInfo, "I received a request!") res.AddMessage(LevelInfo, "Is this working?") res.Result["foo"] = "seems so" return nil }) http.HandleFunc("/dim", s.Handle) log.Fatal(http.ListenAndServe(cfg.Listen, nil)) }