mirror of
https://git.maid.zone/stuff/soundcloak.git
synced 2025-12-10 13:49:39 +05:00
option for embedding files in binary
This commit is contained in:
@@ -9,7 +9,7 @@ README.md
|
|||||||
docs
|
docs
|
||||||
|
|
||||||
*.fiber.gz
|
*.fiber.gz
|
||||||
node_modules
|
|
||||||
*_templ.go
|
*_templ.go
|
||||||
regexp2_codegen.go
|
regexp2_codegen.go
|
||||||
main
|
main
|
||||||
|
static/external
|
||||||
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,14 +1,15 @@
|
|||||||
# soundcloak config (and instance-specific files)
|
# soundcloak config (and instance-specific files)
|
||||||
soundcloak.json
|
soundcloak.json
|
||||||
instance/*
|
static/instance/*
|
||||||
|
|
||||||
|
# external js modules
|
||||||
|
static/external/*
|
||||||
|
|
||||||
# other configs
|
# other configs
|
||||||
compose.yaml
|
compose.yaml
|
||||||
fly.toml
|
fly.toml
|
||||||
|
|
||||||
# created by soundcloak/dependencies
|
# created by soundcloak/dependencies
|
||||||
node_modules
|
|
||||||
package-lock.json
|
|
||||||
*.fiber.gz
|
*.fiber.gz
|
||||||
|
|
||||||
# codegen
|
# codegen
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ git pull
|
|||||||
```
|
```
|
||||||
|
|
||||||
2. Update dependencies/tools:
|
2. Update dependencies/tools:
|
||||||
|
|
||||||
|
You can skip some parts if the tools/dependencies have not been updated.
|
||||||
```sh
|
```sh
|
||||||
go get # for go packages
|
go get # for go packages
|
||||||
|
|
||||||
@@ -87,6 +89,7 @@ soundcloakctl js download # re-download JS modules
|
|||||||
```
|
```
|
||||||
|
|
||||||
3. Clean precompressed static files
|
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:
|
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
|
```sh
|
||||||
find . -name \*.fiber.gz -type f -delete
|
find . -name \*.fiber.gz -type f -delete
|
||||||
|
|||||||
@@ -25,7 +25,9 @@ cp compose.example.yaml compose.yaml
|
|||||||
Make adjustments as needed.
|
Make adjustments as needed.
|
||||||
|
|
||||||
4. *Optional.* Edit config:
|
4. *Optional.* Edit config:
|
||||||
Refer to [Configuration guide](#configuration-guide) for configuration information. Can be configured from environment variables or JSON file.
|
|
||||||
|
Refer to [Configuration guide](#configuration-guide) for configuration information. Can be configured from environment variables or JSON file.
|
||||||
|
|
||||||
5. Run the container:
|
5. Run the container:
|
||||||
|
|
||||||
```sh
|
```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
|
## Regular method
|
||||||
|
|
||||||
|
** Not recommended for deployment. **
|
||||||
Refer to the [developer guide](DEV_GUIDE.md#setup)
|
Refer to the [developer guide](DEV_GUIDE.md#setup)
|
||||||
|
|
||||||
# Updating your instance
|
# 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. |
|
| 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. |
|
| 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 |
|
| 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>
|
</details>
|
||||||
|
|
||||||
@@ -128,17 +132,17 @@ Some notes:
|
|||||||
|
|
||||||
I will mainly talk about the static files here. Maybe about the templates too in the future
|
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
|
### 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
|
2. Create a file with the same name as the one you want to override
|
||||||
3. Put whatever you want there
|
3. Put whatever you want there
|
||||||
|
|
||||||
### Basic theming
|
### 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:
|
2. Put your CSS rules there:
|
||||||
|
|
||||||
```css
|
```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>
|
</details>
|
||||||
|
|
||||||
|
|||||||
@@ -89,6 +89,8 @@ var TrustedProxies = []string{}
|
|||||||
|
|
||||||
var CodegenConfig = false
|
var CodegenConfig = false
|
||||||
|
|
||||||
|
var EmbedFiles = false
|
||||||
|
|
||||||
// // end of config // //
|
// // end of config // //
|
||||||
|
|
||||||
// defaults are:
|
// defaults are:
|
||||||
@@ -389,6 +391,16 @@ func fromEnv() error {
|
|||||||
TrustedProxies = p
|
TrustedProxies = p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("CODEGEN_CONFIG")
|
||||||
|
if env != "" {
|
||||||
|
CodegenConfig = boolean(env)
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("EMBED_FILES")
|
||||||
|
if env != "" {
|
||||||
|
EmbedFiles = boolean(env)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -440,6 +452,7 @@ func init() {
|
|||||||
TrustedProxyCheck *bool
|
TrustedProxyCheck *bool
|
||||||
TrustedProxies *[]string
|
TrustedProxies *[]string
|
||||||
CodegenConfig *bool
|
CodegenConfig *bool
|
||||||
|
EmbedFiles *bool
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(data, &config)
|
err = json.Unmarshal(data, &config)
|
||||||
@@ -514,6 +527,9 @@ func init() {
|
|||||||
if config.CodegenConfig != nil {
|
if config.CodegenConfig != nil {
|
||||||
CodegenConfig = *config.CodegenConfig
|
CodegenConfig = *config.CodegenConfig
|
||||||
}
|
}
|
||||||
|
if config.EmbedFiles != nil {
|
||||||
|
EmbedFiles = *config.EmbedFiles
|
||||||
|
}
|
||||||
|
|
||||||
if config.DefaultPreferences != nil {
|
if config.DefaultPreferences != nil {
|
||||||
loadDefaultPreferences(*config.DefaultPreferences)
|
loadDefaultPreferences(*config.DefaultPreferences)
|
||||||
|
|||||||
22
main.go
22
main.go
@@ -3,11 +3,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/compress"
|
"github.com/gofiber/fiber/v2/middleware/compress"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
||||||
|
|
||||||
"github.com/goccy/go-json"
|
"github.com/goccy/go-json"
|
||||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
proxystreams "git.maid.zone/stuff/soundcloak/lib/proxy_streams"
|
proxystreams "git.maid.zone/stuff/soundcloak/lib/proxy_streams"
|
||||||
"git.maid.zone/stuff/soundcloak/lib/restream"
|
"git.maid.zone/stuff/soundcloak/lib/restream"
|
||||||
"git.maid.zone/stuff/soundcloak/lib/sc"
|
"git.maid.zone/stuff/soundcloak/lib/sc"
|
||||||
|
"git.maid.zone/stuff/soundcloak/static"
|
||||||
"git.maid.zone/stuff/soundcloak/templates"
|
"git.maid.zone/stuff/soundcloak/templates"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,15 +39,11 @@ func main() {
|
|||||||
TrustedProxies: cfg.TrustedProxies,
|
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(recover.New())
|
||||||
}
|
}
|
||||||
app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed}))
|
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.
|
// 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 {
|
if cfg.Debug {
|
||||||
app.Get("/_/cachedump/tracks", func(c *fiber.Ctx) error {
|
app.Get("/_/cachedump/tracks", func(c *fiber.Ctx) error {
|
||||||
@@ -79,6 +78,19 @@ func main() {
|
|||||||
app.Get("/index.html", mainPageHandler)
|
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 {
|
app.Get("/search", func(c *fiber.Ctx) error {
|
||||||
prefs, err := preferences.Get(c)
|
prefs, err := preferences.Get(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"dependencies": {
|
|
||||||
"hls.js": "^1.5.17"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Before Width: | Height: | Size: 5.2 KiB After Width: | Height: | Size: 5.2 KiB |
14
static/init.go
Normal file
14
static/init.go
Normal 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
0
static/instance/.gitkeep
Normal file
Reference in New Issue
Block a user