genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] reorder project structur + remove timestamp from log config

This commit is contained in:
Martin Geno 2017-03-30 15:36:04 +02:00
parent 2560bafade
commit 2921580e11
7 changed files with 17 additions and 30 deletions

1
cmd/review/config.conf Symbolic link
View File

@ -0,0 +1 @@
../../config_example.conf

View File

@ -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(&timestamps, "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)
}

View File

@ -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")
}

View File

@ -1,4 +1,4 @@
package lib
package http
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package lib
package http
import (
"bytes"

View File

@ -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

View File

@ -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")