option for embedding files in binary

This commit is contained in:
Laptop
2024-12-29 11:45:34 +02:00
parent a8d6d67fda
commit 5afc1e830a
23 changed files with 66 additions and 21 deletions

View File

@@ -9,7 +9,7 @@ README.md
docs
*.fiber.gz
node_modules
*_templ.go
regexp2_codegen.go
main
static/external

7
.gitignore vendored
View File

@@ -1,14 +1,15 @@
# soundcloak config (and instance-specific files)
soundcloak.json
instance/*
static/instance/*
# external js modules
static/external/*
# other configs
compose.yaml
fly.toml
# created by soundcloak/dependencies
node_modules
package-lock.json
*.fiber.gz
# codegen

View File

@@ -74,6 +74,8 @@ git pull
```
2. Update dependencies/tools:
You can skip some parts if the tools/dependencies have not been updated.
```sh
go get # for go packages
@@ -87,6 +89,7 @@ soundcloakctl js download # re-download JS modules
```
3. Clean precompressed static files
Those are created by the webserver in order to more efficiently serve static files. They have the `.fiber.gz` extension. You can easily remove them from all directories like this:
```sh
find . -name \*.fiber.gz -type f -delete

View File

@@ -25,7 +25,9 @@ cp compose.example.yaml compose.yaml
Make adjustments as needed.
4. *Optional.* Edit config:
Refer to [Configuration guide](#configuration-guide) for configuration information. Can be configured from environment variables or JSON file.
5. Run the container:
```sh
@@ -40,6 +42,7 @@ Soundcloak will now be up at `127.0.0.1:4664` (or the address you specified in y
## Regular method
** Not recommended for deployment. **
Refer to the [developer guide](DEV_GUIDE.md#setup)
# Updating your instance
@@ -117,7 +120,8 @@ Some notes:
| Prefork | PREFORK | false | Run multiple instances of soundcloak locally to be able to handle more requests. Each one will be a separate process, so they will have separate cache. |
| TrustedProxyCheck | TRUSTED_PROXY_CHECK | true | Use X-Forwarded-* headers if IP is in TrustedProxies list. When disabled, those headers will blindly be used. |
| TrustedProxies | TRUSTED_PROXIES | [] | List of IPs or IP ranges of trusted proxies |
| CodegenConfig | CODEGEN_CONFIG | false | Mainly needed for the dockerfile. If you enable this, and build the container (or run `soundcloakctl config codegen`) - your current config will be parsed and embedded in the code, which allows the compiler to do some more optimizations. Keep in mind that you won't be able to change the config dynamically, you will have to rebuild the container / binary |
| CodegenConfig | CODEGEN_CONFIG | false | Highly recommended to enable. Embeds the config into the binary, which helps reduce size if you aren't using certain features and generally optimize the binary better. Keep in mind that you will have to rebuild the image/binary each time you want to change config. (Note: you need to run `soundcloakctl config codegen` or use docker, as it runs it for you) |
| EmbedFiles | EMBED_FILES | false | Embed files into the binary. Keep in mind that you will have to rebuild the image/binary each time you update static files (e.g. custom instance files) |
</details>
@@ -128,17 +132,17 @@ Some notes:
I will mainly talk about the static files here. Maybe about the templates too in the future
The static files are stored in `assets` folder
The static files are stored in `static/assets` folder
### Overriding files
1. Create a folder named `instance`
1. Create a folder named `instance` in `static` folder
2. Create a file with the same name as the one you want to override
3. Put whatever you want there
### Basic theming
1. Create `instance.css` file in the `instance` folder
1. Create `instance.css` file in the `static/instance` folder
2. Put your CSS rules there:
```css
@@ -152,7 +156,7 @@ The static files are stored in `assets` folder
}
```
Refer to `assets/global.css` file for existing rules.
Refer to `static/assets/global.css` file for existing rules.
</details>

View File

@@ -89,6 +89,8 @@ var TrustedProxies = []string{}
var CodegenConfig = false
var EmbedFiles = false
// // end of config // //
// defaults are:
@@ -389,6 +391,16 @@ func fromEnv() error {
TrustedProxies = p
}
env = os.Getenv("CODEGEN_CONFIG")
if env != "" {
CodegenConfig = boolean(env)
}
env = os.Getenv("EMBED_FILES")
if env != "" {
EmbedFiles = boolean(env)
}
return nil
}
@@ -440,6 +452,7 @@ func init() {
TrustedProxyCheck *bool
TrustedProxies *[]string
CodegenConfig *bool
EmbedFiles *bool
}
err = json.Unmarshal(data, &config)
@@ -514,6 +527,9 @@ func init() {
if config.CodegenConfig != nil {
CodegenConfig = *config.CodegenConfig
}
if config.EmbedFiles != nil {
EmbedFiles = *config.EmbedFiles
}
if config.DefaultPreferences != nil {
loadDefaultPreferences(*config.DefaultPreferences)

22
main.go
View File

@@ -3,11 +3,13 @@ package main
import (
"log"
"math/rand"
"net/http"
"net/url"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2/middleware/recover"
@@ -19,6 +21,7 @@ import (
proxystreams "git.maid.zone/stuff/soundcloak/lib/proxy_streams"
"git.maid.zone/stuff/soundcloak/lib/restream"
"git.maid.zone/stuff/soundcloak/lib/sc"
"git.maid.zone/stuff/soundcloak/static"
"git.maid.zone/stuff/soundcloak/templates"
)
@@ -36,15 +39,11 @@ func main() {
TrustedProxies: cfg.TrustedProxies,
})
if !cfg.Debug {
if !cfg.Debug { // you wanna catch any possible panics as soon as possible
app.Use(recover.New())
}
app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed}))
app.Static("/", "instance", fiber.Static{Compress: true, MaxAge: 7200}) // 2 hours
app.Static("/", "assets", fiber.Static{Compress: true, MaxAge: 14400}) // 4 hours
app.Static("/js/", "external", fiber.Static{Compress: true, MaxAge: 28800}) // 8 hours
// Just for easy inspection of cache in development. Since debug is constant, the compiler will just remove the code below if it's set to false, so this has no runtime overhead.
if cfg.Debug {
app.Get("/_/cachedump/tracks", func(c *fiber.Ctx) error {
@@ -79,6 +78,19 @@ func main() {
app.Get("/index.html", mainPageHandler)
}
const InstanceMaxAge = 7200 // 2hrs
const AssetsMaxAge = 14400 // 4hrs
const ExternalMaxAge = 28800 // 8hrs
if cfg.EmbedFiles {
app.Use("/", filesystem.New(filesystem.Config{Root: http.FS(static.Instance), PathPrefix: "instance", MaxAge: InstanceMaxAge}))
app.Use("/", filesystem.New(filesystem.Config{Root: http.FS(static.Assets), PathPrefix: "assets", MaxAge: AssetsMaxAge}))
app.Use("/js/", filesystem.New(filesystem.Config{Root: http.FS(static.External), PathPrefix: "external", MaxAge: ExternalMaxAge}))
} else {
app.Static("/", "static/instance", fiber.Static{Compress: true, MaxAge: InstanceMaxAge})
app.Static("/", "static/assets", fiber.Static{Compress: true, MaxAge: AssetsMaxAge})
app.Static("/js/", "static/external", fiber.Static{Compress: true, MaxAge: ExternalMaxAge})
}
app.Get("/search", func(c *fiber.Ctx) error {
prefs, err := preferences.Get(c)
if err != nil {

View File

@@ -1,5 +0,0 @@
{
"dependencies": {
"hls.js": "^1.5.17"
}
}

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

14
static/init.go Normal file
View File

@@ -0,0 +1,14 @@
package static
// I had to move the folders to here due to go limitation. You can't embed from relative (e.g. parent) paths
import "embed"
//go:embed assets/*
var Assets embed.FS
//go:embed instance/*
var Instance embed.FS
//go:embed external/*
var External embed.FS

0
static/instance/.gitkeep Normal file
View File