Browse Source

Working on API layout

pull/24/head
Jordan 7 years ago
parent
commit
2a88b259b3
4 changed files with 20 additions and 8 deletions
  1. +14
    -2
      controllers/api.go
  2. +1
    -1
      controllers/route.go
  3. +1
    -1
      middleware/middleware.go
  4. +4
    -4
      models/models.go

+ 14
- 2
controllers/api.go View File

@ -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)
}

+ 1
- 1
controllers/route.go View File

@ -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)

+ 1
- 1
middleware/middleware.go View File

@ -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)
}

+ 4
- 4
models/models.go View File

@ -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.

Loading…
Cancel
Save