improve loggin
This commit is contained in:
parent
05bd01e015
commit
4da9b60aa1
|
@ -0,0 +1,7 @@
|
||||||
|
#
|
||||||
|
|
||||||
|
# TODO on change
|
||||||
|
```
|
||||||
|
ALTER TABLE login ALTER createat TYPE timestamptz;
|
||||||
|
ALTER TABLE login ALTER lastloginat TYPE timestamptz;
|
||||||
|
```
|
|
@ -9,6 +9,10 @@ import (
|
||||||
|
|
||||||
var Log *log.Logger
|
var Log *log.Logger
|
||||||
|
|
||||||
|
type ModulLog struct {
|
||||||
|
log *log.Entry
|
||||||
|
}
|
||||||
|
|
||||||
func NewLogger(path string) *log.Logger {
|
func NewLogger(path string) *log.Logger {
|
||||||
if Log != nil {
|
if Log != nil {
|
||||||
return Log
|
return Log
|
||||||
|
@ -22,10 +26,17 @@ func NewLogger(path string) *log.Logger {
|
||||||
return Log
|
return Log
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLog(r *http.Request, modul string, request string) *log.Entry {
|
func NewModulLog(modul string) *ModulLog {
|
||||||
return Log.WithFields(log.Fields{
|
return &ModulLog{
|
||||||
|
log: Log.WithFields(log.Fields{
|
||||||
|
"modul": modul,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ModulLog) GetLog(r *http.Request, request string) *log.Entry {
|
||||||
|
return m.log.WithFields(log.Fields{
|
||||||
"remote": r.RemoteAddr,
|
"remote": r.RemoteAddr,
|
||||||
"modul": modul,
|
|
||||||
"request": request,
|
"request": request,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
2
main.go
2
main.go
|
@ -39,7 +39,7 @@ func main() {
|
||||||
// Main Databaseconnection
|
// Main Databaseconnection
|
||||||
dbconnection, err = xorm.NewEngine("postgres", config.Database)
|
dbconnection, err = xorm.NewEngine("postgres", config.Database)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Log.Fatal("[system] Error database connection: ", err)
|
log.Log.Fatal("database connection: ", err)
|
||||||
}
|
}
|
||||||
defer dbconnection.Close()
|
defer dbconnection.Close()
|
||||||
dbconnection.ShowSQL(config.DatabaseDebug)
|
dbconnection.ShowSQL(config.DatabaseDebug)
|
||||||
|
|
|
@ -14,16 +14,25 @@ import (
|
||||||
libpassword "dev.sum7.de/sum7/warehost/lib/password"
|
libpassword "dev.sum7.de/sum7/warehost/lib/password"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//MODULNAME to get global name for the modul
|
||||||
|
const MODULNAME = "system"
|
||||||
|
|
||||||
//API keep data in module global
|
//API keep data in module global
|
||||||
type API struct {
|
type API struct {
|
||||||
config *libconfig.Config
|
config *libconfig.Config
|
||||||
sessions *session.Manager
|
sessions *session.Manager
|
||||||
dbconnection *xorm.Engine
|
dbconnection *xorm.Engine
|
||||||
|
log *log.ModulLog
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAPI sets the routes to the api functions
|
// NewAPI sets the routes to the api functions
|
||||||
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *xorm.Engine, router *httprouter.Router, prefix string) {
|
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *xorm.Engine, router *httprouter.Router, prefix string) {
|
||||||
api := &API{config: config, sessions: sessions, dbconnection: dbconnection}
|
api := &API{
|
||||||
|
config: config,
|
||||||
|
sessions: sessions,
|
||||||
|
dbconnection: dbconnection,
|
||||||
|
log: log.NewModulLog(MODULNAME),
|
||||||
|
}
|
||||||
router.GET(prefix+"/status", api.Status)
|
router.GET(prefix+"/status", api.Status)
|
||||||
router.POST(prefix+"/login", api.Login)
|
router.POST(prefix+"/login", api.Login)
|
||||||
router.GET(prefix+"/logout", api.Logout)
|
router.GET(prefix+"/logout", api.Logout)
|
||||||
|
@ -33,7 +42,7 @@ func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *x
|
||||||
// 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 := api.sessions.SessionStart(w, r)
|
sess := api.sessions.SessionStart(w, r)
|
||||||
logger := log.GetLog(r, "system", "status")
|
logger := api.log.GetLog(r, "status")
|
||||||
result, err := api.dbconnection.Count(new(Login))
|
result, err := api.dbconnection.Count(new(Login))
|
||||||
connection := false
|
connection := false
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -51,7 +60,7 @@ func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Para
|
||||||
func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
api.sessions.SessionDestroy(w, r)
|
api.sessions.SessionDestroy(w, r)
|
||||||
sess := api.sessions.SessionStart(w, r)
|
sess := api.sessions.SessionStart(w, r)
|
||||||
logger := log.GetLog(r, "system", "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)
|
||||||
}
|
}
|
||||||
|
@ -64,7 +73,7 @@ func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Para
|
||||||
// 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 := api.sessions.SessionStart(w, r)
|
sess := api.sessions.SessionStart(w, r)
|
||||||
logger := log.GetLog(r, "system", "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 {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
log "dev.sum7.de/sum7/warehost/lib/log"
|
||||||
libpassword "dev.sum7.de/sum7/warehost/lib/password"
|
libpassword "dev.sum7.de/sum7/warehost/lib/password"
|
||||||
|
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
|
@ -30,11 +30,11 @@ type Login struct {
|
||||||
func SyncModels(dbconnection *xorm.Engine) {
|
func SyncModels(dbconnection *xorm.Engine) {
|
||||||
err := dbconnection.Sync(new(Login))
|
err := dbconnection.Sync(new(Login))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("[system] Error create table \"login\": ", err)
|
log.Log.Fatal("create table \"login\" ", err)
|
||||||
}
|
}
|
||||||
result, err := dbconnection.Count(new(Login))
|
result, err := dbconnection.Count(new(Login))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("[system] Error get \"login\" count: ", err)
|
log.Log.Error("get \"login\" count ", err)
|
||||||
}
|
}
|
||||||
if result <= 0 {
|
if result <= 0 {
|
||||||
login := new(Login)
|
login := new(Login)
|
||||||
|
@ -45,9 +45,9 @@ func SyncModels(dbconnection *xorm.Engine) {
|
||||||
login.Superadmin = true
|
login.Superadmin = true
|
||||||
_, err := dbconnection.Insert(login)
|
_, err := dbconnection.Insert(login)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Print("[system] Create user \"root\"")
|
log.Log.Warn("Create user \"root\"")
|
||||||
} else {
|
} else {
|
||||||
log.Print("[system] Error cound not first user: ", err)
|
log.Log.Fatal("cound not first user \"root\" ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue