Working on API layout

pull/24/head
Jordan 2014-01-12 20:00:20 -06:00
parent ef1bf55484
commit 2a88b259b3
4 changed files with 20 additions and 8 deletions

View File

@ -1,26 +1,33 @@
package controllers package controllers
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
ctx "github.com/gorilla/context"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
func API(w http.ResponseWriter, r *http.Request) { 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. //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) {
w.Header().Set("Content-Type", "application/json")
switch { switch {
case r.Method == "GET": case r.Method == "GET":
case r.Method == "POST": case r.Method == "POST":
fmt.Fprintf(w, "Hello 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 //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) { func API_Doc(w http.ResponseWriter, r *http.Request) {
getTemplate(w, "api_doc").ExecuteTemplate(w, "base", nil) 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)
}

View File

@ -52,7 +52,7 @@ func CreateRouter() *mux.Router {
// Create the API routes // Create the API routes
api := router.PathPrefix("/api").Subrouter() api := router.PathPrefix("/api").Subrouter()
api.HandleFunc("/", API) api.HandleFunc("/", Use(API, mid.RequireLogin))
api.HandleFunc("/campaigns", API_Campaigns) api.HandleFunc("/campaigns", API_Campaigns)
api.HandleFunc("/campaigns/{id}", API_Campaigns_Id) api.HandleFunc("/campaigns/{id}", API_Campaigns_Id)
api.HandleFunc("/doc", API_Doc) api.HandleFunc("/doc", API_Doc)

View File

@ -18,7 +18,7 @@ func GetContext(handler http.Handler) http.HandlerFunc {
// Put the session in the context so that // Put the session in the context so that
ctx.Set(r, "session", session) ctx.Set(r, "session", session)
if id, ok := session.Values["id"]; ok { if id, ok := session.Values["id"]; ok {
u, err := auth.GetUser(id.(int)) u, err := auth.GetUserById(id.(int))
if err != nil { if err != nil {
ctx.Set(r, "user", nil) ctx.Set(r, "user", nil)
} }

View File

@ -16,10 +16,10 @@ type Config struct {
// User represents the user model for gophish. // User represents the user model for gophish.
type User struct { type User struct {
Id int Id int `json:"id"`
Username string Username string `json:"username"`
Hash string Hash string `json:"-"`
APIKey string APIKey string `json:"apikey"`
} }
// Flash is used to hold flash information for use in templates. // Flash is used to hold flash information for use in templates.