Add stuff

This commit is contained in:
Henri Burau 2024-06-06 21:30:29 +02:00
parent 72dce76e2b
commit ddbfc32fb1
13 changed files with 77 additions and 20 deletions

1
.env
View File

@ -1 +1,2 @@
LISTEN_ADDR=":8080" LISTEN_ADDR=":8080"
DB_LOC="local.db"

13
Dockerfile Normal file
View File

@ -0,0 +1,13 @@
FROM golang:1.22 AS build-stage
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . /app
RUN CGO_ENABLED=0 GOOS=linux go build -o /entrypoint
FROM gcr.io/distroless/static-debian11 AS release-stage
WORKDIR /
COPY --from=build-stage /entrypoint /entrypoint
COPY --from=build-stage /app/assets /assets
EXPOSE 8080
ENTRYPOINT ["/entrypoint"]

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
"log/slog" "log/slog"
@ -17,12 +18,16 @@ import (
func main() { func main() {
if err := godotenv.Load(); err != nil { if err := godotenv.Load(); err != nil {
if errors.Is(err, os.ErrNotExist) {
slog.Info(".env not found using env")
} else {
log.Fatal(err) log.Fatal(err)
} }
}
router := chi.NewMux() router := chi.NewMux()
persistence, err := models.NewSQLitePersistence("local.db") persistence, err := models.NewSQLitePersistence(os.Getenv("DB_LOC"))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -51,7 +56,7 @@ func main() {
go scanner.Scan() go scanner.Scan()
router.Use(routes.PathMiddleware, routes.AuthMiddleware) router.Use(routes.LogMiddleware, routes.PathMiddleware, routes.AuthMiddleware)
router.Handle("/*", public()) router.Handle("/*", public())
router.Get("/health", routes.Make(server.HandleHealth)) router.Get("/health", routes.Make(server.HandleHealth))
router.Get("/server/status", routes.Make(server.HandleServerStatus)) router.Get("/server/status", routes.Make(server.HandleServerStatus))

View File

@ -58,11 +58,15 @@ func (msl MapScoreList) BuildTable() *ResultTable {
userMapRows := make(map[string]int) userMapRows := make(map[string]int)
rt.Header = append(rt.Header, make([]ResultTableHeader, len(msl))...)
for mapIndex, mapScore := range msl { for mapIndex, mapScore := range msl {
rt.Header = append(rt.Header, ResultTableHeader{ reversedIndex := len(msl) - 1 - mapIndex
rt.Header[reversedIndex+2] = ResultTableHeader{
Title: mapScore.Map, Title: mapScore.Map,
Subtitle: timediff.TimeDiff(mapScore.StartTime), Subtitle: timediff.TimeDiff(mapScore.StartTime),
}) }
for _, score := range mapScore.ScoreList { for _, score := range mapScore.ScoreList {
if score.Ping > 0 { if score.Ping > 0 {
if _, ok := userMapRows[score.Name]; !ok { if _, ok := userMapRows[score.Name]; !ok {
@ -77,7 +81,7 @@ func (msl MapScoreList) BuildTable() *ResultTable {
row := userMapRows[score.Name] row := userMapRows[score.Name]
rt.Rows[row].Total = rt.Rows[row].Total + score.Score rt.Rows[row].Total = rt.Rows[row].Total + score.Score
rt.Rows[row].Individual[mapIndex] = score.Score rt.Rows[row].Individual[reversedIndex] = score.Score
} }
} }
} }

View File

@ -2,6 +2,7 @@ package routes
import ( import (
"context" "context"
"errors"
"log/slog" "log/slog"
"net/http" "net/http"
@ -27,10 +28,12 @@ func OnlyAuthenticatedMiddleware(next http.Handler) http.Handler {
func AuthMiddleware(next http.Handler) http.Handler { func AuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, err := getSession(r) session, err := getSession(r)
username := ""
if err != nil { if err != nil {
if !errors.Is(err, http.ErrNoCookie) {
slog.Error("error getting token", "err", err) slog.Error("error getting token", "err", err)
} }
username := "" }
if session != nil { if session != nil {
username = session.Username username = session.Username
} }

15
routes/log_middleware.go Normal file
View File

@ -0,0 +1,15 @@
package routes
import (
"log/slog"
"net/http"
)
func LogMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Info("request", "method", r.Method, "path", r.URL.Path, "origin", r.RemoteAddr)
next.ServeHTTP(w, r)
})
}

View File

@ -1 +1,12 @@
import 'htmx.org' import 'htmx.org'
export function copyToClipboard(text: string): void {
navigator.clipboard.writeText(text).then(
() => {
},
(er) => {
console.log("failed to write to clipboard", er)
}
)
}

View File

@ -25,7 +25,7 @@ templ Capture(capture *models.Capture, table models.ResultTable) {
</div> </div>
<div class="flex w-full justify-around items-center gap-5"> <div class="flex w-full justify-around items-center gap-5">
if len(table.Rows) >= 1 { if len(table.Rows) >= 1 {
@Medal("1. Platz", table.Rows[0].Name, "text-yellow-600") @Medal("1. Platz", table.Rows[0].Name, "text-yellow-400")
} }
if len(table.Rows) >= 2 { if len(table.Rows) >= 2 {
@Medal("2. Platz", table.Rows[1].Name, "text-gray-500") @Medal("2. Platz", table.Rows[1].Name, "text-gray-500")

View File

@ -74,7 +74,7 @@ func Capture(capture *models.Capture, table models.ResultTable) templ.Component
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
if len(table.Rows) >= 1 { if len(table.Rows) >= 1 {
templ_7745c5c3_Err = Medal("1. Platz", table.Rows[0].Name, "text-yellow-600").Render(ctx, templ_7745c5c3_Buffer) templ_7745c5c3_Err = Medal("1. Platz", table.Rows[0].Name, "text-yellow-400").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

View File

@ -2,6 +2,7 @@ package components
import "gitea.henriburau.de/haw-lan/cod4watcher/views" import "gitea.henriburau.de/haw-lan/cod4watcher/views"
import "strings" import "strings"
import "fmt"
templ Nagivation() { templ Nagivation() {
<nav class="bg-gray-800"> <nav class="bg-gray-800">
@ -28,7 +29,10 @@ templ Nagivation() {
</div> </div>
<div class="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0"> <div class="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">
if views.Username(ctx) != "" { if views.Username(ctx) != "" {
<a href="/logout" class="text-gray-300 hover:bg-gray-700 hover:text-white rounded-md px-3 py-2 text-sm font-medium">{ strings.ToUpper(views.Username(ctx)) }</a> <div class="bg-gray-300 rounded-full mx-3 w-6 h-6 flex items-center justify-center">
<span class="block text-gray-800 font-bold">{ fmt.Sprintf("%c", strings.ToUpper(views.Username(ctx))[0]) }</span>
</div>
<a href="/logout" class="text-gray-300 hover:bg-gray-700 hover:text-white rounded-md px-3 py-2 text-sm font-medium">Logout</a>
} else { } else {
<a href="/login" class="text-gray-300 hover:bg-gray-700 hover:text-white rounded-md px-3 py-2 text-sm font-medium">Login</a> <a href="/login" class="text-gray-300 hover:bg-gray-700 hover:text-white rounded-md px-3 py-2 text-sm font-medium">Login</a>
} }

View File

@ -12,6 +12,7 @@ import "bytes"
import "gitea.henriburau.de/haw-lan/cod4watcher/views" import "gitea.henriburau.de/haw-lan/cod4watcher/views"
import "strings" import "strings"
import "fmt"
func Nagivation() templ.Component { func Nagivation() templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
@ -46,20 +47,20 @@ func Nagivation() templ.Component {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
if views.Username(ctx) != "" { if views.Username(ctx) != "" {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<a href=\"/logout\" class=\"text-gray-300 hover:bg-gray-700 hover:text-white rounded-md px-3 py-2 text-sm font-medium\">") _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"bg-gray-300 rounded-full mx-3 w-6 h-6 flex items-center justify-center\"><span class=\"block text-gray-800 font-bold\">")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
var templ_7745c5c3_Var2 string var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(strings.ToUpper(views.Username(ctx))) templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%c", strings.ToUpper(views.Username(ctx))[0]))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/components/navbar.templ`, Line: 31, Col: 160} return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/components/navbar.templ`, Line: 33, Col: 111}
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</a>") _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</span></div><a href=\"/logout\" class=\"text-gray-300 hover:bg-gray-700 hover:text-white rounded-md px-3 py-2 text-sm font-medium\">Logout</a>")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

View File

@ -7,9 +7,9 @@ templ ServerAddress(host, port string) {
<span class="block"> <span class="block">
{ host }:{ port } { host }:{ port }
</span> </span>
<a class="block" href="#"> <button>
<i class="fa-regular fa-clipboard"></i> <i class="fa-regular fa-clipboard"></i>
</a> </button>
<a class="block" href={ templ.SafeURL(fmt.Sprintf("cod4://%s:%s", host, port)) }> <a class="block" href={ templ.SafeURL(fmt.Sprintf("cod4://%s:%s", host, port)) }>
<i class="block fa-solid fa-play"></i> <i class="block fa-solid fa-play"></i>
</a> </a>

View File

@ -51,7 +51,7 @@ func ServerAddress(host, port string) templ.Component {
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</span> <a class=\"block\" href=\"#\"><i class=\"fa-regular fa-clipboard\"></i></a> <a class=\"block\" href=\"") _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</span> <button><i class=\"fa-regular fa-clipboard\"></i></button> <a class=\"block\" href=\"")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }