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.
 
 

87 lines
2.2 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"`
Debug bool `toml:"debug_mode"`
}
)
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, cfg.Debug)
if err != nil {
log.Fatalf("could not create server instance: %s", err)
return
}
s.Register("layer3domain_create", layer3DomainCreate)
s.Register("layer3domain_list", layer3DomainList)
s.Register("layer3domain_get_attr", layer3DomainGetAttr)
s.Register("layer3domain_set_attr", layer3DomainSetAttr)
s.Register("ipblock_create", containerCreate)
s.Register("ipblock_remove", containerDelete)
s.Register("ipblock_list", containerList)
s.Register("ipblock_set_attr", containerSetAttr)
s.Register("ipblock_get_attr", containerGetAttr)
s.Register("ippool_create", PoolCreate)
s.Register("ippool_delete", PoolDelete)
s.Register("ippool_list", PoolList)
s.Register("ippool_get_attr", PoolGetAttr)
s.Register("ippool_set_attr", PoolSetAttr)
s.Register("zone_create", zoneCreate)
s.Register("zone_list", zoneList)
http.HandleFunc("/dim", s.Handle)
log.Fatal(http.ListenAndServe(cfg.Listen, nil))
}