sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

new api structur

This commit is contained in:
Martin Geno 2016-08-16 08:30:02 +02:00
parent a07d4f4d69
commit b78aa4be3d
3 changed files with 60 additions and 24 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
/webroot !/webroot
test.log test.log
config.yml config.yml

View File

@ -5,18 +5,26 @@ import (
"net/http" "net/http"
"github.com/astaxie/session" "github.com/astaxie/session"
"github.com/julienschmidt/httprouter"
) )
type Handle func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session) (interface{}, *ErrorResult)
type ErrorResult struct {
Fields []string `json:"fields"`
Message string `json:"msg"`
}
type JsonResult struct { type JsonResult struct {
Data interface{} `json:"data"` Data interface{} `json:"data"`
Error *ErrorResult `json:"error,omitemty"`
Session struct { Session struct {
Login interface{} `json:"login"` Login interface{} `json:"login"`
Profil map[string]interface{} `json:"profil"` Profil map[string]interface{} `json:"profil"`
} `json:"session"` } `json:"session"`
} }
func JsonOutput(sess session.Session, w http.ResponseWriter, r *http.Request, data interface{}) { func JsonOutput(w http.ResponseWriter, r *http.Request, sess session.Session, data interface{}, errorresult *ErrorResult) {
result := JsonResult{Data: data} result := JsonResult{Data: data, Error: errorresult}
result.Session.Login = sess.Get("login") result.Session.Login = sess.Get("login")
js, err := json.Marshal(result) js, err := json.Marshal(result)
if err != nil { if err != nil {
@ -33,3 +41,19 @@ func JsonOutput(sess session.Session, w http.ResponseWriter, r *http.Request, da
w.Header().Set("Access-Control-Allow-Credentials", "true") w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Write(js) w.Write(js)
} }
func SessionHandler(h Handle, sessions *session.Manager) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
sess := sessions.SessionStart(w, r)
data, err := h(w, r, ps, sess)
JsonOutput(w, r, sess, data, err)
}
}
func LoginHandler(h Handle, sessions *session.Manager) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
sess := sessions.SessionStart(w, r)
data, err := h(w, r, ps, sess)
JsonOutput(w, r, sess, data, err)
}
}

View File

@ -33,33 +33,33 @@ func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *x
dbconnection: dbconnection, dbconnection: dbconnection,
log: log.NewModulLog(MODULNAME), log: log.NewModulLog(MODULNAME),
} }
router.GET(prefix+"/status", api.Status) router.GET(prefix+"/status", libapi.SessionHandler(api.Status, sessions))
router.POST(prefix+"/login", api.Login) router.POST(prefix+"/login", libapi.SessionHandler(api.Login, sessions))
router.GET(prefix+"/logout", api.Logout) router.GET(prefix+"/logout", libapi.LoginHandler(api.Logout, sessions))
router.POST(prefix+"/password", libapi.LoginHandler(api.Password, sessions))
router.GET(prefix+"/delete", libapi.LoginHandler(api.Status, sessions))
} }
// Status to get Login and Server status // Status to get Login and Server status
func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
sess := api.sessions.SessionStart(w, r) returndata = false
logger := api.log.GetLog(r, "status") logger := api.log.GetLog(r, "status")
result, err := api.dbconnection.Count(new(Login)) result, err := api.dbconnection.Count(new(Login))
connection := false
if err != nil { if err != nil {
returnerr = &libapi.ErrorResult{Message: "Affe"}
logger.Error("get login count: ", err) logger.Error("get login count: ", err)
} else { } else {
if result > 0 { if result > 0 {
connection = true returndata = true
} }
} }
logger.Info("status") logger.Info("status")
libapi.JsonOutput(sess, w, r, connection) return
} }
// Logout current user // Logout current user
func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
api.sessions.SessionDestroy(w, r) api.sessions.SessionDestroy(w, r)
sess := api.sessions.SessionStart(w, r)
logger := api.log.GetLog(r, "logout") logger := api.log.GetLog(r, "logout")
if login := sess.Get("login"); login != nil { if login := sess.Get("login"); login != nil {
logger = logger.WithField("user", login.(Login).Username) logger = logger.WithField("user", login.(Login).Username)
@ -67,19 +67,19 @@ func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Para
sess.Delete("login") sess.Delete("login")
sess.Delete("profil") sess.Delete("profil")
logger.Info("logout") logger.Info("logout")
libapi.JsonOutput(sess, w, r, true) returndata = true
return
} }
// Login of system // Login of system
func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
sess := api.sessions.SessionStart(w, r)
logger := api.log.GetLog(r, "login") logger := api.log.GetLog(r, "login")
var requestlogin RequestLogin var requestlogin RequestLogin
err := json.NewDecoder(r.Body).Decode(&requestlogin) err := json.NewDecoder(r.Body).Decode(&requestlogin)
if err != nil { if err != nil {
logger.Error("fetch request") logger.Error("fetch request")
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
libapi.JsonOutput(sess, w, r, false) returndata = false
return return
} }
logger = logger.WithField("user", requestlogin.Username) logger = logger.WithField("user", requestlogin.Username)
@ -87,19 +87,19 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
_, err = api.dbconnection.Get(&login) _, err = api.dbconnection.Get(&login)
if err != nil { if err != nil {
logger.Error("fetch database") logger.Error("fetch database")
libapi.JsonOutput(sess, w, r, false) returndata = false
return return
} }
if login.Id <= 0 { if login.Id <= 0 {
logger.Warn("user not found") logger.Warn("user not found")
libapi.JsonOutput(sess, w, r, false) returndata = false
return return
} }
result := false returndata = false
if login.Active { if login.Active {
output, _ := libpassword.Validate(login.Password, requestlogin.Password) output, _ := libpassword.Validate(login.Password, requestlogin.Password)
if output { if output {
result = true returndata = true
sess.Set("login", login) sess.Set("login", login)
logger.Info("logged in") logger.Info("logged in")
} else { } else {
@ -109,5 +109,17 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
logger.Warn("not active") logger.Warn("not active")
} }
libapi.JsonOutput(sess, w, r, result) return
}
func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
logger := api.log.GetLog(r, "password")
logger.Warn("not implemented")
returndata = false
return
}
func (api *API) Delete(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
logger := api.log.GetLog(r, "delete")
logger.Warn("not implemented")
returndata = false
return
} }