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
test.log
config.yml

View File

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

View File

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

View File

@ -15,8 +15,7 @@ type JsonResult struct {
} `json:"session"`
}
func JsonOutput(sessions *session.Manager, w http.ResponseWriter, r *http.Request, data interface{}) {
sess := sessions.SessionStart(w, r)
func JsonOutput(sess session.Session, w http.ResponseWriter, r *http.Request, data interface{}) {
result := JsonResult{Data: data}
result.Session.Login = sess.Get("login")
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 (
"flag"
"log"
"net"
"net/http"
@ -15,6 +14,7 @@ import (
"github.com/rs/cors"
libconfig "dev.sum7.de/sum7/warehost/config"
log "dev.sum7.de/sum7/warehost/lib/log"
"dev.sum7.de/sum7/warehost/system"
)
@ -30,6 +30,7 @@ func main() {
flag.StringVar(&configFile, "c", "config.yml", "path of configuration file")
flag.Parse()
config = libconfig.ReadConfigFile(configFile)
log.NewLogger(config.Log.Path)
// Session mgmt
sessions, _ = session.NewManager("memory", "session", 3600)
@ -38,7 +39,7 @@ func main() {
// Main Databaseconnection
dbconnection, err = xorm.NewEngine("postgres", config.Database)
if err != nil {
log.Fatal("[system] Error database connection: ", err)
log.Log.Fatal("[system] Error database connection: ", err)
}
defer dbconnection.Close()
dbconnection.ShowSQL(config.DatabaseDebug)
@ -62,7 +63,7 @@ func main() {
handler := c.Handler(router)
// Start server
address := net.JoinHostPort(config.API.Address, config.API.Port)
log.Println("starting webserver on", address)
log.Log.Info("starting api on ", address)
// TODO bad
log.Fatal(http.ListenAndServe(address, handler))
log.Log.Fatal(http.ListenAndServe(address, handler))
}

View File

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