Skip to content

Commit

Permalink
Use html/template for page generation
Browse files Browse the repository at this point in the history
  • Loading branch information
patapancakes committed Jul 23, 2024
1 parent 7a4f0c8 commit abdc63b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 64 deletions.
97 changes: 33 additions & 64 deletions ingame/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,35 @@ package browser

import (
"fmt"
"html/template"
"net/http"
"reboxed/db"
"reboxed/utils"
"strconv"
)

var categories = map[string]string{
"entities": "entity",
"weapons": "weapon",
"props": "prop",
"saves": "savemap",
"maps": "map",
type BrowserTemplateData struct {
InGame bool
Category string
PageNum int
Packages []utils.Package
PrevLink string
NextLink string
}

const header = `<html>
<title>reboxed</title>
<style>
body {margin: 0px; font-family: Helvetica; background-color: #36393D; color: #EEE; padding-bottom: 50px;}
a {color: #FFF; text-decoration: none;}
a:hover {color: #0AF;}
.nav {padding: 8px; background-color: #4096EE; height: 20px; border-bottom: 1px solid #90C6FE; box-shadow: 0px 16px 16px rgba(0, 0, 0, 0.1);}
.nav a {margin: 20px; font-size: 20px; font-weight: bolder;}
.logo h1 {padding-right: 20px; margin: 0px; font-size: 20px; font-style: italic; float: right; color: #FFF;}
.content {padding: 16px 8px;}
.pagenav {padding: 0px 20px; float: right;}
.pagenav a {margin: 8px; font-weight: bolder;}
.item {margin-left: 2px; margin-right: 2px; display: inline-block; font-size: 11px; font-weight: bolder; width: 128px; height: 125px; text-align: center; text-shadow: 1px 1px 1px #000; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; letter-spacing: -0.1px;}
.item img {width: 128px; height: 100px;}
.thumb {background-position: center;}
</style>
`

const itemsPerPage = 50

var (
categories = map[string]string{
"entities": "entity",
"weapons": "weapon",
"props": "prop",
"saves": "savemap",
"maps": "map",
}
t, _ = template.New("Browser").Parse(tmpl)
)

func Handle(w http.ResponseWriter, r *http.Request) {
var category string
category, ok := categories[r.PathValue("category")]
Expand All @@ -62,20 +57,6 @@ func Handle(w http.ResponseWriter, r *http.Request) {
return
}

body := header

body += `<div class="nav"><div class="logo"><h1>reboxed</h1></div>`

if r.Header.Get("GMOD_VERSION") != "" { // ingame
if category != "map" { // don't show nav buttons if browsing maps
body += `<a href="/browse/entities">Entities</a><a href="/browse/weapons">Weapons</a><a href="/browse/props">Props</a><a href="/browse/saves">Saves</a>`
}
} else { // not ingame
body += `<a href="/browse/entities">Entities</a><a href="/browse/weapons">Weapons</a><a href="/browse/props">Props</a><a href="/browse/saves">Saves</a><a href="/browse/maps">Maps</a>`
}

body += `</div><div class="content">`

page, _ := strconv.Atoi(r.URL.Query().Get("page"))
if page < 1 {
page = 1
Expand All @@ -87,38 +68,26 @@ func Handle(w http.ResponseWriter, r *http.Request) {
return
}

for _, pkg := range list {
action := "spawn"
if category == "map" {
action = "install"
}

if category == "savemap" {
if r.Header.Get("MAP") != "" && r.Header.Get("MAP") != pkg.Dataname {
continue
}
}

link := fmt.Sprintf(`href="garrysmod://%s/%s/%d/%d"`, action, category, pkg.ID, pkg.Revision)

body += fmt.Sprintf(`<div class="item"><a %s><div class="thumb" style="background-image: url(//image.reboxed.fun/%d_thumb_128.png), url(//image.reboxed.fun/no_thumb_128.png);"><img src="//image.reboxed.fun/overlay_128.png"></div>%s</a></div>`, link, pkg.ID, pkg.Name)
}

body += "</div>"

previous := fmt.Sprintf("?page=%d", page-1)
prev := fmt.Sprintf("?page=%d", page-1)
if page <= 1 {
previous = "#"
prev = "#"
}

next := fmt.Sprintf("?page=%d", page+1)
if len(list) < itemsPerPage {
next = "#"
}

body += fmt.Sprintf(`<div class="pagenav"><a href="%s">Previous</a>%d<a href="%s">Next</a></div>`, previous, page, next)

body += "</html>"

w.Write([]byte(body))
err = t.Execute(w, BrowserTemplateData{
InGame: r.Header.Get("GMOD_VERSION") != "",
Category: category,
PageNum: page,
Packages: list,
PrevLink: prev,
NextLink: next,
})
if err != nil {
utils.WriteError(w, r, fmt.Sprintf("failed to execute template: %s", err))
return
}
}
48 changes: 48 additions & 0 deletions ingame/browser/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package browser

const tmpl = `<html>
<head>
<title>reboxed</title>
<style>
body {margin: 0px; font-family: Helvetica; background-color: #36393D; color: #EEE; padding-bottom: 50px;}
a {color: #FFF; text-decoration: none;}
a:hover {color: #0AF;}
.nav {padding: 8px; background-color: #4096EE; height: 20px; border-bottom: 1px solid #90C6FE; box-shadow: 0px 16px 16px rgba(0, 0, 0, 0.1);}
.nav a {margin: 20px; font-size: 20px; font-weight: bolder;}
.logo h1 {padding-right: 20px; margin: 0px; font-size: 20px; font-style: italic; float: right; color: #FFF;}
.content {padding: 16px 8px;}
.pagenav {padding: 0px 20px; float: right;}
.pagenav a {margin: 8px; font-weight: bolder;}
.item {margin-left: 2px; margin-right: 2px; display: inline-block; font-size: 11px; font-weight: bolder; width: 128px; height: 125px; text-align: center; text-shadow: 1px 1px 1px #000; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; letter-spacing: -0.1px;}
.item img {width: 128px; height: 100px;}
.thumb {background-position: center;}
</style>
</head>
<body>
<div class="nav">
<div class="logo"><h1>reboxed</h1></div>
{{if .InGame}}
{{if (.Category | eq "maps")}}
<a href="/browse/entities">Entities</a><a href="/browse/weapons">Weapons</a><a href="/browse/props">Props</a><a href="/browse/saves">Saves</a>
{{end}}
{{else}}
<a href="/browse/entities">Entities</a><a href="/browse/weapons">Weapons</a><a href="/browse/props">Props</a><a href="/browse/saves">Saves</a><a href="/browse/maps">Maps</a>
{{end}}
</div>
<div class="content">
{{range .Packages}}
<div class="item">
<a href="garrysmod://{{if (.Type | eq "map")}}install{{else}}spawn{{end}}/{{.Type}}/{{.ID}}/{{.Revision}}">
<div class="thumb" style="background-image: url(//image.reboxed.fun/{{.ID}}_thumb_128.png), url(//image.reboxed.fun/no_thumb_128.png);">
<img src="//image.reboxed.fun/overlay_128.png">
</div>
{{.Name}}
</a>
</div>
{{end}}
</div>
<div class="pagenav">
<a href="{{.PrevLink}}">Previous</a>{{.PageNum}}<a href="{{.NextLink}}">Next</a>
</div>
</body>
</html>`

0 comments on commit abdc63b

Please sign in to comment.