Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopiovanello committed Jan 15, 2024
1 parent 4b740f5 commit f902c4e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
2 changes: 1 addition & 1 deletion internal/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func index(tmpls *map[string]*template.Template, sc *config.SafeConfig) http.Han
data["System"] = sc.Cfg.System

if err := tmpl.Execute(w, data); err != nil {
http.Error(w, "cannot execute template", http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ func parseTemplates() error {
continue
}

parsed, err := template.ParseFS(files, "tmpl/"+tmpl.Name(), "tmpl/layouts/*.html")
parsed, err := template.ParseFS(
files,
"tmpl/"+tmpl.Name(),
"tmpl/layouts/*.html",
"tmpl/fragments/*.html",
)
if err != nil {
return fmt.Errorf("cannot parse template %s, err: %w", tmpl.Name(), err)
}

log.Println("parsed", tmpl.Name())

tmpls[tmpl.Name()] = parsed
}

Expand Down
12 changes: 12 additions & 0 deletions tmpl/fragments/service.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{define "service"}}
<a href="{{$.Url}}"
class="backdrop-blur-md bg-neutral-300/40 hover:bg-neutral-200/40 duration-100 p-3 rounded shadow-md">
<div class="flex flex-row items-center gap-4">
<img src="https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/svg/{{.Icon}}" class="h-8 w-8">
<div class="flex flex-col gap-1 text-neutral-50">
<h2 class="text-md">{{$.Name}}</h2>
<h3 class="text-xs">{{$.Description}}</h3>
</div>
</div>
</a>
{{end}}
44 changes: 4 additions & 40 deletions tmpl/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ <h3 class="text-xl text-neutral-100/95 my-4">
</h3>
<div class="grid grid-cols-4 gap-4 mb-4">
{{range $_, $element := .Media}}
<a href="{{$element.Url}}"
class="backdrop-blur-md bg-neutral-300/40 hover:bg-neutral-200/40 duration-100 p-3 rounded shadow-md">
<div class="flex flex-row items-center gap-4">
<img src="https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/svg/{{.Icon}}" class="h-8 w-8">
<div class="flex flex-col gap-1 text-neutral-50">
<h2 class="text-md">{{$element.Name}}</h2>
<h3 class="text-xs">{{$element.Description}}</h3>
</div>
</div>
</a>
{{template "service" $element}}
{{end}}
</div>

Expand All @@ -30,16 +21,7 @@ <h3 class="text-xl text-neutral-100/95 my-4">
</h3>
<div class="grid grid-cols-4 gap-4 mb-4">
{{range $_, $element := .Arr}}
<a href="{{$element.Url}}"
class="backdrop-blur-md bg-neutral-300/40 hover:bg-neutral-200/40 duration-100 p-3 rounded shadow-md">
<div class="flex flex-row items-center gap-4">
<img src="https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/svg/{{.Icon}}" class="h-8 w-8">
<div class="flex flex-col gap-1 text-neutral-50">
<h2 class="text-md">{{$element.Name}}</h2>
<h3 class="text-xs">{{$element.Description}}</h3>
</div>
</div>
</a>
{{template "service" $element}}
{{end}}
</div>

Expand All @@ -48,16 +30,7 @@ <h3 class="text-xl text-neutral-100/95 my-4">
</h3>
<div class="grid grid-cols-4 gap-4 mb-4">
{{range $_, $element := .Downloaders}}
<a href="{{$element.Url}}"
class="backdrop-blur-md bg-neutral-300/40 hover:bg-neutral-200/40 duration-100 p-3 rounded shadow-md">
<div class="flex flex-row items-center gap-4">
<img src="https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/svg/{{.Icon}}" class="h-8 w-8">
<div class="flex flex-col gap-1 text-neutral-50">
<h2 class="text-md">{{$element.Name}}</h2>
<h3 class="text-xs">{{$element.Description}}</h3>
</div>
</div>
</a>
{{template "service" $element}}
{{end}}
</div>

Expand All @@ -66,16 +39,7 @@ <h3 class="text-xl text-neutral-100/95 my-4">
</h3>
<div class="grid grid-cols-4 gap-4 mb-4">
{{range $_, $element := .System}}
<a href="{{$element.Url}}"
class="backdrop-blur-md bg-neutral-300/40 hover:bg-neutral-200/40 duration-100 p-3 rounded shadow-md">
<div class="flex flex-row items-center gap-4">
<img src="https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/svg/{{.Icon}}" class="h-8 w-8">
<div class="flex flex-col gap-1 text-neutral-50">
<h2 class="text-md">{{$element.Name}}</h2>
<h3 class="text-xs">{{$element.Description}}</h3>
</div>
</div>
</a>
{{template "service" $element}}
{{end}}
</div>

Expand Down

0 comments on commit f902c4e

Please sign in to comment.