Add log buffer site
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"log"
|
||||
"strconv"
|
||||
"trikotwaschliste/logbuffer"
|
||||
"trikotwaschliste/models"
|
||||
)
|
||||
|
||||
@@ -61,3 +62,7 @@ func ChangeName(pool *pgxpool.Pool, id, personID string) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func Logs(queue *logbuffer.Queue) gin.H {
|
||||
return gin.H{"items": queue.ToSlice()}
|
||||
}
|
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"net/http"
|
||||
"trikotwaschliste/database"
|
||||
"trikotwaschliste/logbuffer"
|
||||
)
|
||||
|
||||
func AdminMainPage(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
@@ -22,3 +23,9 @@ func ChangeName(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
c.Redirect(302, "/admin")
|
||||
}
|
||||
}
|
||||
|
||||
func Logs(queue *logbuffer.Queue) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.HTML(http.StatusAccepted, "adminlogs.html", database.Logs(queue))
|
||||
}
|
||||
}
|
34
html/admin/adminlogs.html
Normal file
34
html/admin/adminlogs.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Trikotwaschliste - Admin</title>
|
||||
<link rel="stylesheet" type="text/css" href="/static/semantic.min.css">
|
||||
<link rel="stylesheet" type="text/css" href="/static/style.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
{{template "navbaradmin"}}
|
||||
<div class="ui center">
|
||||
<h1 class="">Logs Admin<small>Die letzten 50 Einträge</small></h1>
|
||||
<table class="ui celled table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Logs</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .items}}
|
||||
<tr>
|
||||
<td>
|
||||
{{.}}
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
@@ -1,6 +1,7 @@
|
||||
{{define "navbaradmin"}}
|
||||
<div class="ui menu">
|
||||
<a href="/admin"><div class="header item">Trikotwaschliste Admin</div></a>
|
||||
<a href="/admin/logs"><div class="header item">Logs</div></a>
|
||||
|
||||
<div class="right menu">
|
||||
<a href="/credits"><div class="item">Credits</div></a>
|
||||
|
33
logbuffer/logbuffer.go
Normal file
33
logbuffer/logbuffer.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package logbuffer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UseLogBuffer(queue *Queue) gin.HandlerFunc {
|
||||
return gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
output := fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
|
||||
param.ClientIP,
|
||||
param.TimeStamp.Format(time.RFC1123),
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.Request.Proto,
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
param.Request.UserAgent(),
|
||||
param.ErrorMessage,
|
||||
)
|
||||
|
||||
// Write the output to our queue
|
||||
queue.push(output)
|
||||
queue.Limit(50)
|
||||
|
||||
// Return nothing
|
||||
return ""
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
44
logbuffer/queue.go
Normal file
44
logbuffer/queue.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package logbuffer
|
||||
|
||||
import "container/list"
|
||||
|
||||
type Queue struct {
|
||||
queue *list.List
|
||||
}
|
||||
|
||||
func New() *Queue{
|
||||
return &Queue{queue: list.New()}
|
||||
}
|
||||
|
||||
func (q Queue)push(value interface{}) {
|
||||
q.queue.PushBack(value)
|
||||
}
|
||||
|
||||
func (q Queue)pop() interface{} {
|
||||
element := q.queue.Front()
|
||||
value := element.Value
|
||||
q.queue.Remove(element)
|
||||
return value
|
||||
}
|
||||
|
||||
func (q Queue)Length() int {
|
||||
return q.queue.Len()
|
||||
}
|
||||
|
||||
// Limit the amount of items in the queue
|
||||
func (q Queue)Limit(items int) {
|
||||
for q.Length() > items {
|
||||
// Remove items
|
||||
q.pop()
|
||||
}
|
||||
}
|
||||
|
||||
func (q Queue)ToSlice() []string {
|
||||
var slice []string
|
||||
element := q.queue.Front()
|
||||
for i := 0; i < q.Length(); i++ {
|
||||
slice = append(slice, element.Value.(string))
|
||||
element = element.Next()
|
||||
}
|
||||
return slice
|
||||
}
|
8
main.go
8
main.go
@@ -9,6 +9,7 @@ import (
|
||||
"log"
|
||||
"time"
|
||||
"trikotwaschliste/database/migrations"
|
||||
"trikotwaschliste/logbuffer"
|
||||
"trikotwaschliste/models"
|
||||
"trikotwaschliste/routes"
|
||||
)
|
||||
@@ -27,11 +28,16 @@ func main() {
|
||||
//Start postgres connection
|
||||
pool := createDatabasePool(cfg)
|
||||
|
||||
// Init the logging buffer for online log view
|
||||
logbuf := logbuffer.New()
|
||||
r.Use(logbuffer.UseLogBuffer(logbuf))
|
||||
|
||||
r.LoadHTMLGlob("html/**/*")
|
||||
r.Static("/static", "html/static")
|
||||
//r.StaticFile("/favicon.ico", "html/favicon.ico")
|
||||
//routes.RoutesInit(r, pool)
|
||||
routes.RoutesInit(r, pool, &cfg)
|
||||
routes.RoutesInit(r, pool, &cfg, logbuf)
|
||||
|
||||
|
||||
err = r.Run("0.0.0.0:8082")
|
||||
|
||||
|
@@ -4,10 +4,11 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"trikotwaschliste/handler"
|
||||
"trikotwaschliste/logbuffer"
|
||||
"trikotwaschliste/models"
|
||||
)
|
||||
|
||||
func RoutesInit(router *gin.Engine, pool *pgxpool.Pool, config *models.Config) {
|
||||
func RoutesInit(router *gin.Engine, pool *pgxpool.Pool, config *models.Config, logbuf *logbuffer.Queue) {
|
||||
router.GET("/", handler.MainPage(pool))
|
||||
router.GET("/credits", handler.Credits())
|
||||
router.POST("/uploadname", handler.UploadName(pool))
|
||||
@@ -22,5 +23,6 @@ func RoutesInit(router *gin.Engine, pool *pgxpool.Pool, config *models.Config) {
|
||||
// Basic auth routes
|
||||
authorized.GET("/", handler.AdminMainPage(pool))
|
||||
authorized.POST("/changename", handler.ChangeName(pool))
|
||||
authorized.GET("/logs", handler.Logs(logbuf))
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user