2015-04-27 15:31:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-29 15:25:48 +00:00
|
|
|
"database/sql"
|
2015-04-27 15:31:51 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-04-29 15:25:48 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2015-04-28 15:10:40 +00:00
|
|
|
_ "github.com/lib/pq"
|
2015-04-29 15:25:48 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2015-04-27 15:31:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const VERSION = "0.1"
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Sql struct {
|
|
|
|
Host string
|
|
|
|
Dbname string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
2015-04-29 15:25:48 +00:00
|
|
|
Port string
|
2015-04-27 15:31:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 15:25:48 +00:00
|
|
|
const (
|
|
|
|
flash_err = "_flash_err"
|
|
|
|
flash_info = "_flash_info"
|
|
|
|
)
|
|
|
|
|
2015-04-27 15:31:51 +00:00
|
|
|
var (
|
2015-04-29 15:25:48 +00:00
|
|
|
config Config
|
|
|
|
db *sql.DB
|
|
|
|
store = sessions.NewCookieStore([]byte("key-store-auth-secret"))
|
|
|
|
templates = map[string]*template.Template{}
|
2015-04-27 15:31:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2015-04-30 04:32:39 +00:00
|
|
|
err = decoder.Decode(&config)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error: cannot decode config file: ", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2015-04-27 15:31:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 15:10:40 +00:00
|
|
|
func dbConnect() {
|
2015-04-29 15:25:48 +00:00
|
|
|
var err error
|
2015-04-28 15:10:40 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-27 15:31:51 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("transmet ", VERSION)
|
|
|
|
fmt.Println("Loading...")
|
|
|
|
loadConfig()
|
2015-04-29 15:25:48 +00:00
|
|
|
dbConnect()
|
|
|
|
initTemplates()
|
|
|
|
init_route_handlers()
|
|
|
|
|
2015-05-01 15:20:02 +00:00
|
|
|
fmt.Println("Listening on", config.Port, "...")
|
2015-04-29 15:25:48 +00:00
|
|
|
err := http.ListenAndServe(":"+config.Port, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Fatal Error: ", err)
|
|
|
|
}
|
|
|
|
|
2015-04-28 15:10:40 +00:00
|
|
|
db.Close()
|
2015-04-27 15:31:51 +00:00
|
|
|
}
|