bump copyright, added stats page and updated dependencies

This commit is contained in:
ston1th 2020-01-11 11:32:27 +01:00
commit 0265d820fe
111 changed files with 9556 additions and 3835 deletions

View file

@ -1,4 +1,4 @@
// Copyright (C) 2019 Marius Schellenberger
// Copyright (C) 2020 Marius Schellenberger
package server

View file

@ -1,4 +1,4 @@
// Copyright (C) 2019 Marius Schellenberger
// Copyright (C) 2020 Marius Schellenberger
package server
@ -712,7 +712,7 @@ func userNewHandler(ctx *Context) {
return
}
log.Printf("create: user %s created by %s\n", user, ctx.User())
ctx.Redirect("/users", http.StatusFound)
ctx.Redirect(core.UsersURI, http.StatusFound)
}
}
@ -754,7 +754,7 @@ func userEditHandler(ctx *Context) {
return
}
log.Printf("update: user %s updated by %s\n", user, ctx.User())
ctx.Redirect("/users", http.StatusFound)
ctx.Redirect(core.UsersURI, http.StatusFound)
return
}
if err := ctx.Srv.DB.UpdateUserPassword(user, password); err != nil {
@ -832,7 +832,7 @@ func userTotpHandler(ctx *Context) {
} else {
log.Printf("totp: enabled by %s for %s\n", user, req.Username)
}
ctx.Redirect("/user/edit/"+req.Username, http.StatusFound)
ctx.Redirect(core.UserURI+"/edit/"+req.Username, http.StatusFound)
}
}
@ -842,7 +842,7 @@ func userUnlockHandler(ctx *Context) {
log.Println("unlock:", err)
}
log.Printf("unlock: %s unlocked by %s\n", user, ctx.User())
ctx.Redirect("/users", http.StatusFound)
ctx.Redirect(core.UsersURI, http.StatusFound)
}
func userDelHandler(ctx *Context) {
@ -871,10 +871,23 @@ func userDelHandler(ctx *Context) {
}
log.Printf("delete: user %s deleted by %s\n", user, self)
if user == self {
ctx.Redirect("/logout", http.StatusFound)
ctx.Redirect(core.LogoutURI, http.StatusFound)
return
}
ctx.Redirect("/users", http.StatusFound)
ctx.Redirect(core.UsersURI, http.StatusFound)
}
}
func statsHandler(ctx *Context) {
ctx.Template("statsHandler")
ctx.Data = webData{
Title: "Stats",
BodyTitle: "Stats",
}
switch ctx.Method() {
case "GET":
ctx.Data.Data = getStats(ctx.Srv.DB.Index)
ctx.Exec()
}
}

View file

@ -1,9 +1,12 @@
// Copyright (C) 2019 Marius Schellenberger
// Copyright (C) 2020 Marius Schellenberger
package server
import (
"errors"
"git.giftfish.de/ston1th/gowiki/pkg/core"
bleve "git.giftfish.de/ston1th/gowiki/pkg/index"
"runtime"
"strconv"
"time"
)
@ -12,6 +15,8 @@ const (
day = time.Hour * 24
min = time.Second * 30
max = day * 90
div = 1048576
)
func duration(duration, mode string) (d time.Duration, err error) {
@ -43,3 +48,19 @@ func duration(duration, mode string) (d time.Duration, err error) {
}
return
}
func fmtMem(i uint64) string {
return strconv.FormatFloat(float64(i)/div, 'f', 2, 64)
}
func getStats(i *bleve.Index) *core.Stats {
mem := new(runtime.MemStats)
runtime.ReadMemStats(mem)
return &core.Stats{
Goroutines: runtime.NumGoroutine(),
Alloc: fmtMem(mem.Alloc),
Sys: fmtMem(mem.Sys),
Docs: i.DocCount(),
GoVersion: runtime.Version(),
}
}

View file

