Cleaning up router

/campaigns - depreciated
/api/doc - now /api/
pull/24/head
Jordan 2014-01-31 21:49:35 -06:00
parent c75e55154b
commit 3edcc11e61
4 changed files with 19 additions and 34 deletions

View File

@ -20,23 +20,16 @@ const (
ERROR string = "Error" ERROR string = "Error"
) )
// API (/api) provides access to api documentation
func API(w http.ResponseWriter, r *http.Request) { func API(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { switch {
case r.Method == "GET":
} getTemplate(w, "api_doc").ExecuteTemplate(w, "base", nil)
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)
} }
} }
//API_Campaigns returns a list of campaigns if requested via GET. // 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. // 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) { func API_Campaigns(w http.ResponseWriter, r *http.Request) {
switch { switch {
case r.Method == "GET": 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 // API_Campaigns_Id returns details about the requested campaign. If the campaign is not
//valid, API_Campaigns_Id returns null. // valid, API_Campaigns_Id returns null.
func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) { func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["id"], 0, 64) 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) { func writeJSON(w http.ResponseWriter, c []byte) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", c) fmt.Fprintf(w, "%s", c)

View File

@ -47,17 +47,15 @@ func CreateRouter() *mux.Router {
router.HandleFunc("/login", Login) router.HandleFunc("/login", Login)
router.HandleFunc("/register", Register) router.HandleFunc("/register", Register)
router.HandleFunc("/", Use(Base, mid.RequireLogin)) router.HandleFunc("/", Use(Base, mid.RequireLogin))
router.HandleFunc("/campaigns", Use(Campaigns, mid.RequireLogin))
router.HandleFunc("/campaigns/{id}", Use(Campaigns_Id, mid.RequireLogin)) router.HandleFunc("/campaigns/{id}", Use(Campaigns_Id, mid.RequireLogin))
router.HandleFunc("/users", Use(Users, mid.RequireLogin)) router.HandleFunc("/users", Use(Users, mid.RequireLogin))
router.HandleFunc("/settings", Use(Settings, mid.RequireLogin)) router.HandleFunc("/settings", Use(Settings, mid.RequireLogin))
// Create the API routes // Create the API routes
api := router.PathPrefix("/api").Subrouter() 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", Use(API_Campaigns, mid.RequireAPIKey))
api.HandleFunc("/campaigns/{id}", Use(API_Campaigns_Id, mid.RequireAPIKey)) api.HandleFunc("/campaigns/{id}", Use(API_Campaigns_Id, mid.RequireAPIKey))
api.HandleFunc("/doc", API_Doc)
//Setup static file serving //Setup static file serving
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/"))) 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) 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) { func Campaigns_Id(w http.ResponseWriter, r *http.Request) {
//session, _ := auth.Store.Get(r, "gophish") params := struct {
getTemplate(w, "dashboard").ExecuteTemplate(w, "base", nil) 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) { func Login(w http.ResponseWriter, r *http.Request) {

View File

@ -1,4 +1,4 @@
{{define "content"}} {{template "nav"}} {{%define "content"%}} {{%template "nav"%}}
<div class="jumbotron"> <div class="jumbotron">
<div class="container" style="text-align:center;"> <div class="container" style="text-align:center;">
<h1 class="sans header"> <h1 class="sans header">
@ -15,7 +15,7 @@
</li> </li>
<li><a href="/settings">Settings</a> <li><a href="/settings">Settings</a>
</li> </li>
<li class="active"><a href="/api/doc">API Documentation</a> <li class="active"><a href="/api/">API Documentation</a>
</li> </li>
</ul> </ul>
</div> </div>
@ -26,5 +26,5 @@
<p>Test.</p> <p>Test.</p>
</div> </div>
</div> </div>
{{end}} {{%end%}}

View File

@ -15,7 +15,7 @@
</li> </li>
<li><a href="/settings">Settings</a> <li><a href="/settings">Settings</a>
</li> </li>
<li><a href="/api/doc">API Documentation</a> <li><a href="/api/">API Documentation</a>
</li> </li>
</ul> </ul>
</div> </div>