sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

add logging

This commit is contained in:
Martin Geno 2016-08-14 15:29:54 +02:00
parent 7fc74044d7
commit 05bd01e015
7 changed files with 77 additions and 18 deletions

1
.gitignore vendored
View File

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

View File

@ -3,6 +3,8 @@ api:
address: ::1 address: ::1
port: 8080 port: 8080
allowedorigins: "*" allowedorigins: "*"
log:
path: test.log
webroot: ./webroot/build webroot: ./webroot/build
database: "host=localhost user=warehost dbname=warehost password=hallo sslmode=disable" database: "host=localhost user=warehost dbname=warehost password=hallo sslmode=disable"
databasedebug: false databasedebug: false

View File

@ -14,6 +14,9 @@ type Config struct {
Port string `yaml:"port"` Port string `yaml:"port"`
AllowedOrigins string `yaml:"allowedorigins"` AllowedOrigins string `yaml:"allowedorigins"`
} `yaml:"api"` } `yaml:"api"`
Log struct {
Path string `yaml:"path"`
} `yaml:"log"`
Webroot string `yaml:"webroot"` Webroot string `yaml:"webroot"`
Database string `yaml:"database"` Database string `yaml:"database"`
DatabaseDebug bool `yaml:"databasedebug"` DatabaseDebug bool `yaml:"databasedebug"`

View File

@ -15,8 +15,7 @@ type JsonResult struct {
} `json:"session"` } `json:"session"`
} }
func JsonOutput(sessions *session.Manager, w http.ResponseWriter, r *http.Request, data interface{}) { func JsonOutput(sess session.Session, w http.ResponseWriter, r *http.Request, data interface{}) {
sess := sessions.SessionStart(w, r)
result := JsonResult{Data: data} result := JsonResult{Data: data}
result.Session.Login = sess.Get("login") result.Session.Login = sess.Get("login")
js, err := json.Marshal(result) js, err := json.Marshal(result)

31
lib/log/main.go Normal file
View File

@ -0,0 +1,31 @@
package log
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/rifflock/lfshook"
)
var Log *log.Logger
func NewLogger(path string) *log.Logger {
if Log != nil {
return Log
}
Log = log.New()
//Log.Formatter = new(log.JSONFormatter)
Log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
log.WarnLevel: path,
log.ErrorLevel: path,
}))
return Log
}
func GetLog(r *http.Request, modul string, request string) *log.Entry {
return Log.WithFields(log.Fields{
"remote": r.RemoteAddr,
"modul": modul,
"request": request,
})
}

View File

@ -2,7 +2,6 @@ package main
import ( import (
"flag" "flag"
"log"
"net" "net"
"net/http" "net/http"
@ -15,6 +14,7 @@ import (
"github.com/rs/cors" "github.com/rs/cors"
libconfig "dev.sum7.de/sum7/warehost/config" libconfig "dev.sum7.de/sum7/warehost/config"
log "dev.sum7.de/sum7/warehost/lib/log"
"dev.sum7.de/sum7/warehost/system" "dev.sum7.de/sum7/warehost/system"
) )
@ -30,6 +30,7 @@ func main() {
flag.StringVar(&configFile, "c", "config.yml", "path of configuration file") flag.StringVar(&configFile, "c", "config.yml", "path of configuration file")
flag.Parse() flag.Parse()
config = libconfig.ReadConfigFile(configFile) config = libconfig.ReadConfigFile(configFile)
log.NewLogger(config.Log.Path)
// Session mgmt // Session mgmt
sessions, _ = session.NewManager("memory", "session", 3600) sessions, _ = session.NewManager("memory", "session", 3600)
@ -38,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.Fatal("[system] Error database connection: ", err) log.Log.Fatal("[system] Error database connection: ", err)
} }
defer dbconnection.Close() defer dbconnection.Close()
dbconnection.ShowSQL(config.DatabaseDebug) dbconnection.ShowSQL(config.DatabaseDebug)
@ -62,7 +63,7 @@ func main() {
handler := c.Handler(router) handler := c.Handler(router)
// Start server // Start server
address := net.JoinHostPort(config.API.Address, config.API.Port) address := net.JoinHostPort(config.API.Address, config.API.Port)
log.Println("starting webserver on", address) log.Log.Info("starting api on ", address)
// TODO bad // TODO bad
log.Fatal(http.ListenAndServe(address, handler)) log.Log.Fatal(http.ListenAndServe(address, handler))
} }

View File

@ -2,7 +2,6 @@ package system
import ( import (
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
"github.com/astaxie/session" "github.com/astaxie/session"
@ -11,6 +10,7 @@ import (
libconfig "dev.sum7.de/sum7/warehost/config" libconfig "dev.sum7.de/sum7/warehost/config"
libapi "dev.sum7.de/sum7/warehost/lib/api" libapi "dev.sum7.de/sum7/warehost/lib/api"
log "dev.sum7.de/sum7/warehost/lib/log"
libpassword "dev.sum7.de/sum7/warehost/lib/password" libpassword "dev.sum7.de/sum7/warehost/lib/password"
) )
@ -25,48 +25,65 @@ type API struct {
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}
router.GET(prefix+"/status", api.Status) router.GET(prefix+"/status", api.Status)
router.POST(prefix+"/login", api.Login)
router.GET(prefix+"/logout", api.Logout) router.GET(prefix+"/logout", api.Logout)
router.POST(prefix+"/login", api.Login)
//router.OPTIONS(prefix+"/login", api.Login)
} }
// 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)
logger := log.GetLog(r, "system", "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 {
log.Print("[error][system]-status: get login count: ", err) logger.Error("get login count: ", err)
} else { } else {
if result > 0 { if result > 0 {
connection = true connection = true
} }
} }
libapi.JsonOutput(api.sessions, w, r, connection) logger.Info("status")
libapi.JsonOutput(sess, w, r, connection)
} }
// 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) {
api.sessions.SessionDestroy(w, r) api.sessions.SessionDestroy(w, r)
libapi.JsonOutput(api.sessions, w, r, true) sess := api.sessions.SessionStart(w, r)
logger := log.GetLog(r, "system", "logout")
if login := sess.Get("login"); login != nil {
logger = logger.WithField("user", login.(Login).Username)
}
sess.Delete("login")
sess.Delete("profil")
logger.Info("logout")
libapi.JsonOutput(sess, w, r, true)
} }
// 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")
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 {
log.Println("[system]-login error fetch request") logger.Error("fetch request")
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
libapi.JsonOutput(api.sessions, w, r, false) libapi.JsonOutput(sess, w, r, false)
return return
} }
logger = logger.WithField("user", requestlogin.Username)
var login = Login{Username: requestlogin.Username} var login = Login{Username: requestlogin.Username}
_, err = api.dbconnection.Get(&login) _, err = api.dbconnection.Get(&login)
if err != nil { if err != nil {
log.Println("[system]-login error fetch database") logger.Error("fetch database")
libapi.JsonOutput(api.sessions, w, r, false) libapi.JsonOutput(sess, w, r, false)
return
}
if login.Id <= 0 {
logger.Warn("user not found")
libapi.JsonOutput(sess, w, r, false)
return return
} }
result := false result := false
@ -75,8 +92,13 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
if output { if output {
result = true result = true
sess.Set("login", login) sess.Set("login", login)
logger.Info("logged in")
} else {
logger.Warn("wrong password")
} }
} else {
logger.Warn("not active")
} }
log.Println("[system]-login worked", result)
libapi.JsonOutput(api.sessions, w, r, result) libapi.JsonOutput(sess, w, r, result)
} }