Working access logs, .env file loading

This commit is contained in:
Kirby 2024-03-14 16:26:07 +01:00
parent a2109224c2
commit 06d85229d7
4 changed files with 23 additions and 1 deletions

4
.env.example Normal file
View File

@ -0,0 +1,4 @@
DATABASE_HOST=
DATABASE_USER=
DATABASE_PASSWORD=
DATABASE_NAME=

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
.idea/* .idea/*
*.swp *.swp
*.log *.log
.env
infra-dashboard infra-dashboard

View File

@ -3,13 +3,30 @@ package http
import ( import (
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"time"
) )
func RequestHandler(w http.ResponseWriter, r *http.Request) { func RequestHandler(w http.ResponseWriter, r *http.Request) {
t := time.Now()
logRequest(t, r, 200)
fmt.Fprint(w, "Hello, World!") fmt.Fprint(w, "Hello, World!")
} }
func HealthHandler(w http.ResponseWriter, _ *http.Request) { func HealthHandler(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "OK") _, _ = io.WriteString(w, "OK")
} }
func logRequest(t time.Time, r *http.Request, s int) {
log.Printf("%s - - %s \"%s %s %s\" %d 0 \"-\" \"%s\" %d\n",
r.Host,
t.Format("[02/Jan/2006:15:04:05 -0700]"),
r.Method,
r.URL.Path,
r.Proto,
s,
r.UserAgent(),
time.Since(t).Milliseconds(),
)
}

View File

@ -13,7 +13,7 @@ import (
var ( var (
args struct { args struct {
Log string `arg:"-l,--logfile" help:"location of output logfile." default:"/app/http-broadcaster.log"` Log string `arg:"-l,--logfile" help:"location of output logfile." default:"infra-dashboard-access.log"`
EnvFile string `arg:"-e,--envfile" help:"location of file containing environment variables." default:".env"` EnvFile string `arg:"-e,--envfile" help:"location of file containing environment variables." default:".env"`
Metrics bool `arg:"--metrics" help:"enable prometheus exporter on /metrics." default:"false"` Metrics bool `arg:"--metrics" help:"enable prometheus exporter on /metrics." default:"false"`
} }