move common static files, 503 on unavailable services

This commit is contained in:
ari melody 2026-07-24 12:41:36 +01:00
parent 7b92f97b24
commit b2485eda79
Signed by: ari
GPG key ID: CF99829C92678188
47 changed files with 36 additions and 9 deletions

View file

@ -5,13 +5,16 @@ import (
"fmt" "fmt"
"log" "log"
"math" "math"
"net/http"
"os" "os"
"os/signal" "os/signal"
"strconv" "strconv"
"strings"
"syscall" "syscall"
"codeberg.org/arimelody/ari-stream-tools/learning" "codeberg.org/arimelody/ari-stream-tools/learning"
"codeberg.org/arimelody/ari-stream-tools/music" "codeberg.org/arimelody/ari-stream-tools/music"
"codeberg.org/arimelody/ari-stream-tools/static"
"codeberg.org/arimelody/ari-stream-tools/twitch" "codeberg.org/arimelody/ari-stream-tools/twitch"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -41,6 +44,7 @@ func main() {
} }
srv := gin.New() srv := gin.New()
// srv.Use(gin.Logger())
srv.SetTrustedProxies([]string{"127.0.0.1", "::1"}) srv.SetTrustedProxies([]string{"127.0.0.1", "::1"})
learningService, err := learning.New(ctx, learning.ServiceConfig{ learningService, err := learning.New(ctx, learning.ServiceConfig{
@ -48,26 +52,41 @@ func main() {
}) })
if err != nil { if err != nil {
log.Printf("Failed to create learning service: %v", err) log.Printf("Failed to create learning service: %v", err)
srv.Group("learning").Any("/*any", func(ctx *gin.Context) {
ctx.String(http.StatusServiceUnavailable, http.StatusText(http.StatusServiceUnavailable))
})
} else { } else {
learningService.BindRoutes(srv.Group("/learning")) learningService.BindRoutes(srv.Group("learning"))
} }
musicService, err := music.New(ctx) musicService, err := music.New(ctx)
if err != nil { if err != nil {
log.Printf("Failed to create music service: %v", err) log.Printf("Failed to create music service: %v", err)
srv.Group("music").Any("/*any", func(ctx *gin.Context) {
ctx.String(http.StatusServiceUnavailable, http.StatusText(http.StatusServiceUnavailable))
})
} else { } else {
musicService.BindRoutes(srv.Group("/music")) musicService.BindRoutes(srv.Group("music"))
go musicService.Run(ctx) go musicService.Run(ctx)
} }
twitchService, err := twitch.New(ctx, twitch.ServiceOptions{ Port: port }) twitchService, err := twitch.New(ctx, twitch.ServiceOptions{ Port: port })
if err != nil { if err != nil {
log.Printf("Failed to create Twitch service: %v", err) log.Printf("Failed to create Twitch service: %v", err)
srv.Group("twitch").Any("/*any", func(ctx *gin.Context) {
ctx.String(http.StatusServiceUnavailable, http.StatusText(http.StatusServiceUnavailable))
})
} else { } else {
twitchService.BindRoutes(srv.Group("/twitch")) twitchService.BindRoutes(srv.Group("twitch"))
go twitchService.Run(ctx) go twitchService.Run(ctx)
} }
public := srv.Group("/public")
public.GET("/*path", func(ctx *gin.Context) {
filepath := strings.TrimPrefix(ctx.Request.URL.Path, "/public/")
http.ServeFileFS(ctx.Writer, ctx.Request, static.PublicFS, filepath)
})
go func() { go func() {
log.Printf("Now serving at http://%s:%d\n", host, port) log.Printf("Now serving at http://%s:%d\n", host, port)
failed <- srv.Run(fmt.Sprintf("%s:%d", host, port)) failed <- srv.Run(fmt.Sprintf("%s:%d", host, port))

View file

@ -73,8 +73,8 @@ func New(ctx context.Context) (*Service, error) {
func (srv *Service) BindRoutes(group *gin.RouterGroup) { func (srv *Service) BindRoutes(group *gin.RouterGroup) {
group.GET("/public/*path", func(ctx *gin.Context) { group.GET("/public/*path", func(ctx *gin.Context) {
path := strings.TrimPrefix(ctx.Request.URL.Path, "/music/") filepath := strings.TrimPrefix(ctx.Request.URL.Path, "/music/")
http.ServeFileFS(ctx.Writer, ctx.Request, publicFS, path) http.ServeFileFS(ctx.Writer, ctx.Request, publicFS, filepath)
}) })
group.GET("/sse", func(ctx *gin.Context) { group.GET("/sse", func(ctx *gin.Context) {

View file

@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Nothing is playing...</title> <title>Nothing is playing...</title>
<link rel="stylesheet" href="/music/public/fonts/inter/inter.css"> <link rel="stylesheet" href="/public/fonts/inter/inter.css">
<link rel="stylesheet" href="/music/public/music.css"> <link rel="stylesheet" href="/music/public/music.css">
</head> </head>
<body> <body>

6
static/static.go Normal file
View file

@ -0,0 +1,6 @@
package static
import "embed"
//go:embed public
var PublicFS embed.FS

View file

@ -412,6 +412,7 @@ type (
// Unfortunately, this function is very unreliable for pulling chronological // Unfortunately, this function is very unreliable for pulling chronological
// subscription records. Twitch API does not currently provide a mechanism for // subscription records. Twitch API does not currently provide a mechanism for
// fetching the latest subscriber; this will need to be tracked manually. // fetching the latest subscriber; this will need to be tracked manually.
/*
func (srv *Service) getSubscribers( func (srv *Service) getSubscribers(
ctx context.Context, ctx context.Context,
broadcasterID string, broadcasterID string,
@ -442,3 +443,4 @@ func (srv *Service) getSubscribers(
return resData, nil return resData, nil
} }
*/

View file

@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Twitch Labels</title> <title>Twitch Labels</title>
<link rel="stylesheet" href="/music/public/fonts/inter/inter.css"> <link rel="stylesheet" href="/public/fonts/inter/inter.css">
<link rel="stylesheet" href="/twitch/public/labels.css"> <link rel="stylesheet" href="/twitch/public/labels.css">
</head> </head>
<body> <body>

View file

@ -178,8 +178,8 @@ func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
func (srv *Service) BindRoutes(group *gin.RouterGroup) { func (srv *Service) BindRoutes(group *gin.RouterGroup) {
group.GET("/public/*path", func(ctx *gin.Context) { group.GET("/public/*path", func(ctx *gin.Context) {
path := strings.TrimPrefix(ctx.Request.URL.Path, "/twitch/") filepath := strings.TrimPrefix(ctx.Request.URL.Path, "/twitch/")
http.ServeFileFS(ctx.Writer, ctx.Request, publicFS, path) http.ServeFileFS(ctx.Writer, ctx.Request, publicFS, filepath)
}) })
group.GET("/login", func(ctx *gin.Context) { group.GET("/login", func(ctx *gin.Context) {