Moved Use() to controllers from middleware for cleaner usage (I'll consider moving it back if it doesn't logically make sense)

Renamed Base_Campaigns to Campaigns
This commit is contained in:
Jordan 2014-01-10 22:11:44 -06:00
parent 61ef18b3b4
commit 42d7c463df
3 changed files with 23 additions and 18 deletions

View file

@ -35,19 +35,20 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/jordan-wright/gophish/auth"
"github.com/jordan-wright/gophish/middleware"
mid "github.com/jordan-wright/gophish/middleware"
"github.com/jordan-wright/gophish/models"
)
func CreateRouter() http.Handler {
router := mux.NewRouter()
// Base Front-end routes
router.Handle("/", middleware.Use(http.HandlerFunc(Base), middleware.RequireLogin))
router.HandleFunc("/login", Login)
router.HandleFunc("/register", Register)
router.HandleFunc("/campaigns", Base_Campaigns)
router.HandleFunc("/users", Users)
router.HandleFunc("/settings", Settings)
router.Handle("/", Use(http.HandlerFunc(Base), mid.RequireLogin))
router.Handle("/campaigns", Use(http.HandlerFunc(Campaigns), mid.RequireLogin))
router.Handle("/campaigns/{id}", Use(http.HandlerFunc(Campaigns_Id), mid.RequireLogin))
router.Handle("/users", Use(http.HandlerFunc(Users), mid.RequireLogin))
router.Handle("/settings", Use(http.HandlerFunc(Settings), mid.RequireLogin))
// Create the API routes
api := router.PathPrefix("/api").Subrouter()
@ -61,6 +62,15 @@ func CreateRouter() http.Handler {
return router
}
// Use allows us to stack middleware to process the request
// Example taken from https://github.com/gorilla/mux/pull/36#issuecomment-25849172
func Use(handler http.Handler, mid ...func(http.Handler) http.Handler) http.Handler {
for _, m := range mid {
handler = m(handler)
}
return handler
}
func Register(w http.ResponseWriter, r *http.Request) {
// If it is a post request, attempt to register the account
// Now that we are all registered, we can log the user in
@ -87,7 +97,12 @@ func Settings(w http.ResponseWriter, r *http.Request) {
getTemplate(w, "settings").ExecuteTemplate(w, "base", nil)
}
func Base_Campaigns(w http.ResponseWriter, r *http.Request) {
func Campaigns(w http.ResponseWriter, r *http.Request) {
//session, _ := auth.Store.Get(r, "gophish")
getTemplate(w, "dashboard").ExecuteTemplate(w, "base", nil)
}
func Campaigns_Id(w http.ResponseWriter, r *http.Request) {
//session, _ := auth.Store.Get(r, "gophish")
getTemplate(w, "dashboard").ExecuteTemplate(w, "base", nil)
}

View file

@ -41,12 +41,11 @@ var setupFlag = flag.Bool("setup", false, "Starts the initial setup process for
func main() {
//Setup the global variables and settings
err := db.Setup()
//defer db.Conn.Close()
defer db.Conn.Close()
if err != nil {
fmt.Println(err)
}
fmt.Printf("Gophish server started at http://%s\n", config.Conf.URL)
http.Handle("/", middleware.Use(controllers.CreateRouter(), middleware.GetContext))
http.Handle("/", controllers.Use(controllers.CreateRouter(), middleware.GetContext))
http.ListenAndServe(config.Conf.URL, nil)
fmt.Println("Closed.")
}

View file

@ -7,15 +7,6 @@ import (
"github.com/jordan-wright/gophish/auth"
)
// Use allows us to stack middleware to process the request
// Example taken from https://github.com/gorilla/mux/pull/36#issuecomment-25849172
func Use(handler http.Handler, middleware ...func(http.Handler) http.Handler) http.Handler {
for _, m := range middleware {
handler = m(handler)
}
return handler
}
// GetContext wraps each request in a function which fills in the context for a given request.
// This includes setting the User and Session keys and values as necessary for use in later functions.
func GetContext(handler http.Handler) http.Handler {