Skip to content

Commit

Permalink
Merge pull request #23 from mbaraa/dev
Browse files Browse the repository at this point in the history
Release v0.0.2 UI and Performance Improvements!
  • Loading branch information
mbaraa authored May 18, 2024
2 parents 660881b + 285b72c commit 53bdbc2
Show file tree
Hide file tree
Showing 98 changed files with 1,410 additions and 898 deletions.
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
PORT=3000
HOSTNAME="http://localhost:${PORT}"
HOSTNAME="http://localhost:3000"
JWT_SECRET="spoikoninochi"

YOUTUBE_SCAPER_URL="http://localhost:1234"
YOUTUBE_DOWNLOADER_URL="http://localhost:4321"
YOUTUBE_MUSIC_DOWNLOAD_PATH="."
YOUTUBE_MUSIC_DOWNLOAD_PATH="./_serve/"

GOOGLE_APPLICATION_CREDENTIALS="./google-service-account.json"
GOOGLE_CLIENT_ID=""
GOOGLE_CLIENT_SECRET=""

DB_NAME=""
DB_HOST=""
DB_USERNAME=""
DB_PASSWORD=""
DB_NAME="dankabase"
DB_HOST="dank-db"
DB_USERNAME="root"
DB_PASSWORD="previetcomrade"

SMTP_HOST=""
SMTP_PORT=""
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/rex-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches:
- main
paths-ignore:
- "**.md"
pull_request:
branches:
- non-existent
Expand Down
2 changes: 1 addition & 1 deletion cmd/seeder/seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func SeedDb() error {
playlistSongsRepo = db.NewBaseDB[models.PlaylistSong](dbConn)
playlistOwnerRepo = db.NewBaseDB[models.PlaylistOwner](dbConn)

playlistService := playlistspkg.New(playlistRepo, playlistOwnerRepo, nil)
playlistService := playlistspkg.New(playlistRepo, playlistOwnerRepo, nil, nil)

pl, err := playlistService.GetAll(400)
if err != nil {
Expand Down
32 changes: 27 additions & 5 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import (
"dankmuzikk/services/youtube/search"
"embed"
"net/http"
"regexp"

"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
"github.com/tdewolff/minify/v2/json"
"github.com/tdewolff/minify/v2/svg"
"github.com/tdewolff/minify/v2/xml"
)

func StartServer(staticFS embed.FS) error {
Expand All @@ -33,16 +42,29 @@ func StartServer(staticFS embed.FS) error {
playlistSongssRepo := db.NewBaseDB[models.PlaylistSong](dbConn)

downloadService := download.New(songRepo)
playlistsService := playlists.New(playlistRepo, playlistOwnersRepo, downloadService)
songsService := songs.New(playlistSongssRepo, songRepo, playlistRepo)
playlistsService := playlists.New(playlistRepo, playlistOwnersRepo, playlistSongssRepo, downloadService)
songsService := songs.New(playlistSongssRepo, songRepo, playlistRepo, downloadService)

jwtUtil := jwt.NewJWTImpl()

gHandler := handlers.NewHandler(profileRepo, jwtUtil)

///////////// Pages and files /////////////
pagesHandler := http.NewServeMux()
pagesHandler.Handle("/static/", http.FileServer(http.FS(staticFS)))

m := minify.New()
m.AddFunc("text/css", css.Minify)
m.AddFunc("text/html", html.Minify)
m.AddFunc("image/svg+xml", svg.Minify)
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)
pagesHandler.Handle("/static/", m.Middleware(http.FileServer(http.FS(staticFS))))
pagesHandler.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
robotsFile, _ := staticFS.ReadFile("static/robots.txt")
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write(robotsFile)
})
pagesHandler.Handle("/music/", http.StripPrefix("/music", http.FileServer(http.Dir(config.Env().YouTube.MusicDir))))

pagesRouter := pages.NewPagesHandler(profileRepo, playlistsService, jwtUtil)
Expand All @@ -60,7 +82,7 @@ func StartServer(staticFS embed.FS) error {

emailLoginApi := apis.NewEmailLoginApi(login.NewEmailLoginService(accountRepo, profileRepo, otpRepo, jwtUtil))
googleLoginApi := apis.NewGoogleLoginApi(login.NewGoogleLoginService(accountRepo, profileRepo, otpRepo, jwtUtil))
songDownloadApi := apis.NewDownloadHandler(downloadService)
songDownloadApi := apis.NewDownloadHandler(downloadService, songsService)
playlistsApi := apis.NewPlaylistApi(playlistsService, songsService)

apisHandler := http.NewServeMux()
Expand All @@ -73,9 +95,9 @@ func StartServer(staticFS embed.FS) error {
apisHandler.HandleFunc("GET /logout", apis.HandleLogout)
apisHandler.HandleFunc("GET /search-suggestion", apis.HandleSearchSuggestions)
apisHandler.HandleFunc("GET /song/download", songDownloadApi.HandleDownloadSong)
apisHandler.HandleFunc("GET /song/download/queue", songDownloadApi.HandleDownloadSongToQueue)
apisHandler.HandleFunc("POST /playlist", gHandler.AuthApi(playlistsApi.HandleCreatePlaylist))
apisHandler.HandleFunc("PUT /toggle-song-in-playlist", gHandler.AuthApi(playlistsApi.HandleToggleSongInPlaylist))
apisHandler.HandleFunc("PUT /increment-song-plays", gHandler.AuthApi(songDownloadApi.HandleIncrementSongPlaysInPlaylist))

applicationHandler := http.NewServeMux()
applicationHandler.Handle("/", pagesHandler)
Expand Down
2 changes: 1 addition & 1 deletion docker-compose-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ volumes:
driver_opts:
type: none
o: bind
device: /mnt/vault/blobs/dankmuzikk/muzikk/
device: ./_serve/
1 change: 1 addition & 0 deletions entities/profile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entities

type Profile struct {
Id uint `json:"-"`
Email string
Name string
PfpLink string
Expand Down
2 changes: 2 additions & 0 deletions entities/song.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ type Song struct {
Artist string `json:"artist"`
ThumbnailUrl string `json:"thumbnail_url"`
Duration string `json:"duration"`
PlayTimes int `json:"play_times"`
AddedAt string `json:"added_at"`
}
9 changes: 0 additions & 9 deletions entities/song_download.go

This file was deleted.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-sql-driver/mysql v1.8.1
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/uuid v1.6.0
github.com/tdewolff/minify/v2 v2.20.24
golang.org/x/crypto v0.22.0
golang.org/x/oauth2 v0.19.0
google.golang.org/api v0.175.0
Expand All @@ -29,6 +30,7 @@ require (
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/tdewolff/parse/v2 v2.7.14 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.50.0 // indirect
go.opentelemetry.io/otel v1.25.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tdewolff/minify/v2 v2.20.24 h1:I4FCC5Q2YdGnmXNokZ1OkGpkO+Weao/62y5/2eQ19vo=
github.com/tdewolff/minify/v2 v2.20.24/go.mod h1:1TJni7+mATKu24cBQQpgwakrYRD27uC1/rdJOgdv8ns=
github.com/tdewolff/parse/v2 v2.7.14 h1:100KJ+QAO3PpMb3uUjzEU/NpmCdbBYz6KPmCIAfWpR8=
github.com/tdewolff/parse/v2 v2.7.14/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.50.0 h1:cEPbyTSEHlQR89XVlyo78gqluF8Y3oMeBkXGWzQsfXY=
Expand Down
4 changes: 2 additions & 2 deletions handlers/apis/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (p *playlistApi) HandleToggleSongInPlaylist(w http.ResponseWriter, r *http.
// TODO: idk, this is ugly, but it works lol
switch removeSongFromPlaylist {
case "false":
w.Write([]byte("<div class=\"w-[20px] h-[20px] rounded-sm border border-accent bg-accent\"></div>"))
w.Write([]byte("<div class=\"w-[20px] h-[20px] rounded-sm border border-secondary bg-secondary\"></div>"))
case "true":
w.Write([]byte("<div class=\"w-[20px] h-[20px] rounded-sm border border-accent\"></div>"))
w.Write([]byte("<div class=\"w-[20px] h-[20px] rounded-sm border border-secondary\"></div>"))
}
}
47 changes: 26 additions & 21 deletions handlers/apis/songs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,43 @@ package apis
import (
"dankmuzikk/entities"
"dankmuzikk/log"
"dankmuzikk/services/playlists/songs"
"dankmuzikk/services/youtube/download"
"errors"
"net/http"
"net/url"
)

type songDownloadHandler struct {
service *download.Service
service *download.Service
songsService *songs.Service
}

func NewDownloadHandler(service *download.Service) *songDownloadHandler {
return &songDownloadHandler{service}
func NewDownloadHandler(service *download.Service, songsService *songs.Service) *songDownloadHandler {
return &songDownloadHandler{service, songsService}
}

func (s *songDownloadHandler) HandleDownloadSong(w http.ResponseWriter, r *http.Request) {
song, err := s.extractSongFromQuery(r.URL.Query())
if err != nil {
func (s *songDownloadHandler) HandleIncrementSongPlaysInPlaylist(w http.ResponseWriter, r *http.Request) {
songId := r.URL.Query().Get("song-id")
if songId == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
playlistId := r.URL.Query().Get("playlist-id")
if playlistId == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
log.Errorln(err)
return
}

err = s.service.DownloadYoutubeSong(song)
err := s.songsService.IncrementSongPlays(songId, playlistId)
if err != nil {
log.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}

func (s *songDownloadHandler) HandleDownloadSongToQueue(w http.ResponseWriter, r *http.Request) {
func (s *songDownloadHandler) HandleDownloadSong(w http.ResponseWriter, r *http.Request) {
song, err := s.extractSongFromQuery(r.URL.Query())
if err != nil {
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -43,38 +48,38 @@ func (s *songDownloadHandler) HandleDownloadSongToQueue(w http.ResponseWriter, r
return
}

err = s.service.DownloadYoutubeSongQueue(song)
err = s.service.DownloadYoutubeSong(song)
if err != nil {
log.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}

func (s *songDownloadHandler) extractSongFromQuery(query url.Values) (entities.SongDownloadRequest, error) {
id := query.Get("id")
func (s *songDownloadHandler) extractSongFromQuery(query url.Values) (entities.Song, error) {
id := query.Get("yt_id")
if id == "" {
return entities.SongDownloadRequest{}, errors.New("missing song's yt id")
return entities.Song{}, errors.New("missing song's yt_id")
}
thumbnailUrl := query.Get("thumbnailUrl")
thumbnailUrl := query.Get("thumbnail_url")
if thumbnailUrl == "" {
return entities.SongDownloadRequest{}, errors.New("missing song's thumbnailUrl")
return entities.Song{}, errors.New("missing song's thumbnail_url")
}
title := query.Get("title")
if title == "" {
return entities.SongDownloadRequest{}, errors.New("missing song's title")
return entities.Song{}, errors.New("missing song's title")
}
artist := query.Get("artist")
if artist == "" {
return entities.SongDownloadRequest{}, errors.New("missing song's artist name")
return entities.Song{}, errors.New("missing song's artist name")
}
duration := query.Get("duration")
if duration == "" {
return entities.SongDownloadRequest{}, errors.New("missing song's duration")
return entities.Song{}, errors.New("missing song's duration")
}

return entities.SongDownloadRequest{
Id: id,
return entities.Song{
YtId: id,
Title: title,
Artist: artist,
ThumbnailUrl: thumbnailUrl,
Expand Down
Loading

0 comments on commit 53bdbc2

Please sign in to comment.