Skip to content

Commit

Permalink
Merge pull request #30 from mbaraa/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mbaraa authored May 21, 2024
2 parents 9bb68ba + df77ec1 commit 98378d1
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 95 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ git clone https://github.com/mbaraa/dankmuzikk
cp .env.example .env.docker
```

3. Run it with compose.
3. Run it with docker compose.

```bash
docker compose up
docker compose up -f docker-compose-dev.yml
```

3. Visit http://localhost:20250
Expand Down
4 changes: 2 additions & 2 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func StartServer(staticFS embed.FS) error {
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write(robotsFile)
})
pagesHandler.Handle("/music/", http.StripPrefix("/music", http.FileServer(http.Dir(config.Env().YouTube.MusicDir))))
pagesHandler.Handle("/muzikkx/", http.StripPrefix("/muzikkx", http.FileServer(http.Dir(config.Env().YouTube.MusicDir))))

pagesRouter := pages.NewPagesHandler(profileRepo, playlistsService, jwtUtil, &search.ScraperSearch{}, downloadService)
pagesHandler.HandleFunc("/", gHandler.OptionalAuthPage(pagesRouter.HandleHomePage))
Expand Down Expand Up @@ -94,7 +94,7 @@ func StartServer(staticFS embed.FS) error {
apisHandler.HandleFunc("/login/google/callback", googleLoginApi.HandleGoogleOAuthLoginCallback)
apisHandler.HandleFunc("GET /logout", apis.HandleLogout)
apisHandler.HandleFunc("GET /search-suggestion", apis.HandleSearchSuggestions)
apisHandler.HandleFunc("GET /song/download", songDownloadApi.HandleDownloadSong)
apisHandler.HandleFunc("GET /song", songDownloadApi.HandlePlaySong)
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))
Expand Down
39 changes: 35 additions & 4 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
version: "3.8"

services:
app:
container_name: "dankmuzikk"
build:
context: .
image: "dankmuzikk-app"
restart: "always"
ports:
- "20250:3000"
stdin_open: true
env_file:
- .env.docker
volumes:
- ./google-service-account.json:/app/google-service-account.json
- dankuploads-dir:/app/_serve
networks:
- danknetwork
depends_on:
- dank-db
- yt-scraper
- yt-dl

dank-db:
image: "mariadb:10.8"
container_name: "dank-db"
Expand All @@ -9,10 +30,12 @@ services:
MARIADB_ROOT_PASSWORD: "previetcomrade"
MARIADB_DATABASE: "dankabase"
ports:
- 3307:3306
- 3306
volumes:
- db-config:/etc/mysql
- db-data:/var/lib/mysql
networks:
- danknetwork

yt-scraper:
container_name: "yt-scraper"
Expand All @@ -21,20 +44,28 @@ services:
image: "yt-scraper"
restart: "always"
ports:
- "1234:8080"
- 8080
networks:
- danknetwork

yt-dl:
container_name: "yt-dl"
container_name: "ytdl"
build:
context: ./ytdl
image: "ytdl"
restart: "always"
ports:
- "4321:8000"
- 8000
stdin_open: true
env_file:
- .env.docker
volumes:
- dankuploads-dir:/app/_serve
networks:
- danknetwork

networks:
danknetwork: {}

volumes:
dankuploads-dir:
Expand Down
57 changes: 57 additions & 0 deletions docker-compose-services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: "3.8"

services:
dank-db:
image: "mariadb:10.8"
container_name: "dank-db"
restart: "always"
environment:
MARIADB_ROOT_PASSWORD: "previetcomrade"
MARIADB_DATABASE: "dankabase"
ports:
- 3307:3306
volumes:
- db-config:/etc/mysql
- db-data:/var/lib/mysql

yt-scraper:
container_name: "yt-scraper"
build:
context: ./ytscraper
image: "yt-scraper"
restart: "always"
ports:
- "1234:8080"

yt-dl:
container_name: "yt-dl"
build:
context: ./ytdl
image: "ytdl"
restart: "always"
ports:
- "4321:8000"
env_file:
- .env.docker
volumes:
- dankuploads-dir:/app/_serve

volumes:
dankuploads-dir:
driver: local
driver_opts:
type: none
o: bind
device: ./_serve/
db-config:
driver: local
driver_opts:
type: none
o: bind
device: ./_db/etc/
db-data:
driver: local
driver_opts:
type: none
o: bind
device: ./_db/var/
31 changes: 2 additions & 29 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,9 @@ services:
networks:
- danknetwork
depends_on:
- dank-db
- yt-scraper
- yt-dl

dank-db:
image: "mariadb:10.8"
container_name: "dank-db"
restart: "always"
environment:
MARIADB_ROOT_PASSWORD: "previetcomrade"
MARIADB_DATABASE: "dankabase"
ports:
- 3306
volumes:
- db-config:/etc/mysql
- db-data:/var/lib/mysql
networks:
- danknetwork

yt-scraper:
container_name: "yt-scraper"
build:
Expand Down Expand Up @@ -65,7 +49,8 @@ services:
- danknetwork

networks:
danknetwork: {}
danknetwork:
external: true

volumes:
dankuploads-dir:
Expand All @@ -74,15 +59,3 @@ volumes:
type: none
o: bind
device: ./_serve/
db-config:
driver: local
driver_opts:
type: none
o: bind
device: ./_db/etc/
db-data:
driver: local
driver_opts:
type: none
o: bind
device: ./_db/var/
45 changes: 5 additions & 40 deletions handlers/apis/songs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package apis

import (
"dankmuzikk/entities"
"dankmuzikk/handlers"
"dankmuzikk/log"
"dankmuzikk/services/playlists/songs"
"dankmuzikk/services/youtube/download"
"errors"
"net/http"
"net/url"
)

type songDownloadHandler struct {
Expand Down Expand Up @@ -45,50 +42,18 @@ func (s *songDownloadHandler) HandleIncrementSongPlaysInPlaylist(w http.Response
}
}

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

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

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

return entities.Song{
YtId: id,
Title: title,
Artist: artist,
ThumbnailUrl: thumbnailUrl,
Duration: duration,
}, nil
}
2 changes: 1 addition & 1 deletion handlers/pages/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (p *pagesHandler) HandleSearchResultsPage(w http.ResponseWriter, r *http.Re
var playlists []entities.Playlist
profileId, profileIdCorrect := r.Context().Value(handlers.ProfileIdKey).(uint)
if profileIdCorrect {
playlists, songsInPlaylists, _ = p.playlistsService.GetAllMappedForAddPopover(results, profileId)
playlists, songsInPlaylists, _ = p.playlistsService.GetAllMappedForAddPopover(profileId)
}

if handlers.IsNoLayoutPage(r) {
Expand Down
2 changes: 1 addition & 1 deletion services/playlists/playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (p *Service) GetAll(ownerId uint) ([]entities.Playlist, error) {
}

// TODO: fix this weird ass 3 return values
func (p *Service) GetAllMappedForAddPopover(songs []entities.Song, ownerId uint) ([]entities.Playlist, map[string]bool, error) {
func (p *Service) GetAllMappedForAddPopover(ownerId uint) ([]entities.Playlist, map[string]bool, error) {
var dbPlaylists []models.Playlist
err := p.
repo.
Expand Down
12 changes: 6 additions & 6 deletions services/youtube/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ func New(repo db.CRUDRepo[models.Song]) *Service {
//
// Used when playing a new song (usually from search).
// TODO: optimize select query, maybe?
func (d *Service) DownloadYoutubeSong(req entities.Song) error {
song, err := d.repo.GetByConds("yt_id = ?", req.YtId)
func (d *Service) DownloadYoutubeSong(songYtId string) error {
song, err := d.repo.GetByConds("yt_id = ?", songYtId)
if err == nil && len(song) != 0 && song[0].FullyDownloaded {
log.Infof("The song with id %s is already downloaded\n", req.YtId)
log.Infof("The song with id %s is already downloaded\n", songYtId)
return nil
}

err = d.DownloadYoutubeSongQueue(req.YtId)
err = d.DownloadYoutubeSongQueue(songYtId)
if err != nil {
return err
}
resp, err := http.Get(fmt.Sprintf("%s/download/%s", config.Env().YouTube.DownloaderUrl, req.YtId))
resp, err := http.Get(fmt.Sprintf("%s/download/%s", config.Env().YouTube.DownloaderUrl, songYtId))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New("something went wrong when downloading a song; id: " + req.YtId)
return errors.New("something went wrong when downloading a song; id: " + songYtId)
}

return nil
Expand Down
17 changes: 7 additions & 10 deletions static/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,12 @@ function toggleShuffle() {
}

/**
* @param {Song} song
* @param {string} songYtId
*/
async function downloadSong(song) {
if (!song) {
return;
}
return await fetch(
"/api/song/download?" + new URLSearchParams(song).toString(),
).catch((err) => console.error(err));
async function downloadSong(songYtId) {
return await fetch("/api/song?id=" + songYtId).catch((err) =>
console.error(err),
);
}

/**
Expand All @@ -362,9 +359,9 @@ async function playNewSong(song) {
document.body.style.cursor = "progress";
Utils.showLoading();

await downloadSong(song).then(() => {
await downloadSong(song.yt_id).then(() => {
stopMuzikk();
audioPlayerEl.src = `/music/${song.yt_id}.mp3`;
audioPlayerEl.src = `/muzikkx/${song.yt_id}.mp3`;
audioPlayerEl.load();
});

Expand Down

0 comments on commit 98378d1

Please sign in to comment.