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

87 lines
2.5 KiB
Go
Raw Normal View History

2016-08-13 11:03:03 +02:00
package api
import (
2016-12-19 12:24:18 +01:00
"context"
2016-08-13 11:03:03 +02:00
"encoding/json"
"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
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
2016-12-19 12:24:18 +01:00
type Handle func(w http.ResponseWriter, r *http.Request)
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-12-19 12:24:18 +01:00
// SessionHandler Handler to manage session of api request
func SessionHandler(h Handle) Handle {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
sess := libsession.SessionStart(w, r)
ctx = context.WithValue(ctx, "session", sess)
r = r.WithContext(ctx)
h(w, r)
}
}
// JSONWrite generate default json answer
func JSONWrite(w http.ResponseWriter, r *http.Request, data interface{}, errorresult *ErrorResult) {
ctx := r.Context()
2016-10-11 20:16:24 +02:00
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-10-17 14:07:17 +02:00
//JSONDecoder handle complete request of JSON
2016-12-19 12:24:18 +01:00
func JSONDecoder(w http.ResponseWriter, r *http.Request, logger *log.Entry, data interface{}) (returnerr *ErrorResult) {
2016-12-20 00:35:03 +01:00
/*if r.Header.Get("Content-Type") != "application/json" {
2016-12-19 12:24:18 +01:00
logger.Error("fetch wrong request type")
returnerr = &ErrorResult{
Message: "Internal Request Error",
}
w.WriteHeader(http.StatusBadRequest)
return
}
2016-12-20 00:35:03 +01:00
*/
2016-12-19 12:24:18 +01:00
err := json.NewDecoder(r.Body).Decode(data)
2016-10-17 14:07:17 +02:00
if err != nil {
logger.Error("fetch request")
returnerr = &ErrorResult{
Message: "Internal Request Error",
}
w.WriteHeader(http.StatusBadRequest)
return
}
return nil
}