2014-01-09 06:42:05 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2014-01-13 02:00:20 +00:00
|
|
|
"encoding/json"
|
2014-01-09 06:42:05 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2014-01-13 02:00:20 +00:00
|
|
|
ctx "github.com/gorilla/context"
|
2014-01-09 06:42:05 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
func API(w http.ResponseWriter, r *http.Request) {
|
2014-01-13 02:00:20 +00:00
|
|
|
if u, err := json.Marshal(ctx.Get(r, "user")); err == nil {
|
|
|
|
writeJSON(w, u)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "Server Error", 500)
|
|
|
|
}
|
2014-01-09 06:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//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) {
|
2014-01-13 02:00:20 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2014-01-09 06:42:05 +00:00
|
|
|
switch {
|
|
|
|
case r.Method == "GET":
|
|
|
|
|
|
|
|
case r.Method == "POST":
|
|
|
|
fmt.Fprintf(w, "Hello POST!")
|
|
|
|
}
|
2014-01-13 02:00:20 +00:00
|
|
|
//fmt.Fprintf(w, "Hello api")
|
2014-01-09 06:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//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) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
fmt.Fprintf(w, "{\"method\" : \""+r.Method+"\", \"id\" : "+vars["id"]+"}")
|
|
|
|
}
|
|
|
|
|
|
|
|
//API_Doc renders a template describing the API documentation.
|
|
|
|
func API_Doc(w http.ResponseWriter, r *http.Request) {
|
2014-01-10 03:21:54 +00:00
|
|
|
getTemplate(w, "api_doc").ExecuteTemplate(w, "base", nil)
|
2014-01-09 06:42:05 +00:00
|
|
|
}
|
2014-01-13 02:00:20 +00:00
|
|
|
|
|
|
|
func writeJSON(w http.ResponseWriter, c []byte) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
fmt.Fprintf(w, "%s", c)
|
|
|
|
}
|