move common static files, 503 on unavailable services
This commit is contained in:
parent
7b92f97b24
commit
b2485eda79
47 changed files with 36 additions and 9 deletions
|
|
@ -5,13 +5,16 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"codeberg.org/arimelody/ari-stream-tools/learning"
|
||||
"codeberg.org/arimelody/ari-stream-tools/music"
|
||||
"codeberg.org/arimelody/ari-stream-tools/static"
|
||||
"codeberg.org/arimelody/ari-stream-tools/twitch"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
|
@ -41,6 +44,7 @@ func main() {
|
|||
}
|
||||
|
||||
srv := gin.New()
|
||||
// srv.Use(gin.Logger())
|
||||
srv.SetTrustedProxies([]string{"127.0.0.1", "::1"})
|
||||
|
||||
learningService, err := learning.New(ctx, learning.ServiceConfig{
|
||||
|
|
@ -48,26 +52,41 @@ func main() {
|
|||
})
|
||||
if err != nil {
|
||||
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 {
|
||||
learningService.BindRoutes(srv.Group("/learning"))
|
||||
learningService.BindRoutes(srv.Group("learning"))
|
||||
}
|
||||
|
||||
musicService, err := music.New(ctx)
|
||||
if err != nil {
|
||||
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 {
|
||||
musicService.BindRoutes(srv.Group("/music"))
|
||||
musicService.BindRoutes(srv.Group("music"))
|
||||
go musicService.Run(ctx)
|
||||
}
|
||||
|
||||
twitchService, err := twitch.New(ctx, twitch.ServiceOptions{ Port: port })
|
||||
if err != nil {
|
||||
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 {
|
||||
twitchService.BindRoutes(srv.Group("/twitch"))
|
||||
twitchService.BindRoutes(srv.Group("twitch"))
|
||||
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() {
|
||||
log.Printf("Now serving at http://%s:%d\n", host, port)
|
||||
failed <- srv.Run(fmt.Sprintf("%s:%d", host, port))
|
||||
|
|
|
|||
|
|
@ -73,8 +73,8 @@ func New(ctx context.Context) (*Service, error) {
|
|||
|
||||
func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/public/*path", func(ctx *gin.Context) {
|
||||
path := strings.TrimPrefix(ctx.Request.URL.Path, "/music/")
|
||||
http.ServeFileFS(ctx.Writer, ctx.Request, publicFS, path)
|
||||
filepath := strings.TrimPrefix(ctx.Request.URL.Path, "/music/")
|
||||
http.ServeFileFS(ctx.Writer, ctx.Request, publicFS, filepath)
|
||||
})
|
||||
|
||||
group.GET("/sse", func(ctx *gin.Context) {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<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">
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
6
static/static.go
Normal file
6
static/static.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package static
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed public
|
||||
var PublicFS embed.FS
|
||||
|
|
@ -412,6 +412,7 @@ type (
|
|||
// Unfortunately, this function is very unreliable for pulling chronological
|
||||
// subscription records. Twitch API does not currently provide a mechanism for
|
||||
// fetching the latest subscriber; this will need to be tracked manually.
|
||||
/*
|
||||
func (srv *Service) getSubscribers(
|
||||
ctx context.Context,
|
||||
broadcasterID string,
|
||||
|
|
@ -442,3 +443,4 @@ func (srv *Service) getSubscribers(
|
|||
|
||||
return resData, nil
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<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">
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
|
|
@ -178,8 +178,8 @@ func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
|
|||
|
||||
func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/public/*path", func(ctx *gin.Context) {
|
||||
path := strings.TrimPrefix(ctx.Request.URL.Path, "/twitch/")
|
||||
http.ServeFileFS(ctx.Writer, ctx.Request, publicFS, path)
|
||||
filepath := strings.TrimPrefix(ctx.Request.URL.Path, "/twitch/")
|
||||
http.ServeFileFS(ctx.Writer, ctx.Request, publicFS, filepath)
|
||||
})
|
||||
|
||||
group.GET("/login", func(ctx *gin.Context) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue