This commit is contained in:
Dan Ballard 2015-11-19 20:03:51 -08:00
parent 42795c6cef
commit ad765f5798
3 changed files with 22 additions and 23 deletions

17
main.go
View File

@ -83,12 +83,13 @@ func csrfSecret() string {
}
return string(bytes)
}
type CSRFErrorHandler struct {}
type CSRFErrorHandler struct{}
func (self CSRFErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println("csrf Failure: ")
fmt.Println(csrf.FailureReason(r))
fmt.Println("-----")
fmt.Println("csrf Failure: ")
fmt.Println(csrf.FailureReason(r))
fmt.Println("-----")
}
func main() {
@ -104,13 +105,13 @@ func main() {
initTemplates()
muxRouter := init_route_handlers()
//errHandler := csrf.ErrorHandler( CSRFErrorHandler{} )
// Terrible. TODO: Get SSL for prod, and then wrap in if(dev) { {
csrfSecurityOption := csrf.Secure(false)
fmt.Println("Listening on", config.Port, "...")
err := http.ListenAndServe(":"+config.Port, csrf.Protect([]byte(csrfSecret()), /*errHandler,*/ csrfSecurityOption)(muxRouter))
err := http.ListenAndServe(":"+config.Port, csrf.Protect([]byte(csrfSecret()) /*errHandler,*/, csrfSecurityOption)(muxRouter))
if err != nil {
fmt.Println("Fatal Error: ", err)
}

View File

@ -2,12 +2,12 @@ package news
import (
"database/sql"
"errors"
"fmt"
"github.com/dballard/transmet/categories"
_ "github.com/lib/pq"
"time"
"errors"
"strconv"
"time"
)
type News struct {
@ -72,7 +72,7 @@ func Delete(db *sql.DB, id int) error {
}
func Get(db *sql.DB, id int) (*News, error) {
rows, err := db.Query("SELECT " + SQL_NEWS_FIELDS + " FROM news WHERE id=$1", id)
rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE id=$1", id)
if err != nil {
fmt.Println("Error getting news (", id, "): ", err)
@ -94,7 +94,7 @@ func Get(db *sql.DB, id int) (*News, error) {
func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
categories.LoadCategories(db) // required by addContainer
rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news order by timestamp DESC")
rows, err := db.Query("SELECT " + SQL_NEWS_FIELDS + " FROM news order by timestamp DESC")
if err != nil {
fmt.Println("DB errpr reading LoadPage news: ", err)
return nil, 0, err
@ -108,7 +108,7 @@ func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
categories.LoadCategories(db) // required by addContainer
rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE exported is null order by category_id ASC")
rows, err := db.Query("SELECT " + SQL_NEWS_FIELDS + " FROM news WHERE exported is null order by category_id ASC")
if err != nil {
fmt.Println("DB errpr reading unexported news: ", err)
return nil, 0, err
@ -230,5 +230,3 @@ func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, err
return newsTree, count, nil
}

View File

@ -210,7 +210,7 @@ func editFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
}
func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
id, idErr := strconv.Atoi(mux.Vars(r)["id"])
id, idErr := strconv.Atoi(mux.Vars(r)["id"])
if idErr != nil {
session.AddFlash("Error trying to save news item", flash_err)
session.Save(r, w)
@ -430,30 +430,30 @@ func init_route_handlers() *mux.Router {
rGet := r.Methods("GET").Subrouter()
rPost := r.Methods("POST").Subrouter()
rGet.HandleFunc("/login", LoginFormHandler)
rPost.HandleFunc("/login", LoginPostHandler)
rPost.HandleFunc("/logout", userHandler(LogoutHandler))
rGet.HandleFunc("/news/add", userHandler(addFormHandler))
rPost.HandleFunc("/news/add", userHandler(addPostHandler))
rGet.HandleFunc("/", userHandler(newsFormHandler))
rGet.HandleFunc("/news", userHandler(newsFormHandler))
rGet.HandleFunc("/news/export", userHandler(templateFormHandler))
rPost.HandleFunc("/news/export", userHandler(exportHandler))
rGet.HandleFunc("/news/added", userHandler(addedHandler))
rPost.HandleFunc("/news/{id:[0-9]+}/delete", userHandler(deleteHandler))
rGet.HandleFunc("/news/{id:[0-9]+}/edit", userHandler(editFormHandler))
rPost.HandleFunc("/news/{id:[0-9]+}/edit", userHandler(editPostHandler))
rGet.HandleFunc("/categories", userHandler(categoriesFormHandler))
rPost.HandleFunc("/caegories", userHandler(categoriesPostHandler))
rPost.HandleFunc("/categories/{id:[0-9]+}/change-parent", userHandler(categoryChangeParentHandler))
rPost.HandleFunc("/categories/add", userHandler(categoryAddHandler))
rPost.HandleFunc("/categories/{id:[0-9]+}/delete", userHandler(categoryDeleteHandler))