add random element function

This commit is contained in:
pluja 2024-02-14 22:13:23 +01:00
parent cb27dbb57c
commit 31d13b18c9

View File

@ -3,6 +3,7 @@ package server
import (
"context"
"html/template"
"math/rand"
"os"
"path"
"strings"
@ -90,6 +91,7 @@ func (s *Server) RegisterRoutes() {
// GET
s.Router.Get("/", s.handleIndex)
s.Router.Get("/pgp", s.handlePgp)
s.Router.Get("/about", s.handleAbout)
s.Router.Get("/pending", s.handlePending)
s.Router.Get("/service/{name:string}", s.handleService)
@ -130,6 +132,12 @@ func (s *Server) RegisterViews() {
"safe": func(s string) template.HTML {
return template.HTML(s)
},
"randomElem": func(vs []string) string {
if len(vs) == 0 {
return ""
}
return vs[rand.Intn(len(vs))]
},
"shortText": func(s string) string {
if len(s) > 50 {
return strings.TrimSpace(s[:50]) + "..."