2023-02-05 08:53:50 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"html/template"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/barertc/pkg/log"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
)
|
|
|
|
|
2023-02-09 04:01:06 +00:00
|
|
|
// Version of the config format - when new fields are added, it will attempt
|
|
|
|
// to write the settings.toml to disk so new defaults populate.
|
2023-03-22 04:29:24 +00:00
|
|
|
var currentVersion = 2
|
2023-02-09 04:01:06 +00:00
|
|
|
|
2023-02-05 08:53:50 +00:00
|
|
|
// Config for your BareRTC app.
|
|
|
|
type Config struct {
|
2023-02-09 04:01:06 +00:00
|
|
|
Version int // will re-save your settings.toml on migrations
|
|
|
|
|
2023-02-05 08:53:50 +00:00
|
|
|
JWT struct {
|
|
|
|
Enabled bool
|
2023-02-06 01:42:09 +00:00
|
|
|
Strict bool
|
2023-02-05 08:53:50 +00:00
|
|
|
SecretKey string
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:26:00 +00:00
|
|
|
Title string
|
2023-02-06 21:27:29 +00:00
|
|
|
Branding string
|
2023-02-06 01:42:09 +00:00
|
|
|
WebsiteURL string
|
2023-02-11 06:46:39 +00:00
|
|
|
|
2023-02-10 07:03:06 +00:00
|
|
|
CORSHosts []string
|
2023-02-11 06:46:39 +00:00
|
|
|
PermitNSFW bool
|
2023-02-06 01:42:09 +00:00
|
|
|
|
2023-02-09 04:01:06 +00:00
|
|
|
UseXForwardedFor bool
|
|
|
|
|
2023-03-22 04:29:24 +00:00
|
|
|
WebSocketReadLimit int64
|
|
|
|
MaxImageWidth int
|
|
|
|
PreviewImageWidth int
|
|
|
|
|
2023-02-05 08:53:50 +00:00
|
|
|
PublicChannels []Channel
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChannels returns a JavaScript safe array of the default PublicChannels.
|
|
|
|
func (c Config) GetChannels() template.JS {
|
|
|
|
data, _ := json.Marshal(c.PublicChannels)
|
|
|
|
return template.JS(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channel config for a default public room.
|
|
|
|
type Channel struct {
|
|
|
|
ID string // Like "lobby"
|
|
|
|
Name string // Like "Main Chat Room"
|
|
|
|
Icon string `toml:",omitempty"` // CSS class names for room icon (optional)
|
2023-02-06 01:42:09 +00:00
|
|
|
|
|
|
|
// ChatServer messages to send to the user immediately upon connecting.
|
|
|
|
WelcomeMessages []string
|
2023-02-05 08:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Current loaded configuration.
|
|
|
|
var Current = DefaultConfig()
|
|
|
|
|
|
|
|
// DefaultConfig returns sensible defaults and will write the initial
|
|
|
|
// settings.toml file to disk.
|
|
|
|
func DefaultConfig() Config {
|
|
|
|
var c = Config{
|
2023-02-06 04:26:00 +00:00
|
|
|
Title: "BareRTC",
|
2023-02-06 22:21:08 +00:00
|
|
|
Branding: "BareRTC",
|
2023-02-06 01:42:09 +00:00
|
|
|
WebsiteURL: "https://www.example.com",
|
2023-02-10 07:03:06 +00:00
|
|
|
CORSHosts: []string{
|
|
|
|
"https://www.example.com",
|
|
|
|
},
|
2023-03-22 04:29:24 +00:00
|
|
|
WebSocketReadLimit: 1024 * 1024 * 40, // 40 MB.
|
|
|
|
MaxImageWidth: 1280,
|
|
|
|
PreviewImageWidth: 360,
|
2023-02-05 08:53:50 +00:00
|
|
|
PublicChannels: []Channel{
|
|
|
|
{
|
|
|
|
ID: "lobby",
|
|
|
|
Name: "Lobby",
|
|
|
|
Icon: "fa fa-gavel",
|
2023-02-06 01:42:09 +00:00
|
|
|
WelcomeMessages: []string{
|
|
|
|
"Welcome to the chat server!",
|
|
|
|
"Please follow the basic rules:\n\n1. Have fun\n2. Be kind",
|
|
|
|
},
|
2023-02-05 08:53:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "offtopic",
|
|
|
|
Name: "Off Topic",
|
2023-02-06 01:42:09 +00:00
|
|
|
WelcomeMessages: []string{
|
|
|
|
"Welcome to the Off Topic channel!",
|
|
|
|
},
|
2023-02-05 08:53:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-02-06 01:42:09 +00:00
|
|
|
c.JWT.Strict = true
|
2023-02-05 08:53:50 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadSettings reads a settings.toml from disk if available.
|
|
|
|
func LoadSettings() error {
|
|
|
|
data, err := os.ReadFile("./settings.toml")
|
|
|
|
if err != nil {
|
|
|
|
// Settings file didn't exist, create the default one.
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
WriteSettings()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = toml.Decode(string(data), &Current)
|
2023-02-09 04:01:06 +00:00
|
|
|
|
|
|
|
// Have we added new config fields? Save the settings.toml.
|
2023-03-22 04:29:24 +00:00
|
|
|
if Current.Version != currentVersion {
|
2023-02-09 04:01:06 +00:00
|
|
|
log.Warn("New options are available for your settings.toml file. Your settings will be re-saved now.")
|
|
|
|
Current.Version = currentVersion
|
|
|
|
if err := WriteSettings(); err != nil {
|
|
|
|
log.Error("Couldn't write your settings.toml file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 08:53:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteSettings will commit the settings.toml to disk.
|
|
|
|
func WriteSettings() error {
|
|
|
|
log.Error("Note: initial settings.toml was written to disk.")
|
|
|
|
var buf = new(bytes.Buffer)
|
|
|
|
err := toml.NewEncoder(buf).Encode(Current)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.WriteFile("./settings.toml", buf.Bytes(), 0644)
|
|
|
|
}
|