36 lines
1.0 KiB
Go
36 lines
1.0 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/astaxie/session"
|
|
)
|
|
|
|
type JsonResult struct {
|
|
Data interface{} `json:"data"`
|
|
Session struct {
|
|
Login interface{} `json:"login"`
|
|
Profil map[string]interface{} `json:"profil"`
|
|
} `json:"session"`
|
|
}
|
|
|
|
func JsonOutput(sess session.Session, w http.ResponseWriter, r *http.Request, data interface{}) {
|
|
result := JsonResult{Data: data}
|
|
result.Session.Login = sess.Get("login")
|
|
js, err := json.Marshal(result)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if origin := r.Header.Get("Origin"); origin != "" {
|
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
|
}
|
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
w.Write(js)
|
|
}
|