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 ( import (
"flag" "flag"
"log"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -10,35 +9,28 @@ import (
goji "goji.io" goji "goji.io"
http_api "github.com/genofire/hs_master-kss-monolith/http" web "github.com/genofire/hs_master-kss-monolith/http"
"github.com/genofire/hs_master-kss-monolith/lib" "github.com/genofire/hs_master-kss-monolith/lib/log"
"github.com/genofire/hs_master-kss-monolith/models" "github.com/genofire/hs_master-kss-monolith/models"
) )
var ( var (
configFile string configFile string
config *models.Config config *models.Config
timestamps bool
) )
func main() { 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.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)")
flag.Parse() flag.Parse()
// load config // load config
config = models.ReadConfigFile(configFile) config = models.ReadConfigFile(configFile)
if !timestamps { log.Log.Info("Starting rezension monolith")
log.SetFlags(0)
lib.LogTimestamp(false)
}
log.Println("Starting rezension monolith")
// Startwebsrver // Startwebsrver
router := goji.NewMux() router := goji.NewMux()
http_api.BindAPI(router) web.BindAPI(router)
srv := &http.Server{ srv := &http.Server{
Addr: config.WebserverBind, Addr: config.WebserverBind,
Handler: router, Handler: router,
@ -53,5 +45,5 @@ func main() {
// Stop services // Stop services
srv.Close() srv.Close()
log.Println("received", sig) log.Log.Info("received", sig)
} }

View File

@ -3,11 +3,12 @@ package http
import ( import (
"net/http" "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) { func statusHandler(w http.ResponseWriter, r *http.Request) {
log := lib.LogHTTP(r) log := logger.HTTP(r)
lib.Write(w, "running") lib.Write(w, "running")
log.Info("show status") log.Info("show status")
} }

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package lib package log
import ( import (
"log" "log"
@ -11,16 +11,12 @@ var Log *logger.Logger
func init(){ func init(){
Log = logger.New() Log = logger.New()
// Enable fallback if core logger is used:
log.SetOutput(Log.Writer()) log.SetOutput(Log.Writer())
} }
func LogTimestamp(value bool) { // HTTP to add information of a httprequest to log
logger.SetFormatter(&logger.TextFormatter{ func HTTP(r *http.Request) *logger.Entry {
DisableTimestamp: !value,
})
}
// LogHTTP to add information of a httprequest to log
func LogHTTP(r *http.Request) *logger.Entry {
ip := r.Header.Get("X-Forwarded-For") ip := r.Header.Get("X-Forwarded-For")
if len(ip) <= 1 { if len(ip) <= 1 {
ip = r.RemoteAddr ip = r.RemoteAddr

View File

@ -1,4 +1,4 @@
package lib package log
import ( import (
"net/http" "net/http"
@ -10,11 +10,8 @@ import (
func TestLog(t *testing.T) { func TestLog(t *testing.T) {
assertion := assert.New(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) req, _ := http.NewRequest("GET", "https://google.com/lola/duda?q=wasd", nil)
log := LogHTTP(req) log := HTTP(req)
_, ok := log.Data["remote"] _, ok := log.Data["remote"]
assertion.NotNil(ok, "remote address not set in logger") assertion.NotNil(ok, "remote address not set in logger")