package main import ( "encoding/json" "fmt" "os" "database/sql" _ "github.com/lib/pq" ) const VERSION = "0.1" type Config struct { Sql struct { Host string Dbname string Username string Password string } } var ( config Config db *sql.DB ) func loadConfig() { file, err := os.Open("config/local.json") if err != nil { fmt.Println("Error: cannot open config file") os.Exit(-1) } decoder := json.NewDecoder(file) decoder.Decode(&config) } func dbConnect() { var err error db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=require", config.Sql.Username, config.Sql.Password, config.Sql.Host, config.Sql.Dbname)) if err != nil { fmt.Println("DB ERROR: ", err) } err = db.Ping() if err != nil { fmt.Println("DB Error on Ping(): ", err) os.Exit(-1) } } func initTemplates() { files, _ := filepath.Glob("templates/pages/*.html") re := regexp.MustCompile("templates/pages/(.*).html") fmt.Println("Loading Templates:") for _, t := range files { name := re.FindStringSubmatch(t) fmt.Println(" ", name[1]) var err error templates[name[1]], err = template.ParseFiles("templates/layout.html", t) if err != nil { fmt.Println("Template load error: ", err) os.Exit(-1) } } } func main() { fmt.Println("transmet ", VERSION) fmt.Println("Loading...") loadConfig() db.Close() }