Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch-1: Updates #2

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/go-lrc/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"fmt"
"log"
"net"
"net/http"

"github.com/samocodes/go-lrc/env"
Expand All @@ -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(),
}

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
6 changes: 5 additions & 1 deletion helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 4 additions & 14 deletions web/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
Loading