Skip to content

Commit

Permalink
feat: updated all the routes to inject a script to live-reload when i…
Browse files Browse the repository at this point in the history
…n DEV mode
  • Loading branch information
Hamza12700 committed Oct 14, 2024
1 parent e6bac47 commit 88e44ad
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
10 changes: 4 additions & 6 deletions handlers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"database/sql"
"html/template"
"log"
"net/http"

Expand All @@ -28,7 +27,7 @@ func Stats(w http.ResponseWriter, r *http.Request) {
return
}

templ := template.Must(template.ParseFiles("./templs/stat.html"))
templ := utils.Templ("./templs/stat.html")
db := utils.ConnectDB()
defer db.Close()
queries := database.New(db)
Expand All @@ -41,19 +40,18 @@ func Stats(w http.ResponseWriter, r *http.Request) {
return
}

notFoundTempl := template.Must(template.ParseFiles("./templs/404.html"))
w.WriteHeader(http.StatusNotFound)
notFoundTempl := utils.Templ("./templs/404.html")
notFoundTempl.Execute(w, nil)
return
}

data := AccessCount{
Count: shortLinkInfo.AccessedCount,
LastAccessed: timediff.TimeDiff(shortLinkInfo.LastAccessed.Time),
OriginalUrl: shortLinkInfo.OriginalUrl,
OriginalUrl: shortLinkInfo.OriginalUrl,

ShortLink: ShortLink{
ShortUrl: inputCode,
ShortUrl: inputCode,
},
}

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func main() {
router.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))

// Route for index page
router.Handle("GET /", http.FileServer(http.Dir("./static/")))
router.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
w.Write(utils.StaticFile("./static/index.html"))
})

// Reading the URLS-SQL schema file
fileBytes, err := os.ReadFile("./schema/urls.sql")
Expand Down
43 changes: 43 additions & 0 deletions utils/live-reload.go
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
}
4 changes: 1 addition & 3 deletions utils/server_error_templ.go
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")
}

0 comments on commit 88e44ad

Please sign in to comment.