File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
1313 "github.com/warmans/tvgif/pkg/render"
1414 "github.com/warmans/tvgif/pkg/search"
1515 "github.com/warmans/tvgif/pkg/store"
16+ "github.com/warmans/tvgif/pkg/web"
1617 "log"
1718 "log/slog"
1819 "os"
@@ -123,9 +124,19 @@ func NewBotCommand(logger *slog.Logger) *cobra.Command {
123124 return fmt .Errorf ("failed to create bot: %w" , err )
124125 }
125126
127+ server := web .NewServer (":8080" , path .Join (mediaPath , "overlay" ), overlayCache )
128+
126129 if err = bot .Start (); err != nil {
127130 return fmt .Errorf ("failed to start bot: %w" , err )
128131 }
132+
133+ go func () {
134+ logger .Info ("Starting web server" , slog .String ("addr" , ":8080" ))
135+ if err := server .Start (); err != nil {
136+ logger .Error ("web server failed" , slog .String ("err" , err .Error ()))
137+ }
138+ }()
139+
129140 stop := make (chan os.Signal , 1 )
130141 signal .Notify (stop , os .Interrupt )
131142 <- stop
Original file line number Diff line number Diff line change 1+ package web
2+
3+ import (
4+ "github.com/warmans/tvgif/pkg/mediacache"
5+ "html/template"
6+ "net/http"
7+ )
8+
9+ func NewServer (
10+ addr string ,
11+ overlayDir string ,
12+ overlayCache * mediacache.OverlayCache ,
13+ ) * Server {
14+ return & Server {
15+ addr : addr ,
16+ overlayDir : overlayDir ,
17+ overlayCache : overlayCache ,
18+ template : template .Must (template .New ("overlays" ).Parse (`<!doctype html>
19+ <html lang='en'>
20+ <head>
21+ <meta charset='utf-8'>
22+ <title>Overlays</title>
23+ <style>
24+ body {
25+ background-color: #222;
26+ }
27+ h1 {
28+ color: #fff;
29+ }
30+ table {
31+ color: #fff;
32+ border: none;
33+ }
34+
35+ table tr {
36+ border-bottom: 1px solid #000;
37+ }
38+
39+ table tr td {
40+ padding: 10px;
41+ }
42+ </style>
43+ </head>
44+ <body>
45+ <header>
46+ <h1>Available Overlays</h1>
47+ </header>
48+ <main>
49+ <table>
50+ {{range .}}
51+ <tr><td><img width="200px" src="/overlays/{{.}}" /></td><td><pre>{{.}}</pre></td></tr>
52+ {{end}}
53+ </table>
54+ </main>
55+ </body>
56+ </html>` )),
57+ }
58+ }
59+
60+ type Server struct {
61+ addr string
62+ overlayDir string
63+ overlayCache * mediacache.OverlayCache
64+ template * template.Template
65+ }
66+
67+ func (s * Server ) handleOverlays (resp http.ResponseWriter , req * http.Request ) {
68+ if err := s .template .Execute (resp , s .overlayCache .All ()); err != nil {
69+
70+ }
71+ }
72+
73+ func (s * Server ) Start () error {
74+ mux := http .NewServeMux ()
75+ mux .HandleFunc ("/overlays/index.html" , s .handleOverlays )
76+ mux .Handle ("/overlays/" , http .StripPrefix ("/overlays" , http .FileServer (http .Dir (s .overlayDir ))))
77+
78+ return http .ListenAndServe (s .addr , mux )
79+ }
You can’t perform that action at this time.
0 commit comments