From 2a88b259b33fd360d32374d15ac8ef3296705c03 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sun, 12 Jan 2014 20:00:20 -0600 Subject: [PATCH] Working on API layout --- controllers/api.go | 16 ++++++++++++++-- controllers/route.go | 2 +- middleware/middleware.go | 2 +- models/models.go | 8 ++++---- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index 65c58161..87678c6f 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -1,26 +1,33 @@ package controllers import ( + "encoding/json" "fmt" "net/http" + ctx "github.com/gorilla/context" "github.com/gorilla/mux" ) func API(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello api") + 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. //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) { + w.Header().Set("Content-Type", "application/json") switch { case r.Method == "GET": case r.Method == "POST": fmt.Fprintf(w, "Hello POST!") } - fmt.Fprintf(w, "Hello api") + //fmt.Fprintf(w, "Hello api") } //API_Campaigns_Id returns details about the requested campaign. If the campaign is not @@ -35,3 +42,8 @@ func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) { 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 9422cbb8..e81eebbc 100644 --- a/controllers/route.go +++ b/controllers/route.go @@ -52,7 +52,7 @@ func CreateRouter() *mux.Router { // Create the API routes api := router.PathPrefix("/api").Subrouter() - api.HandleFunc("/", API) + api.HandleFunc("/", Use(API, mid.RequireLogin)) api.HandleFunc("/campaigns", API_Campaigns) api.HandleFunc("/campaigns/{id}", API_Campaigns_Id) api.HandleFunc("/doc", API_Doc) diff --git a/middleware/middleware.go b/middleware/middleware.go index ca45dfb7..2105cf3e 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -18,7 +18,7 @@ func GetContext(handler http.Handler) http.HandlerFunc { // Put the session in the context so that ctx.Set(r, "session", session) if id, ok := session.Values["id"]; ok { - u, err := auth.GetUser(id.(int)) + u, err := auth.GetUserById(id.(int)) if err != nil { ctx.Set(r, "user", nil) } diff --git a/models/models.go b/models/models.go index 454e4271..f7186236 100644 --- a/models/models.go +++ b/models/models.go @@ -16,10 +16,10 @@ type Config struct { // User represents the user model for gophish. type User struct { - Id int - Username string - Hash string - APIKey string + Id int `json:"id"` + Username string `json:"username"` + Hash string `json:"-"` + APIKey string `json:"apikey"` } // Flash is used to hold flash information for use in templates.