Skip to content

Commit 361c5a7

Browse files
committed
setup for nginx
1 parent 7237de4 commit 361c5a7

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

MyMusicBoxApi/configuration/util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var Config models.Config
1111
func LoadConfiguration() {
1212
flag.StringVar(&Config.DevPort, "port", "", "-port=8081")
1313
flag.BoolVar(&Config.UseDevUrl, "devurl", false, "-devurl")
14+
flag.BoolVar(&Config.UsePlayUrl, "usePlayUrl", false, "-usePlayUrl")
15+
flag.BoolVar(&Config.UseImageUrl, "useImageUrl", false, "-useImageUrl")
1416
flag.StringVar(&Config.SourceFolder, "sourceFolder", "music", "-sourceFolder=/path to source folder/")
1517
flag.StringVar(&Config.OutputExtension, "outputExtension", "opus", "-outputExtension=opus,mp3,mp4 etc")
1618
flag.Parse()

MyMusicBoxApi/http/v1.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ func V1Endpoints(apiv1Group *gin.RouterGroup) {
2727

2828
apiv1Group.GET("/playlist", playlistHandler.FetchPlaylists)
2929
apiv1Group.GET("/playlist/:playlistId", playlistSongHandler.FetchPlaylistSongs)
30-
apiv1Group.GET("/play/:sourceId", Play)
30+
31+
// If nginx is not configured to handle http 206
32+
if configuration.Config.UsePlayUrl {
33+
apiv1Group.GET("/play/:sourceId", Play)
34+
}
35+
3136
apiv1Group.GET("/tasklogs", taskLogHandler.FetchParentTaskLogs)
3237
apiv1Group.GET("/tasklogs/:parentId", taskLogHandler.FetchChildTaskLogs)
3338

@@ -38,6 +43,9 @@ func V1Endpoints(apiv1Group *gin.RouterGroup) {
3843
apiv1Group.DELETE("/playlist/:playlistId", playlistHandler.DeletePlaylist)
3944
apiv1Group.DELETE("playlistsong/:playlistId/:songId", playlistSongHandler.DeletePlaylistSong)
4045

41-
// Serving static files
42-
apiv1Group.Static("/images", filepath.Join(configuration.Config.SourceFolder, "images"))
46+
// If nginx is not configured to handle it
47+
if configuration.Config.UseImageUrl {
48+
// Serving static files
49+
apiv1Group.Static("/images", filepath.Join(configuration.Config.SourceFolder, "images"))
50+
}
4351
}

MyMusicBoxApi/models/configuration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package models
22

33
type Config struct {
44
UseDevUrl bool
5+
UsePlayUrl bool
6+
UseImageUrl bool
57
DevPort string
68
SourceFolder string
79
OutputExtension string

MyMusicBoxApi/nginx_snippets

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Serve only .opus files under /play
2+
location ~ ^/play/(.*.opus)$ {
3+
alias [path_to_file_location]/$1; # $1 is filename from url, must end with .opus
4+
5+
# Tell browsers and CDNs to cache for 30 days
6+
expires 30d;
7+
add_header Cache-Control "public, max-age=2592000, immutable";
8+
9+
add_header Accept-Ranges bytes;
10+
}
11+
12+
# Serve only .opus files under /play
13+
location ~ ^/image/(.*.jpg)$ {
14+
alias [path_to_file_location]/$1; # $1 is filename from url, must end with .jgp
15+
16+
# Tell browsers and CDNs to cache for 30 days
17+
expires 30d;
18+
add_header Cache-Control "public, max-age=2592000, immutable";
19+
}

0 commit comments

Comments
 (0)