transmet/main.go

125 lines
2.8 KiB
Go
Raw Permalink Normal View History

2015-04-27 17:31:51 +02:00
package main
import (
2015-04-29 17:25:48 +02:00
"database/sql"
2015-04-27 17:31:51 +02:00
"encoding/json"
2015-09-13 23:57:22 +02:00
"flag"
2015-04-27 17:31:51 +02:00
"fmt"
"github.com/gorilla/csrf"
"github.com/gorilla/sessions"
_ "github.com/lib/pq"
2015-04-29 17:25:48 +02:00
"net/http"
"os"
2015-04-27 17:31:51 +02:00
)
2015-05-18 04:25:32 +02:00
const (
2015-09-13 23:57:22 +02:00
VERSION = "0.1"
2015-05-18 04:25:32 +02:00
MAX_DB_CONNS = 10 // already excessive for personal app
)
2015-04-27 17:31:51 +02:00
type Config struct {
Sql struct {
Host string
Dbname string
Username string
Password string
}
2015-04-29 17:25:48 +02:00
Port string
2015-09-13 23:57:22 +02:00
Url string
2015-04-27 17:31:51 +02:00
}
2015-04-29 17:25:48 +02:00
const (
flash_err = "_flash_err"
flash_info = "_flash_info"
)
2015-04-27 17:31:51 +02:00
var (
2015-09-13 23:57:22 +02:00
config Config
db *sql.DB
store = sessions.NewCookieStore([]byte("key-store-auth-secret"))
2015-04-27 17:31:51 +02:00
)
2015-05-14 16:20:30 +02:00
func loadConfig(env string) {
file, err := os.Open("config/" + env + ".json")
2015-04-27 17:31:51 +02:00
if err != nil {
2015-05-14 16:20:30 +02:00
fmt.Println("Error: cannot open config file: config/" + env + ".json")
2015-04-27 17:31:51 +02:00
os.Exit(-1)
}
defer file.Close()
2015-04-27 17:31:51 +02:00
decoder := json.NewDecoder(file)
2015-04-30 06:32:39 +02:00
err = decoder.Decode(&config)
if err != nil {
fmt.Println("Error: cannot decode config file: ", err)
os.Exit(-1)
}
2015-04-27 17:31:51 +02:00
}
func dbConnect() {
2015-04-29 17:25:48 +02: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 05:18:28 +02: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 05:03:51 +01:00
type CSRFErrorHandler struct{}
func (self CSRFErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2015-11-20 05:03:51 +01:00
fmt.Println("csrf Failure: ")
fmt.Println(csrf.FailureReason(r))
fmt.Println("-----")
}
2015-04-27 17:31:51 +02:00
func main() {
fmt.Println("transmet ", VERSION)
2015-09-13 23:57:22 +02:00
2015-05-14 16:20:30 +02:00
envFlag := flag.String("env", "local", "load config/{env}.json")
flag.Parse()
2015-04-27 17:31:51 +02:00
fmt.Println("Loading...")
2015-09-13 23:57:22 +02:00
2015-05-14 16:20:30 +02:00
loadConfig(*envFlag)
2015-04-29 17:25:48 +02:00
dbConnect()
initTemplates()
muxRouter := init_route_handlers()
2015-12-25 22:21:59 +01:00
//errHandler := csrf.ErrorHandler( CSRFErrorHandler{} )
2015-11-20 05:03:51 +01:00
// Terrible. TODO: Get SSL for prod, and then wrap in if(dev) { {
2015-12-25 22:21:59 +01:00
//csrfSecurityOption := csrf.Secure(false)
//csrfMaxTimeOption := csrf.MaxAge(3600 * 24 * 3) // 3 Days - a little more wiggle room
2015-11-20 05:03:51 +01:00
2015-05-01 17:20:02 +02:00
fmt.Println("Listening on", config.Port, "...")
2015-11-20 05:03:51 +01:00
2015-12-25 22:21:59 +01:00
// Disabled CSRF until SSL (and sorting why the popup is throwing CSRF errs
// for tor and FF with ublock + https everywhere)
//err := http.ListenAndServe(":"+config.Port, csrf.Protect([]byte(csrfSecret()), errHandler, csrfSecurityOption, csrfMaxTimeOption)(muxRouter))
err := http.ListenAndServe(":"+config.Port, muxRouter)
2015-04-29 17:25:48 +02:00
if err != nil {
fmt.Println("Fatal Error: ", err)
}
db.Close()
2015-04-27 17:31:51 +02:00
}