From 3edcc11e61e2ed5bfab1299cc917bd57bb816b43 Mon Sep 17 00:00:00 2001 From: Jordan Date: Fri, 31 Jan 2014 21:49:35 -0600 Subject: [PATCH] Cleaning up router /campaigns - depreciated /api/doc - now /api/ --- controllers/api.go | 28 ++++++++-------------------- controllers/route.go | 17 +++++++---------- templates/api_doc.html | 6 +++--- templates/dashboard.html | 2 +- 4 files changed, 19 insertions(+), 34 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index 6fa254f1..78718bf9 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -20,23 +20,16 @@ const ( ERROR string = "Error" ) +// API (/api) provides access to api documentation func API(w http.ResponseWriter, r *http.Request) { - if r.Method == "GET" { - - } - if r.Method == "POST" { - //Add a new campaign - //v := - } - if u, err := json.Marshal(ctx.Get(r, "user")); err == nil { - writeJSON(w, u) - } else { - http.Error(w, "Server Error", 500) + switch { + case r.Method == "GET": + getTemplate(w, "api_doc").ExecuteTemplate(w, "base", nil) } } -//API_Campaigns returns a list of campaigns if requested via GET. -//If requested via POST, API_Campaigns creates a new campaign and returns a reference to it. +// API_Campaigns returns a list of campaigns if requested via GET. +// If requested via POST, API_Campaigns creates a new campaign and returns a reference to it. func API_Campaigns(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "GET": @@ -76,8 +69,8 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) { } } -//API_Campaigns_Id returns details about the requested campaign. If the campaign is not -//valid, API_Campaigns_Id returns null. +// API_Campaigns_Id returns details about the requested campaign. If the campaign is not +// valid, API_Campaigns_Id returns null. func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id, err := strconv.ParseInt(vars["id"], 0, 64) @@ -102,11 +95,6 @@ func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) { } } -//API_Doc renders a template describing the API documentation. -func API_Doc(w http.ResponseWriter, r *http.Request) { - getTemplate(w, "api_doc").ExecuteTemplate(w, "base", nil) -} - func writeJSON(w http.ResponseWriter, c []byte) { w.Header().Set("Content-Type", "application/json") fmt.Fprintf(w, "%s", c) diff --git a/controllers/route.go b/controllers/route.go index 2273cba4..7e0d517f 100644 --- a/controllers/route.go +++ b/controllers/route.go @@ -47,17 +47,15 @@ func CreateRouter() *mux.Router { router.HandleFunc("/login", Login) router.HandleFunc("/register", Register) router.HandleFunc("/", Use(Base, mid.RequireLogin)) - router.HandleFunc("/campaigns", Use(Campaigns, mid.RequireLogin)) router.HandleFunc("/campaigns/{id}", Use(Campaigns_Id, mid.RequireLogin)) router.HandleFunc("/users", Use(Users, mid.RequireLogin)) router.HandleFunc("/settings", Use(Settings, mid.RequireLogin)) // Create the API routes api := router.PathPrefix("/api").Subrouter() - api.HandleFunc("/", Use(API, mid.RequireAPIKey)) + api.HandleFunc("/", Use(API, mid.RequireLogin)) api.HandleFunc("/campaigns", Use(API_Campaigns, mid.RequireAPIKey)) api.HandleFunc("/campaigns/{id}", Use(API_Campaigns_Id, mid.RequireAPIKey)) - api.HandleFunc("/doc", API_Doc) //Setup static file serving router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/"))) @@ -101,14 +99,13 @@ func Settings(w http.ResponseWriter, r *http.Request) { getTemplate(w, "settings").ExecuteTemplate(w, "base", params) } -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) + params := struct { + User models.User + Title string + Flashes []interface{} + }{Title: "Results", User: ctx.Get(r, "user").(models.User)} + getTemplate(w, "dashboard").ExecuteTemplate(w, "base", params) } func Login(w http.ResponseWriter, r *http.Request) { diff --git a/templates/api_doc.html b/templates/api_doc.html index ea514231..a0858438 100644 --- a/templates/api_doc.html +++ b/templates/api_doc.html @@ -1,4 +1,4 @@ -{{define "content"}} {{template "nav"}} +{{%define "content"%}} {{%template "nav"%}}
@@ -26,5 +26,5 @@

Test.

-{{end}} +{{%end%}} diff --git a/templates/dashboard.html b/templates/dashboard.html index da0623a5..fc0c5f32 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -15,7 +15,7 @@
  • Settings
  • -
  • API Documentation +
  • API Documentation