From 44cf0e6ed63ce43fac5bef16e943d55733f7ca54 Mon Sep 17 00:00:00 2001 From: Samir Date: Sun, 7 Jul 2024 13:22:49 +0530 Subject: [PATCH] =?UTF-8?q?patch-1:=20Updates=20=20=20Updated=20go-chi=20t?= =?UTF-8?q?o=20v5=20=20=20Removed=20cors,=20unnessacery=20=20=20Replaced?= =?UTF-8?q?=20empty=20lyric=20with=20'=E2=99=AA'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/go-lrc/main.go | 4 ++-- env/{main.go => load.go} | 0 go.mod | 4 ++-- go.sum | 6 ++---- helpers/helpers.go | 6 +++++- web/routes/routes.go | 18 ++++-------------- 6 files changed, 15 insertions(+), 23 deletions(-) rename env/{main.go => load.go} (100%) diff --git a/cmd/go-lrc/main.go b/cmd/go-lrc/main.go index b981ff6..afaa0ab 100644 --- a/cmd/go-lrc/main.go +++ b/cmd/go-lrc/main.go @@ -1,8 +1,8 @@ package main import ( - "fmt" "log" + "net" "net/http" "github.com/samocodes/go-lrc/env" @@ -19,7 +19,7 @@ func (app *Application) Serve() error { log.Printf("🚀 Server listening to port %s", port) srv := &http.Server{ - Addr: fmt.Sprintf(":%s", port), + Addr: net.JoinHostPort("localhost", port), Handler: routes.Routes(), } diff --git a/env/main.go b/env/load.go similarity index 100% rename from env/main.go rename to env/load.go diff --git a/go.mod b/go.mod index 945c7ff..818b897 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/samocodes/go-lrc go 1.22.4 require ( - github.com/go-chi/chi v1.5.5 - github.com/go-chi/cors v1.2.1 github.com/joho/godotenv v1.5.1 github.com/unrolled/secure v1.15.0 ) + +require github.com/go-chi/chi/v5 v5.1.0 diff --git a/go.sum b/go.sum index e6db298..d9e0398 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ -github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE= -github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw= -github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= -github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/unrolled/secure v1.15.0 h1:q7x+pdp8jAHnbzxu6UheP8fRlG/rwYTb8TPuQ3rn9Og= diff --git a/helpers/helpers.go b/helpers/helpers.go index 6a6a810..90c6ccd 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -40,7 +40,11 @@ func GenerateLRC(music types.Music) string { data.WriteString(fmt.Sprintf("[%s:%s]\n\n", language, music.Language)) for _, lyric := range music.Lyrics { - data.WriteString(fmt.Sprintf("[%s]%s\n", lyric.Time, lyric.Value)) + if lyric.Value == "" { + data.WriteString(fmt.Sprintf("[%s]♪\n", lyric.Time)) + } else { + data.WriteString(fmt.Sprintf("[%s]%s\n", lyric.Time, lyric.Value)) + } } return data.String() diff --git a/web/routes/routes.go b/web/routes/routes.go index ffa8988..34cde19 100644 --- a/web/routes/routes.go +++ b/web/routes/routes.go @@ -12,9 +12,9 @@ import ( "github.com/samocodes/go-lrc/helpers" "github.com/samocodes/go-lrc/types" - "github.com/go-chi/chi" - "github.com/go-chi/chi/middleware" - "github.com/go-chi/cors" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/unrolled/secure" ) @@ -25,22 +25,12 @@ func Routes() http.Handler { FrameDeny: true, ContentTypeNosniff: true, BrowserXssFilter: true, - - // Allows htmx's script to be loaded - // ContentSecurityPolicy: "default-src 'self'; script-src 'self' https://unpkg.com 'nonce-a23gbfz9e'; style-src 'self';", }) router := chi.NewRouter() router.Use(secureMiddleware.Handler) router.Use(middleware.Recoverer) router.Use(middleware.Logger) - router.Use(cors.Handler(cors.Options{ - AllowedOrigins: []string{"http://*", "https://*"}, - AllowedMethods: []string{"GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"}, - AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, - ExposedHeaders: []string{"Link"}, - MaxAge: 300, - })) router.Get("/", func(w http.ResponseWriter, r *http.Request) { // if request header doesn't accepts text/html, then return a html template @@ -73,7 +63,7 @@ func Routes() http.Handler { router.Post("/lrc", func(w http.ResponseWriter, r *http.Request) { b, err := io.ReadAll(r.Body) if err != nil { - http.Error(w, "Body was not provided", http.StatusBadRequest) + http.Error(w, "Body was not provided!", http.StatusBadRequest) return }