From a2536792ab0657d1471bddceb1d88b6746e2b102 Mon Sep 17 00:00:00 2001 From: Matt Majestic <33904170+mattmajestic@users.noreply.github.com> Date: Fri, 12 Apr 2024 19:21:07 -0400 Subject: [PATCH] seo optimize | static dir add --- main.go | 55 ++++++++++++++++++++++-------------- static/script.js | 16 +++++++++++ static/styles.css | 43 +++++++++++++++++++++++++++++ templates/index.tmpl | 66 ++++---------------------------------------- templates/script.js | 30 -------------------- templates/styles.css | 65 ------------------------------------------- 6 files changed, 99 insertions(+), 176 deletions(-) create mode 100644 static/script.js create mode 100644 static/styles.css delete mode 100644 templates/script.js delete mode 100644 templates/styles.css diff --git a/main.go b/main.go index 637ffdb..2078f23 100644 --- a/main.go +++ b/main.go @@ -1,32 +1,45 @@ package main import ( - "net/http" + "net/http" - "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin" ) func fetchYouTubeMetricsFromAPI(c *gin.Context) { - resp, err := http.Get("https://mattmajestic.dev/youtube-metrics") - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch YouTube metrics"}) - return - } - defer resp.Body.Close() - - c.DataFromReader(http.StatusOK, resp.ContentLength, resp.Header.Get("Content-Type"), resp.Body, nil) + resp, err := http.Get("https://mattmajestic.dev/youtube-metrics") + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch YouTube metrics"}) + return + } + defer resp.Body.Close() + c.DataFromReader(http.StatusOK, resp.ContentLength, resp.Header.Get("Content-Type"), resp.Body, nil) } func main() { - router := gin.Default() - - router.LoadHTMLGlob("templates/*") - - router.GET("/", func(c *gin.Context) { - c.HTML(http.StatusOK, "index.tmpl", nil) - }) - - router.GET("/youtube-metrics", fetchYouTubeMetricsFromAPI) - - router.Run(":8080") + // Initialize Gin router + router := gin.Default() + + // Serve static files + router.GET("/styles.css", func(c *gin.Context) { + c.File("./static/styles.css") + }) + router.GET("/script.js", func(c *gin.Context) { + c.File("./static/script.js") + }) + + // Load HTML templates + router.LoadHTMLGlob("templates/*") + + // Define routes + router.GET("/", func(c *gin.Context) { + // Render the index template + c.HTML(http.StatusOK, "index.tmpl", nil) + }) + + // Route to fetch YouTube metrics from API + router.GET("/youtube-metrics", fetchYouTubeMetricsFromAPI) + + // Run the server on port 8080 + router.Run(":8080") } diff --git a/static/script.js b/static/script.js new file mode 100644 index 0000000..bb96130 --- /dev/null +++ b/static/script.js @@ -0,0 +1,16 @@ +const body = document.body; + + // Function to toggle between light and dark themes + function toggleTheme() { + if (body.classList.contains("dark")) { + // Switch to light theme + body.classList.remove("dark"); + } else { + // Switch to dark theme + body.classList.add("dark"); + } + } + + // Add click event listener to the theme toggle button + const themeToggle = document.getElementById("theme-toggle"); + themeToggle.addEventListener("click", toggleTheme); \ No newline at end of file diff --git a/static/styles.css b/static/styles.css new file mode 100644 index 0000000..5ca8bab --- /dev/null +++ b/static/styles.css @@ -0,0 +1,43 @@ +.custom-button { + background-color: #1C3D63; + color: #fff; + border: none; + padding: 10px 20px; + border-radius: 5px; + text-decoration: none; + transition: background-color 0.3s ease; + margin-bottom: 10px; + cursor: pointer; + display: flex; + align-items: center; + justify-content: center; +} + +.custom-button:hover { + background-color: #0056b3; +} + +.theme-toggle-icon { + font-size: 24px; +} + +/* Dark Theme */ +body.dark { + background-color: #fff; + color: #1C3D63; +} + +footer { + position: fixed; + bottom: 0; + left: 0; + width: 100%; + background-color: #333; + color: #fff; + text-align: center; + padding: 8px 0; +} + +.custom-button i { + margin-right: 5px; /* Adjust the margin as needed for spacing */ +} diff --git a/templates/index.tmpl b/templates/index.tmpl index 71a2b6f..d8355a6 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -4,50 +4,13 @@