2026-01-28 12:50:11 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
2026-01-30 14:14:54 +00:00
|
|
|
"golang.org/x/oauth2"
|
2026-01-28 12:50:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type (
|
2026-01-30 14:14:54 +00:00
|
|
|
GoogleConfig struct {
|
|
|
|
|
ApiKey string `toml:"api_key"`
|
|
|
|
|
ClientID string `toml:"client_id"`
|
|
|
|
|
ClientSecret string `toml:"client_secret"`
|
2026-01-28 12:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 14:14:54 +00:00
|
|
|
Config struct {
|
2026-06-08 02:49:39 +01:00
|
|
|
Host string `toml:"host" comment:"Address to host OAuth2 redirect flow"`
|
|
|
|
|
Secure bool `toml:"bool" comment:"Whether or not host is secured with TLS (HTTPS)"`
|
|
|
|
|
Port int16 `toml:"port"`
|
|
|
|
|
VODDirectory string `toml:"vod_directory" comment:"Directory to be used for VOD management"`
|
|
|
|
|
Google GoogleConfig `toml:"google"`
|
|
|
|
|
Token *oauth2.Token `toml:"token" comment:"This section is filled in automatically on a successful authentication flow."`
|
2026-01-28 12:50:11 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var defaultConfig = Config{
|
2026-06-08 02:49:39 +01:00
|
|
|
Host: "localhost",
|
|
|
|
|
Secure: false,
|
|
|
|
|
Port: 8080,
|
2026-01-28 12:50:11 +00:00
|
|
|
Google: GoogleConfig{
|
|
|
|
|
ApiKey: "<your API key here>",
|
|
|
|
|
ClientID: "<your client ID here>",
|
|
|
|
|
ClientSecret: "<your client secret here>",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-31 02:28:09 +00:00
|
|
|
var CONFIG_FILENAME string = "config.toml"
|
2026-01-28 12:50:11 +00:00
|
|
|
|
|
|
|
|
func ReadConfig(filename string) (*Config, error) {
|
|
|
|
|
cfgBytes, err := os.ReadFile(filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to open file: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 14:14:54 +00:00
|
|
|
var config Config
|
2026-01-28 12:50:11 +00:00
|
|
|
err = toml.Unmarshal(cfgBytes, &config)
|
2026-06-08 02:49:39 +01:00
|
|
|
if err != nil { return nil, err }
|
2026-01-28 12:50:11 +00:00
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 14:14:54 +00:00
|
|
|
func WriteConfig(cfg *Config, filename string) error {
|
|
|
|
|
file, err := os.OpenFile(filename, os.O_CREATE | os.O_RDWR, 0644)
|
2026-01-28 12:50:11 +00:00
|
|
|
if err != nil { return err }
|
|
|
|
|
|
2026-01-30 14:14:54 +00:00
|
|
|
err = toml.NewEncoder(file).Encode(cfg)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-01-28 12:50:11 +00:00
|
|
|
|
2026-01-30 14:14:54 +00:00
|
|
|
func GenerateConfig(filename string) error {
|
|
|
|
|
return WriteConfig(&defaultConfig, filename)
|
2026-01-28 12:50:11 +00:00
|
|
|
}
|