mirror of
https://git.maid.zone/stuff/soundcloak.git
synced 2025-12-10 13:49:39 +05:00
add option to load config from environment
This commit is contained in:
79
README.md
79
README.md
@@ -1,9 +1,11 @@
|
|||||||
# soundcloak
|
# soundcloak
|
||||||
|
|
||||||
wip alternative frontend for soundcloud
|
wip alternative frontend for soundcloud
|
||||||
|
|
||||||
# [Instance list](https://maid.zone/soundcloak/instances.html)
|
# [Instance list](https://maid.zone/soundcloak/instances.html)
|
||||||
|
|
||||||
# Already implemented
|
# Already implemented
|
||||||
|
|
||||||
- Searching for songs, users, playlists
|
- Searching for songs, users, playlists
|
||||||
- Basic user overview (songs, playlists, albums, reposts, metadata)
|
- Basic user overview (songs, playlists, albums, reposts, metadata)
|
||||||
- Basic song overview (author, metadata) & streaming (requires some JS if instance has `Restream` disabled)
|
- Basic song overview (author, metadata) & streaming (requires some JS if instance has `Restream` disabled)
|
||||||
@@ -14,11 +16,13 @@ wip alternative frontend for soundcloud
|
|||||||
- Users can change their preferences (should proxying be enabled, what method of playing the song should be used etc)
|
- Users can change their preferences (should proxying be enabled, what method of playing the song should be used etc)
|
||||||
|
|
||||||
## In the works
|
## In the works
|
||||||
|
|
||||||
- Track player embeds (`https://w.soundcloud.com/player/` -> `https://sc.maid.zone/w/player/`)
|
- Track player embeds (`https://w.soundcloud.com/player/` -> `https://sc.maid.zone/w/player/`)
|
||||||
|
|
||||||
The UI isn't really done yet. All parameters other than url are unsupported. You can also specify track without the `soundcloud.com` part: `https://sc.maid.zone/w/player/?url=<id>` or `https://sc.maid.zone/w/player/?url=<user>/<track>`
|
The UI isn't really done yet. All parameters other than url are unsupported. You can also specify track without the `soundcloud.com` part: `https://sc.maid.zone/w/player/?url=<id>` or `https://sc.maid.zone/w/player/?url=<user>/<track>`
|
||||||
|
|
||||||
# Viewing instance settings
|
# Viewing instance settings
|
||||||
|
|
||||||
If the instance isn't outdated and has `InstanceInfo` setting enabled, you can navigate to `<instance>/_/info` to view useful instance settings. ([sc.maid.zone/_/info](https://sc.maid.zone/_/info) for example)
|
If the instance isn't outdated and has `InstanceInfo` setting enabled, you can navigate to `<instance>/_/info` to view useful instance settings. ([sc.maid.zone/_/info](https://sc.maid.zone/_/info) for example)
|
||||||
|
|
||||||
An easier way is to navigate to `<instance>/_/preferences`.
|
An easier way is to navigate to `<instance>/_/preferences`.
|
||||||
@@ -26,6 +30,7 @@ An easier way is to navigate to `<instance>/_/preferences`.
|
|||||||
If some features are disabled by the instance, they won't show up there.
|
If some features are disabled by the instance, they won't show up there.
|
||||||
|
|
||||||
Available features:
|
Available features:
|
||||||
|
|
||||||
- Parse descriptions: Highlight `@username`, `https://example.com` and `email@example.com` in text as clickable links
|
- Parse descriptions: Highlight `@username`, `https://example.com` and `email@example.com` in text as clickable links
|
||||||
- Proxy images: Retrieve images through the instance, instead of going to soundcloud's servers for them
|
- Proxy images: Retrieve images through the instance, instead of going to soundcloud's servers for them
|
||||||
- Player: In what way should the track be streamed. Can be Restream (does not require JS, better compatibility, can be a bit buggy client-side) or HLS (requires JS, more stable, less good compatibility (you'll be ok unless you are using a very outdated browser))
|
- Player: In what way should the track be streamed. Can be Restream (does not require JS, better compatibility, can be a bit buggy client-side) or HLS (requires JS, more stable, less good compatibility (you'll be ok unless you are using a very outdated browser))
|
||||||
@@ -34,46 +39,56 @@ Available features:
|
|||||||
- - Fully preload track: Fully loads the track when you load the page instead of buffering a small part of it
|
- - Fully preload track: Fully loads the track when you load the page instead of buffering a small part of it
|
||||||
|
|
||||||
# Contributing
|
# Contributing
|
||||||
|
|
||||||
Contributions are appreciated! This includes feedback, feature requests, issues, pull requests and etc.
|
Contributions are appreciated! This includes feedback, feature requests, issues, pull requests and etc.
|
||||||
Feedback and feature requests are especially needed, since I (laptopcat) don't really know what to prioritize
|
Feedback and feature requests are especially needed, since I (laptopcat) don't really know what to prioritize
|
||||||
|
|
||||||
You can contribute on:
|
You can contribute on:
|
||||||
|
|
||||||
- [GitHub](https://github.com/maid-zone/soundcloak)
|
- [GitHub](https://github.com/maid-zone/soundcloak)
|
||||||
- [Codeberg (mirror)](https://codeberg.org/maid-zone/soundcloak)
|
- [Codeberg (mirror)](https://codeberg.org/maid-zone/soundcloak)
|
||||||
|
|
||||||
You can also [reach out to me privately](https://laptopc.at)
|
You can also [reach out to me privately](https://laptopc.at)
|
||||||
|
|
||||||
# Setting it up
|
# Setting it up
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>1. Regular method</summary>
|
<summary>1. Regular method</summary>
|
||||||
|
|
||||||
## Prerequisites:
|
## Prerequisites:
|
||||||
|
|
||||||
1. [node.js + npm](https://nodejs.org) (any recent enough version should do, it's just used for getting hls.js builds)
|
1. [node.js + npm](https://nodejs.org) (any recent enough version should do, it's just used for getting hls.js builds)
|
||||||
2. [golang](https://go.dev) (1.21 or higher was tested, others might work too)
|
2. [golang](https://go.dev) (1.21 or higher was tested, others might work too)
|
||||||
3. [git](https://git-scm.com)
|
3. [git](https://git-scm.com)
|
||||||
|
|
||||||
## Setup:
|
## Setup:
|
||||||
|
|
||||||
1. Clone this repository:
|
1. Clone this repository:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone https://github.com/maid-zone/soundcloak
|
git clone https://github.com/maid-zone/soundcloak
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Go into the cloned repository:
|
2. Go into the cloned repository:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd soundcloak
|
cd soundcloak
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Download hls.js:
|
3. Download hls.js:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
npm i
|
npm i
|
||||||
```
|
```
|
||||||
|
|
||||||
4. Download templ:
|
4. Download templ:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go install github.com/a-h/templ/cmd/templ@latest
|
go install github.com/a-h/templ/cmd/templ@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
*You might need to add go binaries to your PATH (add this line to your .bashrc / .zshrc / whatever)*
|
*You might need to add go binaries to your PATH (add this line to your .bashrc / .zshrc / whatever)*
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
export PATH=${PATH}:`go env GOPATH`/bin
|
export PATH=${PATH}:`go env GOPATH`/bin
|
||||||
```
|
```
|
||||||
@@ -85,6 +100,7 @@ templ generate
|
|||||||
```
|
```
|
||||||
|
|
||||||
6. Download other required go modules:
|
6. Download other required go modules:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go get
|
go get
|
||||||
```
|
```
|
||||||
@@ -100,11 +116,13 @@ go build main.go
|
|||||||
```
|
```
|
||||||
|
|
||||||
9. Run the binary:
|
9. Run the binary:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
./main
|
./main
|
||||||
```
|
```
|
||||||
|
|
||||||
This will run soundcloak on localhost, port 4664. (by default)
|
This will run soundcloak on localhost, port 4664. (by default)
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -113,24 +131,30 @@ This will run soundcloak on localhost, port 4664. (by default)
|
|||||||
The docker image was made by [vlnst](https://github.com/vlnst)
|
The docker image was made by [vlnst](https://github.com/vlnst)
|
||||||
|
|
||||||
## Prerequisites:
|
## Prerequisites:
|
||||||
|
|
||||||
1. [Docker](https://www.docker.com/)
|
1. [Docker](https://www.docker.com/)
|
||||||
2. [Git](https://git-scm.com)
|
2. [Git](https://git-scm.com)
|
||||||
|
|
||||||
## Setup:
|
## Setup:
|
||||||
|
|
||||||
1. Clone this repository:
|
1. Clone this repository:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone https://github.com/maid-zone/soundcloak
|
git clone https://github.com/maid-zone/soundcloak
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Go into the cloned repository:
|
2. Go into the cloned repository:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd soundcloak
|
cd soundcloak
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Make a copy of the example `compose.yaml` file:
|
3. Make a copy of the example `compose.yaml` file:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cp compose.example.yaml compose.yaml
|
cp compose.example.yaml compose.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
Make adjustments as needed.
|
Make adjustments as needed.
|
||||||
|
|
||||||
4. *Optional.* Edit config:
|
4. *Optional.* Edit config:
|
||||||
@@ -142,14 +166,58 @@ You can make a `soundcloak.json` file in the folder if you want. Refer to `sound
|
|||||||
```sh
|
```sh
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
```
|
```
|
||||||
|
|
||||||
(if you get `docker: 'compose' is not a docker command.`, use `docker-compose up -d`)
|
(if you get `docker: 'compose' is not a docker command.`, use `docker-compose up -d`)
|
||||||
|
|
||||||
This will run soundcloak as a daemon (remove the -d part of the command to just run it) on localhost, port 4664. (by default)
|
This will run soundcloak as a daemon (remove the -d part of the command to just run it) on localhost, port 4664. (by default)
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
# Configuration guide
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Click to view</summary>
|
||||||
|
|
||||||
|
You can only configure in one of the two ways:
|
||||||
|
- Using config file (`soundcloak.json` in current directory // your own path and filename)
|
||||||
|
- Using environment variables (`SOUNDCLOAK_CONFIG` must be set to `FROM_ENV`!)
|
||||||
|
|
||||||
|
Some notes:
|
||||||
|
- When specifying time, specify it in seconds.
|
||||||
|
|
||||||
|
| JSON key | Environment variable | Default value | Description |
|
||||||
|
| :------------------------ | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| None | SOUNDCLOAK_CONFIG | soundcloak.json | File to load soundcloak config from. If set to `FROM_ENV`, soundcloak loads the config from environment variables. |
|
||||||
|
| DefaultPreferences | DEFAULT_PREFERENCES | {"Player": "hls", "ProxyStreams": false, "FullyPreloadTrack": false, "ProxyImages": false, "ParseDescriptions": true} | see /_/preferences page, default values adapt to your config (Player: "restream" if Restream, else "hls", ProxyStreams and ProxyImages will be same as respective config values) |
|
||||||
|
| ProxyImages | PROXY_IMAGES | false | Enables proxying of images (user avatars, track covers etc) |
|
||||||
|
| ImageCacheControl | IMAGE_CACHE_CONTROL | max-age=600, public, immutable | [Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Contro) header value for proxied images. Cached for 10 minutes by default. |
|
||||||
|
| ProxyStreams | PROXY_STREAMS | false | Enables proxying of song parts and hls playlist files |
|
||||||
|
| Restream | RESTREAM | false | Enables Restream Player in settings and the /_/restream/:author/:track endpoint. This player can be used without JavaScript and also can be used for downloading songs. |
|
||||||
|
| ClientIDTTL | CLIENT_ID_TTL | 30 minutes | Time until ClientID cache expires. ClientID is used for authenticating with SoundCloud API |
|
||||||
|
| UserTTL | USER_TTL | 10 minutes | Time until User profile cache expires |
|
||||||
|
| UserCacheCleanDelay | USER_CACHE_CLEAN_DELAY | 2.5 minutes | Time between each cleanup of the cache (to remove expired users) |
|
||||||
|
| TrackTTL | TRACK_TTL | 10 minutes | Time until Track data cache expires |
|
||||||
|
| TrackCacheCleanDelay | TRACK_CACHE_CLEAN_DELAY | 2.5 minutes | Time between each cleanup of the cache (to remove expired tracks) |
|
||||||
|
| PlaylistTTL | PLAYLIST_TTL | 10 minutes | Time until Playlist data cache expires |
|
||||||
|
| PlaylistCacheCleanDelay | PLAYLIST_CACHE_CLEAN_DELAY | 2.5 minutes | Time between each cleanup of the cache (to remove expired playlists) |
|
||||||
|
| UserAgent | USER_AGENT | Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.3 | User-Agent header used for requests to SoundCloud |
|
||||||
|
| DNSCacheTTL | DNS_CACHE_TTL | 10 minutes | Time until DNS cache expires |
|
||||||
|
| Addr | ADDR | :4664 | Address and port for soundcloak to listen on |
|
||||||
|
| 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 |
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
# Maintenance-related stuffs
|
# Maintenance-related stuffs
|
||||||
|
|
||||||
## Updating
|
## Updating
|
||||||
|
|
||||||
Note: this guide works only if you install from source. If you used docker, you could probably do the 1st step (pulling the code), stop the container (`docker container stop soundcloak`) and start it again. (`docker compose up -d`)
|
Note: this guide works only if you install from source. If you used docker, you could probably do the 1st step (pulling the code), stop the container (`docker container stop soundcloak`) and start it again. (`docker compose up -d`)
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Click to view</summary>
|
||||||
|
|
||||||
1. Retrieve the latest code:
|
1. Retrieve the latest code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -162,6 +230,7 @@ git pull
|
|||||||
The webserver is configured to locally cache compressed versions of files. They have `.fiber.gz` extension and can be found in `assets` folder and `node_modules/hls.js/dist`. If any static files have been changed, you should purge these compressed files so the new versions can be served. Static files are also cached in user's browser, so you will need to clean your cache to get the new files (Ctrl + F5)
|
The webserver is configured to locally cache compressed versions of files. They have `.fiber.gz` extension and can be found in `assets` folder and `node_modules/hls.js/dist`. If any static files have been changed, you should purge these compressed files so the new versions can be served. Static files are also cached in user's browser, so you will need to clean your cache to get the new files (Ctrl + F5)
|
||||||
|
|
||||||
For example, you can clean these files from `assets` folder like so:
|
For example, you can clean these files from `assets` folder like so:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd assets
|
cd assets
|
||||||
rm *.fiber.gz
|
rm *.fiber.gz
|
||||||
@@ -171,7 +240,6 @@ rm *.fiber.gz
|
|||||||
|
|
||||||
Sometimes, new updates add new config values or change default ones. You can make a `soundcloak.json` file in the folder with the binary if you want, or an environment variable `SOUNDCLOAK_CONFIG` with path to the config. Refer to `soundcloak.json.example` and `lib/cfg/init.go` for configuration values and their meaning.
|
Sometimes, new updates add new config values or change default ones. You can make a `soundcloak.json` file in the folder with the binary if you want, or an environment variable `SOUNDCLOAK_CONFIG` with path to the config. Refer to `soundcloak.json.example` and `lib/cfg/init.go` for configuration values and their meaning.
|
||||||
|
|
||||||
|
|
||||||
4. Regenerate templates (if they changed):
|
4. Regenerate templates (if they changed):
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -179,34 +247,43 @@ templ generate
|
|||||||
```
|
```
|
||||||
|
|
||||||
5. Get latest Go modules:
|
5. Get latest Go modules:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go get
|
go get
|
||||||
```
|
```
|
||||||
|
|
||||||
6. Update hls.js:
|
6. Update hls.js:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
npm i
|
npm i
|
||||||
```
|
```
|
||||||
|
|
||||||
7. Build binary:
|
7. Build binary:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go build main.go
|
go build main.go
|
||||||
```
|
```
|
||||||
|
|
||||||
8. Run it:
|
8. Run it:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
./main
|
./main
|
||||||
```
|
```
|
||||||
|
|
||||||
Congratulations! You have succesfully updated your soundcloak.
|
Congratulations! You have succesfully updated your soundcloak.
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
# Built with
|
# Built with
|
||||||
|
|
||||||
## Backend
|
## Backend
|
||||||
|
|
||||||
- [Go programming language](https://github.com/golang/go)
|
- [Go programming language](https://github.com/golang/go)
|
||||||
- [Fiber (v2)](https://github.com/gofiber/fiber/tree/v2)
|
- [Fiber (v2)](https://github.com/gofiber/fiber/tree/v2)
|
||||||
- [templ](https://github.com/a-h/templ)
|
- [templ](https://github.com/a-h/templ)
|
||||||
- [fasthttp](https://github.com/valyala/fasthttp)
|
- [fasthttp](https://github.com/valyala/fasthttp)
|
||||||
|
|
||||||
## Frontend
|
## Frontend
|
||||||
|
|
||||||
- HTML, CSS and JavaScript
|
- HTML, CSS and JavaScript
|
||||||
- [hls.js](https://github.com/video-dev/hls.js)
|
- [hls.js](https://github.com/video-dev/hls.js)
|
||||||
|
|||||||
274
lib/cfg/init.go
274
lib/cfg/init.go
@@ -3,6 +3,8 @@ package cfg
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/segmentio/encoding/json"
|
"github.com/segmentio/encoding/json"
|
||||||
@@ -93,15 +95,10 @@ var DNSCacheTTL = 10 * time.Minute
|
|||||||
// run soundcloak on this address (localhost:4664 by default)
|
// run soundcloak on this address (localhost:4664 by default)
|
||||||
var Addr = ":4664"
|
var Addr = ":4664"
|
||||||
|
|
||||||
// run multiple instances of soundcloud locally to be able to handle more requests
|
// 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
|
// each one will be a separate process, so they will have separate cache
|
||||||
var Prefork = false
|
var Prefork = false
|
||||||
|
|
||||||
// Enables TLS Early Data (0-RTT / zero round trip time)
|
|
||||||
// This can reduce latency, but also makes requests replayable (not that much of a concern for soundcloak, since there are no authenticated operations)
|
|
||||||
// There might be breakage when used together with TrustedProxyCheck and the proxy is untrusted
|
|
||||||
var EarlyData = false
|
|
||||||
|
|
||||||
// use X-Forwarded-* headers ONLY when ip is in TrustedProxies list
|
// use X-Forwarded-* headers ONLY when ip is in TrustedProxies list
|
||||||
// when disabled, the X-Forwarded-* headers will be blindly used
|
// when disabled, the X-Forwarded-* headers will be blindly used
|
||||||
var TrustedProxyCheck = true
|
var TrustedProxyCheck = true
|
||||||
@@ -111,6 +108,12 @@ var TrustedProxies = []string{}
|
|||||||
|
|
||||||
// // end of config // //
|
// // end of config // //
|
||||||
|
|
||||||
|
// defaults are:
|
||||||
|
// Player: RestreamPlayer if Restream is enabled, otherwise - HLSPlayer
|
||||||
|
// ProxyStreams: same as ProxyStreams in your config (false by default)
|
||||||
|
// FullyPreloadTrack: false
|
||||||
|
// ProxyImages: same as ProxyImages in your config (false by default)
|
||||||
|
// ParseDescriptions: true
|
||||||
func defaultPreferences() {
|
func defaultPreferences() {
|
||||||
var p string
|
var p string
|
||||||
if Restream {
|
if Restream {
|
||||||
@@ -131,9 +134,218 @@ func defaultPreferences() {
|
|||||||
DefaultPreferences.ParseDescriptions = &t
|
DefaultPreferences.ParseDescriptions = &t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadDefaultPreferences(loaded Preferences) {
|
||||||
|
var f bool
|
||||||
|
var t = true
|
||||||
|
if loaded.Player != nil {
|
||||||
|
DefaultPreferences.Player = loaded.Player
|
||||||
|
} else {
|
||||||
|
var p string
|
||||||
|
if Restream {
|
||||||
|
p = RestreamPlayer
|
||||||
|
} else {
|
||||||
|
p = HLSPlayer
|
||||||
|
}
|
||||||
|
DefaultPreferences.Player = &p
|
||||||
|
}
|
||||||
|
|
||||||
|
if loaded.ProxyStreams != nil {
|
||||||
|
DefaultPreferences.ProxyStreams = loaded.ProxyStreams
|
||||||
|
} else {
|
||||||
|
DefaultPreferences.ProxyStreams = &ProxyStreams
|
||||||
|
}
|
||||||
|
|
||||||
|
if loaded.FullyPreloadTrack != nil {
|
||||||
|
DefaultPreferences.FullyPreloadTrack = loaded.FullyPreloadTrack
|
||||||
|
} else {
|
||||||
|
DefaultPreferences.FullyPreloadTrack = &f
|
||||||
|
}
|
||||||
|
|
||||||
|
if loaded.ProxyImages != nil {
|
||||||
|
DefaultPreferences.ProxyImages = loaded.ProxyImages
|
||||||
|
} else {
|
||||||
|
DefaultPreferences.ProxyImages = &ProxyImages
|
||||||
|
}
|
||||||
|
|
||||||
|
if loaded.ParseDescriptions != nil {
|
||||||
|
DefaultPreferences.ParseDescriptions = loaded.ParseDescriptions
|
||||||
|
} else {
|
||||||
|
DefaultPreferences.ParseDescriptions = &t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func boolean(in string) bool {
|
||||||
|
return strings.Trim(strings.ToLower(in), " ") == "true"
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromEnv() error {
|
||||||
|
env := os.Getenv("DEFAULT_PREFERENCES")
|
||||||
|
if env != "" {
|
||||||
|
var p Preferences
|
||||||
|
err := json.Unmarshal([]byte(env), &p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
loadDefaultPreferences(p)
|
||||||
|
} else {
|
||||||
|
defaultPreferences()
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("PROXY_IMAGES")
|
||||||
|
if env != "" {
|
||||||
|
ProxyImages = boolean(env)
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("IMAGE_CACHE_CONTROL")
|
||||||
|
if env != "" {
|
||||||
|
ImageCacheControl = env
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("PROXY_STREAMS")
|
||||||
|
if env != "" {
|
||||||
|
ProxyStreams = boolean(env)
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("RESTREAM")
|
||||||
|
if env != "" {
|
||||||
|
Restream = boolean(env)
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("RESTREAM_CACHE_CONTROL")
|
||||||
|
if env != "" {
|
||||||
|
RestreamCacheControl = env
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("INSTANCE_INFO")
|
||||||
|
if env != "" {
|
||||||
|
InstanceInfo = boolean(env)
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("CLIENT_ID_TTL")
|
||||||
|
if env != "" {
|
||||||
|
num, err := strconv.ParseInt(env, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ClientIDTTL = time.Duration(num) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("USER_TTL")
|
||||||
|
if env != "" {
|
||||||
|
num, err := strconv.ParseInt(env, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
UserTTL = time.Duration(num) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("USER_CACHE_CLEAN_DELAY")
|
||||||
|
if env != "" {
|
||||||
|
num, err := strconv.ParseInt(env, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
UserCacheCleanDelay = time.Duration(num) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("TRACK_TTL")
|
||||||
|
if env != "" {
|
||||||
|
num, err := strconv.ParseInt(env, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
TrackTTL = time.Duration(num) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("TRACK_CACHE_CLEAN_DELAY")
|
||||||
|
if env != "" {
|
||||||
|
num, err := strconv.ParseInt(env, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
TrackCacheCleanDelay = time.Duration(num) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("PLAYLIST_TTL")
|
||||||
|
if env != "" {
|
||||||
|
num, err := strconv.ParseInt(env, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
PlaylistTTL = time.Duration(num) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("PLAYLIST_CACHE_CLEAN_DELAY")
|
||||||
|
if env != "" {
|
||||||
|
num, err := strconv.ParseInt(env, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
PlaylistCacheCleanDelay = time.Duration(num) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("USER_AGENT")
|
||||||
|
if env != "" {
|
||||||
|
UserAgent = env
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("DNS_CACHE_TTL")
|
||||||
|
if env != "" {
|
||||||
|
num, err := strconv.ParseInt(env, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
DNSCacheTTL = time.Duration(num) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("ADDR")
|
||||||
|
if env != "" {
|
||||||
|
Addr = env
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("PREFORK")
|
||||||
|
if env != "" {
|
||||||
|
Prefork = boolean(env)
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("TRUSTED_PROXY_CHECK")
|
||||||
|
if env != "" {
|
||||||
|
TrustedProxyCheck = boolean(env)
|
||||||
|
}
|
||||||
|
|
||||||
|
env = os.Getenv("TRUSTED_PROXIES")
|
||||||
|
if env != "" {
|
||||||
|
var p []string
|
||||||
|
err := json.Unmarshal([]byte(env), &p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
TrustedProxies = p
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
filename := "soundcloak.json"
|
filename := "soundcloak.json"
|
||||||
if env := os.Getenv("SOUNDCLOAK_CONFIG"); env != "" {
|
if env := os.Getenv("SOUNDCLOAK_CONFIG"); env == "FROM_ENV" {
|
||||||
|
err := fromEnv()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
} else {
|
||||||
filename = env
|
filename = env
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +375,6 @@ func init() {
|
|||||||
DNSCacheTTL *time.Duration
|
DNSCacheTTL *time.Duration
|
||||||
Addr *string
|
Addr *string
|
||||||
Prefork *bool
|
Prefork *bool
|
||||||
EarlyData *bool
|
|
||||||
TrustedProxyCheck *bool
|
TrustedProxyCheck *bool
|
||||||
TrustedProxies *[]string
|
TrustedProxies *[]string
|
||||||
}
|
}
|
||||||
@@ -228,9 +439,6 @@ func init() {
|
|||||||
if config.Prefork != nil {
|
if config.Prefork != nil {
|
||||||
Prefork = *config.Prefork
|
Prefork = *config.Prefork
|
||||||
}
|
}
|
||||||
if config.EarlyData != nil {
|
|
||||||
EarlyData = *config.EarlyData
|
|
||||||
}
|
|
||||||
if config.TrustedProxyCheck != nil {
|
if config.TrustedProxyCheck != nil {
|
||||||
TrustedProxyCheck = *config.TrustedProxyCheck
|
TrustedProxyCheck = *config.TrustedProxyCheck
|
||||||
}
|
}
|
||||||
@@ -238,50 +446,8 @@ func init() {
|
|||||||
TrustedProxies = *config.TrustedProxies
|
TrustedProxies = *config.TrustedProxies
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaults are:
|
|
||||||
// Player: RestreamPlayer if Restream is enabled, otherwise - HLSPlayer
|
|
||||||
// ProxyStreams: same as ProxyStreams in your config (false by default)
|
|
||||||
// FullyPreloadTrack: false
|
|
||||||
// ProxyImages: same as ProxyImages in your config (false by default)
|
|
||||||
// ParseDescriptions: true
|
|
||||||
if config.DefaultPreferences != nil {
|
if config.DefaultPreferences != nil {
|
||||||
var f bool
|
loadDefaultPreferences(*config.DefaultPreferences)
|
||||||
var t = true
|
|
||||||
if config.DefaultPreferences.Player != nil {
|
|
||||||
DefaultPreferences.Player = config.DefaultPreferences.Player
|
|
||||||
} else {
|
|
||||||
var p string
|
|
||||||
if Restream {
|
|
||||||
p = RestreamPlayer
|
|
||||||
} else {
|
|
||||||
p = HLSPlayer
|
|
||||||
}
|
|
||||||
DefaultPreferences.Player = &p
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.DefaultPreferences.ProxyStreams != nil {
|
|
||||||
DefaultPreferences.ProxyStreams = config.DefaultPreferences.ProxyStreams
|
|
||||||
} else {
|
|
||||||
DefaultPreferences.ProxyStreams = &ProxyStreams
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.DefaultPreferences.FullyPreloadTrack != nil {
|
|
||||||
DefaultPreferences.FullyPreloadTrack = config.DefaultPreferences.FullyPreloadTrack
|
|
||||||
} else {
|
|
||||||
DefaultPreferences.FullyPreloadTrack = &f
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.DefaultPreferences.ProxyImages != nil {
|
|
||||||
DefaultPreferences.ProxyImages = config.DefaultPreferences.ProxyImages
|
|
||||||
} else {
|
|
||||||
DefaultPreferences.ProxyImages = &ProxyImages
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.DefaultPreferences.ParseDescriptions != nil {
|
|
||||||
DefaultPreferences.ParseDescriptions = config.DefaultPreferences.ParseDescriptions
|
|
||||||
} else {
|
|
||||||
DefaultPreferences.ParseDescriptions = &t
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
defaultPreferences()
|
defaultPreferences()
|
||||||
}
|
}
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
"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/earlydata"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||||
"github.com/segmentio/encoding/json"
|
"github.com/segmentio/encoding/json"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
@@ -33,9 +32,6 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.Use(recover.New())
|
app.Use(recover.New())
|
||||||
if cfg.EarlyData {
|
|
||||||
app.Use(earlydata.New())
|
|
||||||
}
|
|
||||||
app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed}))
|
app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed}))
|
||||||
|
|
||||||
app.Static("/", "assets", fiber.Static{Compress: true, MaxAge: 3600}) // 1hour
|
app.Static("/", "assets", fiber.Static{Compress: true, MaxAge: 3600}) // 1hour
|
||||||
|
|||||||
Reference in New Issue
Block a user