Skip to content

Commit

Permalink
feat: embed static assets (#8)
Browse files Browse the repository at this point in the history
* build(makefile): add target command to download htmx and bootstrap

* build(static): add a directory to contain the static assets

* feat(main): add embed pkg to embed static assest into the go binary

* feat(templates): replace the cdn links to the assets with the static ones
  • Loading branch information
danvergara authored Oct 31, 2023
1 parent 9f6d6bd commit aed9b0e
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 20 deletions.
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
HTMX_VERSION=1.9.6
BOOTSTRAP_VERSION=5.3.2

.PHONY: run
## run: Runs the air command.
run:
air -c .air.toml

.PHONY: download-htmx
## download-htmx: Downloads HTMX minified js file
download-htmx:
curl -o static/htmx.min.js https://unpkg.com/htmx.org@${HTMX_VERSION}/dist/htmx.min.js

.PHONY: download-bootstrap
## download-bootstrap: Downloads Bootstrap minified css/js file
download-bootstrap:
curl -o static/bootstrap.min.css https://cdn.jsdelivr.net/npm/bootstrap@${BOOTSTRAP_VERSION}/dist/css/bootstrap.min.css
curl -o static/bootstrap.min.js https://cdn.jsdelivr.net/npm/bootstrap@${BOOTSTRAP_VERSION}/dist/js/bootstrap.bundle.min.js
47 changes: 30 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"embed"
"fmt"
"html/template"
"io"
Expand All @@ -21,6 +22,14 @@ const (
uploadFileFormField = "uploadFile"
)

var (
//go:embed all:templates
templatesHTML embed.FS

//go:embed all:static
staticFiles embed.FS
)

type ConvertedFile struct {
Filename string
}
Expand All @@ -32,16 +41,16 @@ type FileFormat struct {

func index(w http.ResponseWriter, _ *http.Request) {
files := []string{
"./templates/base.tmpl",
"./templates/partials/htmx.tmpl",
"./templates/partials/style.tmpl",
"./templates/partials/nav.tmpl",
"./templates/partials/form.tmpl",
"./templates/partials/modal.tmpl",
"./templates/partials/js.tmpl",
"templates/base.tmpl",
"templates/partials/htmx.tmpl",
"templates/partials/style.tmpl",
"templates/partials/nav.tmpl",
"templates/partials/form.tmpl",
"templates/partials/modal.tmpl",
"templates/partials/js.tmpl",
}

tmpl, err := template.ParseFiles(files...)
tmpl, err := template.ParseFS(templatesHTML, files...)
if err != nil {
log.Printf("error ocurred parsing templates: %v", err)
renderError(w, "INTERNAL_ERROR", http.StatusInternalServerError)
Expand Down Expand Up @@ -123,11 +132,11 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) {
}

files := []string{
"./templates/partials/card_file.tmpl",
"./templates/partials/modal.tmpl",
"templates/partials/card_file.tmpl",
"templates/partials/modal.tmpl",
}

tmpl, err := template.ParseFiles(files...)
tmpl, err := template.ParseFS(templatesHTML, files...)
if err != nil {
log.Printf("error occurred parsing template files: %v", err)
renderError(w, "INTERNAL_ERROR", http.StatusInternalServerError)
Expand Down Expand Up @@ -162,10 +171,10 @@ func handleFileFormat(w http.ResponseWriter, r *http.Request) {
detectedFileType := http.DetectContentType(fileBytes)

files := []string{
"./templates/partials/form.tmpl",
"templates/partials/form.tmpl",
}

tmpl, err := template.ParseFiles(files...)
tmpl, err := template.ParseFS(templatesHTML, files...)
switch detectedFileType {
case "image/jpeg", "image/jpg":
formats = map[string][]FileFormat{
Expand Down Expand Up @@ -193,10 +202,10 @@ func handleModal(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("filename")

files := []string{
"./templates/partials/active_modal.tmpl",
"templates/partials/active_modal.tmpl",
}

tmpl, err := template.ParseFiles(files...)
tmpl, err := template.ParseFS(templatesHTML, files...)
if err != nil {
log.Printf("error occurred parsing template files: %v", err)
renderError(w, "INTERNAL_ERROR", http.StatusInternalServerError)
Expand All @@ -215,9 +224,13 @@ func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)

fs := http.FileServer(http.Dir(uploadPath))
fsUpload := http.FileServer(http.Dir(uploadPath))

var staticFS = http.FS(staticFiles)
fs := http.FileServer(staticFS)

r.Handle("/files/*", http.StripPrefix("/files", fs))
r.Handle("/static/*", fs)
r.Handle("/files/*", http.StripPrefix("/files", fsUpload))
r.Get("/", index)
r.Post("/upload", handleUploadFile)
r.Post("/format", handleFileFormat)
Expand Down
6 changes: 6 additions & 0 deletions static/bootstrap.min.css

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions static/bootstrap.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions static/htmx.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion templates/partials/htmx.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{define "htmx"}}
<script src="https://unpkg.com/htmx.[email protected]" integrity="sha384-FhXw7b6AlE/jyjlZH5iHa/tTe9EpJ1Y55RjcgPbjeWMskSxZt1v9qkxLJWNJaGni" crossorigin="anonymous"></script>
<script src="/static/htmx.min.js"></script>
{{end}}
2 changes: 1 addition & 1 deletion templates/partials/js.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
htmx.find('#progress').setAttribute('value', evt.detail.loaded/evt.detail.total * 100)
});
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
<script src="/static/bootstrap.min.js"></script>
{{end}}
2 changes: 1 addition & 1 deletion templates/partials/style.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{define "style"}}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link href="/static/bootstrap.min.css" rel="stylesheet"></link>
{{end}}

0 comments on commit aed9b0e

Please sign in to comment.