@ -1,4 +1,4 @@
// Copyright (C) 2019 Marius Schellenberger
// Copyright (C) 2020 Marius Schellenberger
package server
@ -30,127 +30,134 @@ var static = []route{
var routes = []route{
{
"/",
core.RootURI,
jwtHandler(
indexHandler),
[]string{"GET"},
},
{
"/sections",
core.SectionsURI,
jwtHandler(
sectionsHandler),
[]string{"GET"},
},
{
"/section/new",
core.SectionURI + "/new",
jwtHandler(
adminAuthHandler(
sectionNewHandler)),
[]string{"GET", "POST"},
},
{
"/section/edit/{section:[a-zA-Z0-9]+$}",
core.SectionURI + "/edit/{section:[a-zA-Z0-9]+$}",
jwtHandler(
adminAuthHandler(
sectionEditHandler)),
[]string{"GET", "POST"},
},
{
"/section/del/{section:[a-zA-Z0-9]+$}",
core.SectionURI + "/del/{section:[a-zA-Z0-9]+$}",
jwtHandler(
adminAuthHandler(
sectionDelHandler)),
[]string{"GET", "POST"},
},
{
"/search",
core.SearchURI,
jwtHandler(
searchHandler),
[]string{"GET", "POST"},
},
{
"/new",
core.NewURI,
jwtHandler(
authHandler(
pageNewHandler)),
[]string{"GET", "POST"},
},
{
"/login",
core.LoginURI,
jwtHandler(
loginHandler),
[]string{"GET", "POST"},
},
{
"/totp",
core.TotpURI,
jwtHandler(
totpAuthHandler(
loginTotpHandler)),
[]string{"GET", "POST"},
},
{
"/blacklist",
core.BlacklistURI,
jwtHandler(
authHandler(
pageBlacklistHandler)),
[]string{"GET", "POST"},
},
{
"/users",
core.UsersURI,
jwtHandler(
adminAuthHandler(
usersHandler)),
[]string{"GET"},
},
{
"/user/new",
core.UserURI + "/new",
jwtHandler(
adminAuthHandler(
userNewHandler)),
[]string{"GET", "POST"},
},
{
"/user/edit/{user:[a-zA-Z0-9]+$}",
core.UserURI + "/edit/{user:[a-zA-Z0-9]+$}",
jwtHandler(
userAuthHandler(
userEditHandler)),
[]string{"GET", "POST"},
},
{
"/user/del/{user:[a-zA-Z0-9]+$}",
core.UserURI + "/del/{user:[a-zA-Z0-9]+$}",
jwtHandler(
userAuthHandler(
userDelHandler)),
[]string{"GET", "POST"},
},
{
"/user/totp/{user:[a-zA-Z0-9]+$}",
core.UserURI + "/totp/{user:[a-zA-Z0-9]+$}",
jwtHandler(
userAuthHandler(
userTotpHandler)),
[]string{"GET", "POST"},
},
{
"/user/unlock/{user:[a-zA-Z0-9]+$}",
core.UserURI + "/unlock/{user:[a-zA-Z0-9]+$}",
jwtHandler(
adminAuthHandler(
userUnlockHandler)),
[]string{"POST"},
},
{
"/logout",
core.StatsURI,
jwtHandler(
adminAuthHandler(
statsHandler)),
[]string{"GET"},
},
{
core.LogoutURI,
jwtHandler(
logoutHandler),
[]string{"GET", "POST"},
},
{
"/view/{key}",
core.ViewURI + "/{key}",
jwtHandler(
pageHandler),
[]string{"GET"},
},
{
"/view/{key}/md",
core.ViewURI + "/{key}/md",
jwtHandler(
pageMDHandler(
pageHandler)),

View file

@ -1,4 +1,4 @@
// Copyright (C) 2019 Marius Schellenberger
// Copyright (C) 2020 Marius Schellenberger
package server

View file

@ -1,4 +1,4 @@
// Copyright (C) 2019 Marius Schellenberger
// Copyright (C) 2020 Marius Schellenberger
// This file is auto-generated by templates.sh
// using the contents of the templates directory
@ -151,6 +151,7 @@ const (
{{if .Login}}
{{if .Admin}}
<li class="nav-item"><a class="nav-link" href="/users">Users</a></li>
<li class="nav-item"><a class="nav-link" href="/stats">Stats</a></li>
{{else}}
<li class="nav-item"><a class="nav-link" href="/user/edit/{{.User}}">Profile</a></li>
{{end}}
@ -625,6 +626,39 @@ const (
{{end}}
</div>
{{end}}
{{end}}`
stats = `{{define "body"}}
<div class="page-header">
<div class="row">
<h2>{{.BodyTitle}}</h2>
</div>
</div>
<div class="row">
<table class="table table-stripped table-hover">
<tbody>
<tr>
<td><b>Goroutines</b></td>
<td>{{.Data.Goroutines}}</td>
</tr>
<tr>
<td><b>Memory usage</b></td>
<td>{{.Data.Alloc}} MiB</td>
</tr>
<tr>
<td><b>Memory allocated</b></td>
<td>{{.Data.Sys}} MiB</td>
</tr>
<tr>
<td><b>Documents</b></td>
<td>{{.Data.Docs}}</td>
</tr>
<tr>
<td><b>Go Version</b></td>
<td>{{.Data.GoVersion}}</td>
</tr>
</tbody>
</table>
</div>
{{end}}`
userDel = `{{define "body"}}
{{if .Data}}
@ -1068,6 +1102,8 @@ func (s *HTTPServer) loadTemplates() {
s.templ["userDelHandler"] = parse(index, menu, userDel)
// totp
s.templ["userTotpHandler"] = parse(index, menu, userTotp)
// stats
s.templ["statsHandler"] = parse(index, menu, stats)
if fatal {
log.Fatal("parse: ", errors.New("parsing templates failed"))

View file

@ -1,4 +1,4 @@
// Copyright (C) 2019 Marius Schellenberger
// Copyright (C) 2020 Marius Schellenberger
package server

View file

@ -1,4 +1,4 @@
// Copyright (C) 2019 Marius Schellenberger
// Copyright (C) 2020 Marius Schellenberger
package server