transmet/main.go

122 lines
2.6 KiB
Go
Raw Normal View History

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"
2015-09-13 21:57:22 +00:00
"flag"
2015-04-27 15:31:51 +00:00
"fmt"
"github.com/gorilla/csrf"
"github.com/gorilla/sessions"
_ "github.com/lib/pq"
2015-04-29 15:25:48 +00:00
"net/http"
"os"
2015-04-27 15:31:51 +00:00
)
2015-05-18 02:25:32 +00:00
const (
2015-09-13 21:57:22 +00:00
VERSION = "0.1"
2015-05-18 02:25:32 +00:00
MAX_DB_CONNS = 10 // already excessive for personal app
)
2015-04-27 15:31:51 +00:00
type Config struct {
Sql struct {
Host string
Dbname string
Username string
Password string
}
2015-04-29 15:25:48 +00:00
Port string
2015-09-13 21:57:22 +00:00
Url 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-09-13 21:57:22 +00:00
config Config
db *sql.DB
store = sessions.NewCookieStore([]byte("key-store-auth-secret"))
2015-04-27 15:31:51 +00:00
)
2015-05-14 14:20:30 +00:00
func loadConfig(env string) {
file, err := os.Open("config/" + env + ".json")
2015-04-27 15:31:51 +00:00
if err != nil {
2015-05-14 14:20:30 +00:00
fmt.Println("Error: cannot open config file: config/" + env + ".json")
2015-04-27 15:31:51 +00:00
os.Exit(-1)
}
defer file.Close()
2015-04-27 15:31:51 +00:00
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
}
func dbConnect() {
2015-04-29 15:25:48 +00:00
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)
}
2015-05-18 03:18:28 +00:00
db.SetMaxIdleConns(MAX_DB_CONNS)
err = db.Ping()
if err != nil {
fmt.Println("DB Error on Ping(): ", err)
os.Exit(-1)
}
}
func csrfSecret() string {
file, err := os.Open("csrf-secret.txt")
if err != nil {
fmt.Println("Error: cannot open csrf-secret.txt (run gen-csrf.sh to generate)")
os.Exit(-1)
}
defer file.Close()
var bytes []byte = make([]byte, 32)
n, err := file.Read(bytes)
if err != nil || n != 32 {
fmt.Println("Error: cannot open csrf-secret.txt (run gen-csrf.sh to generate)")
}
return string(bytes)
}
2015-11-20 04:03:51 +00:00
type CSRFErrorHandler struct{}
func (self CSRFErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2015-11-20 04:03:51 +00:00
fmt.Println("csrf Failure: ")
fmt.Println(csrf.FailureReason(r))
fmt.Println("-----")
}
2015-04-27 15:31:51 +00:00
func main() {
fmt.Println("transmet ", VERSION)
2015-09-13 21:57:22 +00:00
2015-05-14 14:20:30 +00:00
envFlag := flag.String("env", "local", "load config/{env}.json")
flag.Parse()
2015-04-27 15:31:51 +00:00
fmt.Println("Loading...")
2015-09-13 21:57:22 +00:00
2015-05-14 14:20:30 +00:00
loadConfig(*envFlag)
2015-04-29 15:25:48 +00:00
dbConnect()
initTemplates()
muxRouter := init_route_handlers()
errHandler := csrf.ErrorHandler( CSRFErrorHandler{} )
2015-11-20 04:03:51 +00:00
// Terrible. TODO: Get SSL for prod, and then wrap in if(dev) { {
csrfSecurityOption := csrf.Secure(false)
csrfMaxTimeOption := csrf.MaxAge(3600 * 24 * 3) // 3 Days - a little more wiggle room
2015-11-20 04:03:51 +00:00
2015-05-01 15:20:02 +00:00
fmt.Println("Listening on", config.Port, "...")
2015-11-20 04:03:51 +00:00
err := http.ListenAndServe(":"+config.Port, csrf.Protect([]byte(csrfSecret()), errHandler, csrfSecurityOption, csrfMaxTimeOption)(muxRouter))
2015-04-29 15:25:48 +00:00
if err != nil {
fmt.Println("Fatal Error: ", err)
}
db.Close()
2015-04-27 15:31:51 +00:00
}