[TASK] reorder project structur + remove timestamp from log config
This commit is contained in:
parent
2560bafade
commit
2921580e11
|
@ -0,0 +1 @@
|
|||
../../config_example.conf
|
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
@ -10,35 +9,28 @@ import (
|
|||
|
||||
goji "goji.io"
|
||||
|
||||
http_api "github.com/genofire/hs_master-kss-monolith/http"
|
||||
"github.com/genofire/hs_master-kss-monolith/lib"
|
||||
web "github.com/genofire/hs_master-kss-monolith/http"
|
||||
"github.com/genofire/hs_master-kss-monolith/lib/log"
|
||||
"github.com/genofire/hs_master-kss-monolith/models"
|
||||
)
|
||||
|
||||
var (
|
||||
configFile string
|
||||
config *models.Config
|
||||
timestamps bool
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.BoolVar(×tamps, "timestamps", true, "print timestamps in output")
|
||||
flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)")
|
||||
flag.Parse()
|
||||
|
||||
// load config
|
||||
config = models.ReadConfigFile(configFile)
|
||||
|
||||
if !timestamps {
|
||||
log.SetFlags(0)
|
||||
lib.LogTimestamp(false)
|
||||
}
|
||||
|
||||
log.Println("Starting rezension monolith")
|
||||
log.Log.Info("Starting rezension monolith")
|
||||
|
||||
// Startwebsrver
|
||||
router := goji.NewMux()
|
||||
http_api.BindAPI(router)
|
||||
web.BindAPI(router)
|
||||
srv := &http.Server{
|
||||
Addr: config.WebserverBind,
|
||||
Handler: router,
|
||||
|
@ -53,5 +45,5 @@ func main() {
|
|||
// Stop services
|
||||
srv.Close()
|
||||
|
||||
log.Println("received", sig)
|
||||
log.Log.Info("received", sig)
|
||||
}
|
|
@ -3,11 +3,12 @@ package http
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/genofire/hs_master-kss-monolith/lib"
|
||||
lib "github.com/genofire/hs_master-kss-monolith/lib/http"
|
||||
logger "github.com/genofire/hs_master-kss-monolith/lib/log"
|
||||
)
|
||||
|
||||
func statusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log := lib.LogHTTP(r)
|
||||
log := logger.HTTP(r)
|
||||
lib.Write(w, "running")
|
||||
log.Info("show status")
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package lib
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -1,4 +1,4 @@
|
|||
package lib
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
|
@ -1,4 +1,4 @@
|
|||
package lib
|
||||
package log
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
@ -11,16 +11,12 @@ var Log *logger.Logger
|
|||
|
||||
func init(){
|
||||
Log = logger.New()
|
||||
// Enable fallback if core logger is used:
|
||||
log.SetOutput(Log.Writer())
|
||||
}
|
||||
|
||||
func LogTimestamp(value bool) {
|
||||
logger.SetFormatter(&logger.TextFormatter{
|
||||
DisableTimestamp: !value,
|
||||
})
|
||||
}
|
||||
// LogHTTP to add information of a httprequest to log
|
||||
func LogHTTP(r *http.Request) *logger.Entry {
|
||||
// HTTP to add information of a httprequest to log
|
||||
func HTTP(r *http.Request) *logger.Entry {
|
||||
ip := r.Header.Get("X-Forwarded-For")
|
||||
if len(ip) <= 1 {
|
||||
ip = r.RemoteAddr
|
|
@ -1,4 +1,4 @@
|
|||
package lib
|
||||
package log
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
@ -10,11 +10,8 @@ import (
|
|||
func TestLog(t *testing.T) {
|
||||
assertion := assert.New(t)
|
||||
|
||||
// No values check, just if it crashed or not
|
||||
LogTimestamp(false)
|
||||
|
||||
req, _ := http.NewRequest("GET", "https://google.com/lola/duda?q=wasd", nil)
|
||||
log := LogHTTP(req)
|
||||
log := HTTP(req)
|
||||
_, ok := log.Data["remote"]
|
||||
|
||||
assertion.NotNil(ok, "remote address not set in logger")
|
Reference in New Issue