This repository has been archived on 2022-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
elon-eats-my-tweets/config/config.go

40 lines
943 B
Go
Raw Normal View History

2022-05-19 19:51:12 +00:00
package config
2022-05-20 00:26:42 +00:00
import (
"errors"
"os"
)
2022-05-19 19:51:12 +00:00
2022-05-20 19:52:54 +00:00
type TwitterConfig struct {
ClientID, ClientSecret, CallbackURL, AuthorizeURL, TokenURL string
2022-05-19 19:51:12 +00:00
}
type Config struct {
PublicPath string
2022-05-20 00:26:42 +00:00
SessionKey string
2022-05-19 23:03:11 +00:00
ListenAddr string
2022-05-20 19:52:54 +00:00
Twitter TwitterConfig
2022-05-19 19:51:12 +00:00
}
2022-05-20 00:26:42 +00:00
func NewFromEnv() (Config, error) {
2022-05-19 23:03:11 +00:00
listenAddr := os.Getenv("ELON_LISTEN_ADDR")
if listenAddr == "" {
listenAddr = ":8000"
}
2022-05-20 00:26:42 +00:00
sessionKey := os.Getenv("ELON_SESSION_KEY")
if sessionKey == "" {
return Config{}, errors.New("missing ELON_SESSION_KEY")
}
2022-05-19 19:51:12 +00:00
return Config{
PublicPath: os.Getenv("ELON_PUBLIC_PATH"),
2022-05-20 00:26:42 +00:00
SessionKey: sessionKey,
2022-05-19 23:03:11 +00:00
ListenAddr: listenAddr,
2022-05-20 19:52:54 +00:00
Twitter: TwitterConfig{
2022-05-19 19:51:12 +00:00
ClientID: os.Getenv("ELON_TWITTER_CLIENT_ID"),
ClientSecret: os.Getenv("ELON_TWITTER_CLIENT_SECRET"),
CallbackURL: os.Getenv("ELON_TWITTER_CALLBACK_URL"),
2022-05-20 19:52:54 +00:00
AuthorizeURL: os.Getenv("ELON_TWITTER_AUTHORIZE_URL"),
TokenURL: os.Getenv("ELON_TWITTER_TOKEN_URL"),
2022-05-19 19:51:12 +00:00
},
2022-05-20 00:26:42 +00:00
}, nil
2022-05-19 19:51:12 +00:00
}