sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/lib/api/main.go

78 lines
2.3 KiB
Go
Raw Normal View History

2016-08-13 11:03:03 +02:00
package api
import (
"encoding/json"
2016-10-17 14:07:17 +02:00
"io"
2016-08-13 11:03:03 +02:00
"net/http"
2016-10-17 14:07:17 +02:00
log "github.com/Sirupsen/logrus" // TODO-Bad
2016-10-11 20:16:24 +02:00
"goji.io"
"golang.org/x/net/context"
libsession "dev.sum7.eu/sum7/warehost/lib/session"
2016-08-13 11:03:03 +02:00
)
2016-10-11 20:16:24 +02:00
// Handle with response
type Handle func(ctx context.Context, w http.ResponseWriter, r *http.Request) (interface{}, *ErrorResult)
2016-09-03 10:18:46 +02:00
// ErrorResult struct for api error answer
2016-08-16 08:30:02 +02:00
type ErrorResult struct {
Fields []string `json:"fields"`
Message string `json:"msg"`
}
2016-09-03 10:18:46 +02:00
// JSONResult struct for api answer
type JSONResult struct {
2016-08-16 08:30:02 +02:00
Data interface{} `json:"data"`
2016-08-20 01:17:08 +02:00
Error *ErrorResult `json:"error,omitempty"`
2016-08-13 11:03:03 +02:00
Session struct {
2016-08-20 01:17:08 +02:00
Login interface{} `json:"login,omitempty"`
Profil map[string]interface{} `json:"profil,omitempty"`
} `json:"session,omitempty"`
2016-08-13 11:03:03 +02:00
}
2016-09-03 10:18:46 +02:00
// JSONOutput generate default json answer
2016-10-11 20:16:24 +02:00
func JSONOutput(ctx context.Context, w http.ResponseWriter, r *http.Request, data interface{}, errorresult *ErrorResult) {
sess := ctx.Value("session").(libsession.Session)
2016-09-03 10:18:46 +02:00
result := JSONResult{Data: data, Error: errorresult}
2016-08-13 11:03:03 +02:00
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)
}
2016-08-16 08:30:02 +02:00
2016-09-03 10:18:46 +02:00
// SessionHandler Handler to manage session of api request
2016-10-11 20:16:24 +02:00
func SessionHandler(h Handle) goji.HandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
sess := libsession.SessionStart(w, r)
ctx = context.WithValue(ctx, "session", sess)
data, err := h(ctx, w, r)
JSONOutput(ctx, w, r, data, err)
2016-08-16 08:30:02 +02:00
}
}
2016-10-17 14:07:17 +02:00
//JSONDecoder handle complete request of JSON
func JSONDecoder(r io.Reader, data interface{}, w http.ResponseWriter, logger *log.Entry) (returnerr *ErrorResult) {
err := json.NewDecoder(r).Decode(data)
if err != nil {
logger.Error("fetch request")
returnerr = &ErrorResult{
Message: "Internal Request Error",
}
w.WriteHeader(http.StatusBadRequest)
return
}
return nil
}