Skip to content

Commit

Permalink
Added serving media files
Browse files Browse the repository at this point in the history
  • Loading branch information
CallumKerson committed Mar 10, 2023
1 parent 51ee489 commit 937c7d3
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 16 deletions.
10 changes: 7 additions & 3 deletions cmd/athenaeum/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"path"
"path/filepath"
"strings"

Expand All @@ -11,6 +10,7 @@ import (
"github.com/CallumKerson/loggerrific"

"github.com/CallumKerson/Athenaeum/internal/podcasts"
transportHttp "github.com/CallumKerson/Athenaeum/internal/transport/http"
)

type Config struct {
Expand All @@ -30,7 +30,11 @@ type Podcast struct {
}

func (c *Config) GetMediaHost() string {
return path.Join(c.Host, c.Media.HostPath)
return fmt.Sprintf("%s/%s", c.Host, c.Media.HostPath)
}

func (c *Config) GetMediaHandlerOpt() transportHttp.HandlerOption {
return transportHttp.WithMediaConfig(c.Media.Root, c.Media.HostPath)
}

func NewConfig(port int, logger loggerrific.Logger) (*Config, error) {
Expand All @@ -40,7 +44,7 @@ func NewConfig(port int, logger loggerrific.Logger) (*Config, error) {
viper.SetDefault("Podcast.Opts.Explicit", true)
viper.SetDefault("Podcast.Opts.Language", "EN")
viper.SetDefault("Podcast.Root", "/srv/podcasts")
viper.SetDefault("Media.HostPath", "media")
viper.SetDefault("Media.HostPath", "/media")
viper.SetDefault("Media.Root", "/srv/media")
viper.SetDefault("Host", fmt.Sprintf("http://localhost:%d", port))

Expand Down
2 changes: 1 addition & 1 deletion cmd/athenaeum/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestConfig_DefaultsOnly(t *testing.T) {
assert.Equal(t, "None", config.Podcast.Opts.Copyright)
assert.Equal(t, true, config.Podcast.Opts.Explicit)
assert.Equal(t, "EN", config.Podcast.Opts.Language)
assert.Equal(t, "media", config.Media.HostPath)
assert.Equal(t, "/media", config.Media.HostPath)
assert.Equal(t, "/srv/media", config.Media.Root)
assert.Equal(t, "http://localhost:8080", config.Host)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/athenaeum/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Run(port int, logger loggerrific.Logger) error {
}
mediaSvc := mediaService.New(cfg.Media.Root, logger)
podcastService := podcasts.NewService(cfg.GetMediaHost(), &cfg.Podcast.Opts, mediaSvc, logger)
httpHandler := transportHttp.NewHandler(podcastService, logger)
httpHandler := transportHttp.NewHandler(podcastService, logger, cfg.GetMediaHandlerOpt())

return Serve(httpHandler, port, logger)
}
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ services:
athenaeum:
build: .
image: "ghcr.io/callumkerson/athenaeum:development"
volumes:
- ./internal/media/service/testdata:/srv/media
ports:
- "8084:8080"
- "8080:8080"
24 changes: 15 additions & 9 deletions internal/transport/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,39 @@ type PodcastService interface {

type Handler struct {
*mux.Router
Service PodcastService
Log loggerrific.Logger
Service PodcastService
Log loggerrific.Logger
mediaRoot string
mediaServePath string
}

func NewHandler(service PodcastService, logger loggerrific.Logger) *Handler {
func NewHandler(service PodcastService, logger loggerrific.Logger, opts ...HandlerOption) *Handler {
handler := &Handler{
Service: service,
Log: logger,
}
for _, opt := range opts {
opt(handler)
}
handler.Router = mux.NewRouter()
handler.mapRoutes()

handler.Use(TimeoutMiddleware)
m := NewMiddlewares(logger)
handler.Use(m.LoggingMiddleware)

return handler
}

func (h *Handler) mapRoutes() {
h.HandleFunc("/health", healthCheck)
h.HandleFunc("/ready", h.readiness)

middleware := NewMiddlewares(h.Log)

podcastSubrouter := h.PathPrefix("/podcast").Subrouter()
m := NewMiddlewares(h.Log)
podcastSubrouter.Use(m.LoggingMiddleware)
podcastSubrouter.Use(middleware.LoggingMiddleware)
podcastSubrouter.HandleFunc("/feed.rss", h.getFeed)

fs := http.StripPrefix(h.mediaServePath, http.FileServer(http.Dir(h.mediaRoot)))
h.Log.Infoln("Serving files from local path", h.mediaRoot, "at", h.mediaServePath)
h.Router.PathPrefix(h.mediaServePath).Handler(middleware.LoggingMiddleware(fs))
}

func healthCheck(writer http.ResponseWriter, request *http.Request) {
Expand Down
18 changes: 18 additions & 0 deletions internal/transport/http/handler_opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package http

import (
"fmt"
"strings"
)

type HandlerOption func(h *Handler)

func WithMediaConfig(mediaRoot, mediaServePath string) HandlerOption {
return func(h *Handler) {
h.mediaRoot = mediaRoot
if !strings.HasSuffix(mediaServePath, "/") {
mediaServePath = fmt.Sprintf("%s/", mediaServePath)
}
h.mediaServePath = mediaServePath
}
}
3 changes: 2 additions & 1 deletion internal/transport/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
)

func TestHandler(t *testing.T) {
testHandler := NewHandler(&DummyService{}, tlogger.NewTLogger(t))
testHandler := NewHandler(&DummyService{}, tlogger.NewTLogger(t), WithMediaConfig("testdata", "/media/"))

testServer := httptest.NewServer(testHandler)
defer testServer.Close()
Expand All @@ -40,6 +40,7 @@ func TestHandler(t *testing.T) {
{name: "health check", r: newReq("GET", testServer.URL+"/health", nil), expectedStatus: 200, expectedContentType: ContentTypeJSON, expectedBody: "{\n \"health\": \"ok\"\n}\n"},
{name: "readiness check", r: newReq("GET", testServer.URL+"/ready", nil), expectedStatus: 200, expectedContentType: ContentTypeJSON, expectedBody: "{\n \"readiness\": \"ok\"\n}\n"},
{name: "feed", r: newReq("GET", testServer.URL+"/podcast/feed.rss", nil), expectedStatus: 200, expectedContentType: ContentTypeXML, expectedBody: testFeed},
{name: "media", r: newReq("GET", testServer.URL+"/media/media.txt", nil), expectedStatus: 200, expectedContentType: "text/plain; charset=utf-8", expectedBody: "served file\n"},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/transport/http/testdata/media.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
served file

0 comments on commit 937c7d3

Please sign in to comment.