77package config
88
99import (
10- "github.com/spf13/viper "
10+ "encoding/hex "
1111 "fmt"
12+
13+ "github.com/spf13/viper"
1214)
1315
1416type Config struct {
15- // Version of the server
16- Version string `mapstructure:"VERSION"`
17+ // Version of the server
18+ Version string `mapstructure:"VERSION"`
1719 // Accessible server address
1820 CdnURL string `mapstructure:"CDN_URL"`
1921 // The port to run the server on
2022 ServerPort int `mapstructure:"SERVER_PORT"`
2123 // This enables extra logging, INCLUDING SENSETIVE INFORMATION like BLUESKY TOKENS. Useful for debugging with tools like insomnia. DO NOT USE ON PUBLIC SERVERS
2224 // Also requires all passwords start with "dev_" to work
2325 DeveloperMode bool `mapstructure:"DEVELOPER_MODE"`
24- // Collects analytics on users.
25- TrackAnalytics bool `mapstructure:"TRACK_ANALYTICS"`
26- // Database type (mysql, postgres, sqlite)
27- DatabaseType string `mapstructure:"DATABASE_TYPE"`
28- // Database path
29- DatabasePath string `mapstructure:"DATABASE_PATH"`
30-
31- UseXForwardedFor bool `mapstructure:"USE_X_FORWARDED_FOR"`
32-
33- ImgDisplayText string `mapstructure:"IMG_DISPLAY_TEXT"`
34- ImgURLText string `mapstructure:"IMG_URL_TEXT"`
35- VidDisplayText string `mapstructure:"VID_DISPLAY_TEXT"`
36- VidURLText string `mapstructure:"VID_URL_TEXT"`
37-
38- // Secret key used for JWT. Must be at least 32 bytes long. Keep this secret!
39- SecretKey string `mapstructure:"SECRET_KEY"`
40- // The security key but in bytes.
41- SecretKeyBytes []byte
42- // Min Version token version the server will accept
43- MinTokenVersion int `mapstructure:"MIN_TOKEN_VERSION"`
44- // Server Identifier, used for knowing what server a token belongs to.
45- ServerIdentifier string `mapstructure:"SERVER_IDENTIFIER"`
46- // Server URLs used for contacting the server
47- ServerURLs []string `mapstructure:"SERVER_URLS"`
26+ // Collects analytics on users.
27+ TrackAnalytics bool `mapstructure:"TRACK_ANALYTICS"`
28+ // Database type (mysql, postgres, sqlite)
29+ DatabaseType string `mapstructure:"DATABASE_TYPE"`
30+ // Database path
31+ DatabasePath string `mapstructure:"DATABASE_PATH"`
32+
33+ UseXForwardedFor bool `mapstructure:"USE_X_FORWARDED_FOR"`
34+
35+ ImgDisplayText string `mapstructure:"IMG_DISPLAY_TEXT"`
36+ ImgURLText string `mapstructure:"IMG_URL_TEXT"`
37+ VidDisplayText string `mapstructure:"VID_DISPLAY_TEXT"`
38+ VidURLText string `mapstructure:"VID_URL_TEXT"`
39+
40+ // Secret key used for JWT. Must be at least 32 bytes long. Keep this secret!
41+ SecretKey string `mapstructure:"SECRET_KEY"`
42+ // The security key but in bytes.
43+ SecretKeyBytes []byte
44+ // Min Version token version the server will accept
45+ MinTokenVersion int `mapstructure:"MIN_TOKEN_VERSION"`
46+ // Server Identifier, used for knowing what server a token belongs to.
47+ ServerIdentifier string `mapstructure:"SERVER_IDENTIFIER"`
48+ // Server URLs used for contacting the server
49+ ServerURLs []string `mapstructure:"SERVER_URLS"`
50+
51+ //notifications
52+ NotificationTrustedServer string `mapstructure:"NOTIFICATION_TRUSTED_SERVER"`
53+ NotificationFeedbackSecretString string `mapstructure:"NOTIFICATION_FEEDBACK_SECRET"`
54+ NotificationFeedbackSecret []byte
4855}
4956
5057// Loads our config files.
5158func LoadConfig () (* Config , error ) {
52- viper .SetConfigName ("config" ) // Name of the config file (without extension)
53- viper .SetConfigType ("yaml" ) // File type
54- viper .AddConfigPath ("." ) // Look for config in the current directory
55- viper .AddConfigPath ("/config/" ) // Path for Docker setups
56-
57- // Read environment variables with a specific prefix
58- viper .SetEnvPrefix ("TWITTER_BRIDGE" )
59-
60- // Set default values
61- viper .SetDefault ("VERSION" , "1.0.6" ) // wait till i forget to update this
62- viper .SetDefault ("SERVER_PORT" , "3000" )
63- viper .SetDefault ("DEVELOPER_MODE" , false )
64- viper .SetDefault ("DATABASE_TYPE" , "sqlite" )
65- viper .SetDefault ("DATABASE_PATH" , "./db/twitterbridge.db" )
66- viper .SetDefault ("TRACK_ANALYTICS" , true )
67- viper .SetDefault ("CDN_URL" , "http://127.0.0.1:3000" )
68- viper .SetDefault ("USE_X_FORWARDED_FOR" , false )
69- viper .SetDefault ("IMG_DISPLAY_TEXT" , "pic.twitter.com/{shortblob}" )
70- viper .SetDefault ("VID_DISPLAY_TEXT" , "pic.twitter.com/{shortblob}" )
71- viper .SetDefault ("IMG_URL_TEXT" , "http://127.0.0.1:3000/img/{shortblob}" )
72- viper .SetDefault ("VID_URL_TEXT" , "http://127.0.0.1:3000/img/{shortblob}" )
73- viper .SetDefault ("SECRET_KEY" , "" )
74- viper .SetDefault ("MIN_TOKEN_VERSION" , 1 )
75-
76- // Read config file
77- if err := viper .ReadInConfig (); err != nil {
78- fmt .Println ("No config file found, relying on environment variables" )
79- }
80-
81- // Bind config to struct
82- var config Config
83- if err := viper .Unmarshal (& config ); err != nil {
84- return nil , err
85- }
86-
87- config .SecretKeyBytes = []byte (config .SecretKey )
88-
89- return & config , nil
90- }
59+ viper .SetConfigName ("config" ) // Name of the config file (without extension)
60+ viper .SetConfigType ("yaml" ) // File type
61+ viper .AddConfigPath ("." ) // Look for config in the current directory
62+ viper .AddConfigPath ("/config/" ) // Path for Docker setups
63+
64+ // Read environment variables with a specific prefix
65+ viper .SetEnvPrefix ("TWITTER_BRIDGE" )
66+
67+ // Set default values
68+ viper .SetDefault ("VERSION" , "1.0.6" ) // wait till i forget to update this
69+ viper .SetDefault ("SERVER_PORT" , "3000" )
70+ viper .SetDefault ("DEVELOPER_MODE" , false )
71+ viper .SetDefault ("DATABASE_TYPE" , "sqlite" )
72+ viper .SetDefault ("DATABASE_PATH" , "./db/twitterbridge.db" )
73+ viper .SetDefault ("TRACK_ANALYTICS" , true )
74+ viper .SetDefault ("CDN_URL" , "http://127.0.0.1:3000" )
75+ viper .SetDefault ("USE_X_FORWARDED_FOR" , false )
76+ viper .SetDefault ("IMG_DISPLAY_TEXT" , "pic.twitter.com/{shortblob}" )
77+ viper .SetDefault ("VID_DISPLAY_TEXT" , "pic.twitter.com/{shortblob}" )
78+ viper .SetDefault ("IMG_URL_TEXT" , "http://127.0.0.1:3000/img/{shortblob}" )
79+ viper .SetDefault ("VID_URL_TEXT" , "http://127.0.0.1:3000/img/{shortblob}" )
80+ viper .SetDefault ("SECRET_KEY" , "" )
81+ viper .SetDefault ("MIN_TOKEN_VERSION" , 1 )
82+ viper .SetDefault ("NOTIFICATION_TRUSTED_SERVER" , "" )
83+ viper .SetDefault ("NOTIFICATION_SECRET_KEY" , "" )
84+
85+ // Read config file
86+ if err := viper .ReadInConfig (); err != nil {
87+ fmt .Println ("No config file found, relying on environment variables" )
88+ }
89+
90+ // Bind config to struct
91+ var config Config
92+ if err := viper .Unmarshal (& config ); err != nil {
93+ return nil , err
94+ }
95+
96+ config .SecretKeyBytes = []byte (config .SecretKey )
97+ config .NotificationFeedbackSecret = make ([]byte , hex .DecodedLen (len (config .NotificationFeedbackSecretString )))
98+ _ , err := hex .Decode (config .NotificationFeedbackSecret , []byte (config .NotificationFeedbackSecretString ))
99+ if err != nil {
100+ panic ("you failed to set the notification feedback secret to a correct value. set it to be correct or not at all" )
101+ }
102+
103+ return & config , nil
104+ }
0 commit comments