This is a implementation of dim, which is built in python. But this time in go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

72 lines
1.5 KiB

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.conf", "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 'postgres'",
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("zone_create", zoneCreate)
s.Register("zone_list", zoneList)
http.HandleFunc("/dim", s.Handle)
log.Fatal(http.ListenAndServe(cfg.Listen, nil))
}