-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: updated all the routes to inject a script to live-reload when i…
…n DEV mode
- Loading branch information
1 parent
e6bac47
commit 88e44ad
Showing
4 changed files
with
51 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package utils | ||
|
||
import ( | ||
"html/template" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/wavly/shawty/asserts" | ||
"github.com/wavly/shawty/config" | ||
) | ||
|
||
const script = `<script src="/static/live-reload.js"></script>` | ||
|
||
// Injects a script into the [Template File] to live-reload the page if in `DEV` mode, | ||
// else just returns the file as it is. | ||
func Templ(path string) *template.Template { | ||
if config.ENV != "dev" { | ||
return template.Must(template.ParseFiles(path)) | ||
} | ||
content, err := os.ReadFile(path) | ||
asserts.NoErr(err, "Failed to read file") | ||
|
||
content = append(content, []byte(script)...) | ||
ret, err := template.New(filepath.Base(path)).Parse(string(content)) | ||
asserts.NoErr(err, "Faield to parse template") | ||
return ret | ||
} | ||
|
||
// Injects a script into the [Static File] to live-reload the page if in `DEV` mode, | ||
// else just return the file as it is. | ||
func StaticFile(path string) []byte { | ||
if config.ENV != "dev" { | ||
ret, err := os.ReadFile(path) | ||
asserts.NoErr(err, "Failed to read file") | ||
return ret | ||
} | ||
|
||
content, err := os.ReadFile(path) | ||
asserts.NoErr(err, "Failed to read file") | ||
|
||
content = append(content, []byte(script)...) | ||
return content | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
package utils | ||
|
||
import ( | ||
"html/template" | ||
"net/http" | ||
|
||
"github.com/wavly/shawty/asserts" | ||
) | ||
|
||
func ServerErrTempl(w http.ResponseWriter, msg string) { | ||
templ := template.Must(template.ParseFiles("templs/server-error.html")) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
templ := Templ("templs/server-error.html") | ||
asserts.NoErr(templ.Execute(w, msg), "Failed to execute template server-error.html") | ||
} |