summaryrefslogtreecommitdiff
path: root/internal/config/config.go
blob: 86d0d5bb5cf8d74ddc26dca09c5a16660c101c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package config

import (
	"fmt"
	"os"
	"strconv"
)

// Config holds all application configuration
type Config struct {
	// API Keys
	TodoistAPIKey     string
	PlanToEatAPIKey   string
	PlanToEatSession  string // Session cookie for web scraping
	TrelloAPIKey      string
	TrelloToken       string

	// Google Calendar
	GoogleCredentialsFile string
	GoogleCalendarID      string

	// Google Tasks
	GoogleTasksListID string

	// Paths
	DatabasePath string
	MigrationDir string
	TemplateDir  string
	StaticDir    string

	// Server
	Port            string
	CacheTTLMinutes int
	Debug           bool

	// Display
	Timezone string // IANA timezone name (e.g., "Pacific/Honolulu")

	// WebAuthn
	WebAuthnRPID   string // Relying Party ID (domain, e.g., "doot.terst.org")
	WebAuthnOrigin string // Expected origin (e.g., "https://doot.terst.org")
}

// Load reads configuration from environment variables
func Load() (*Config, error) {
	cfg := &Config{
		// API Keys
		TodoistAPIKey:    os.Getenv("TODOIST_API_KEY"),
		PlanToEatAPIKey:  os.Getenv("PLANTOEAT_API_KEY"),
		PlanToEatSession: os.Getenv("PLANTOEAT_SESSION"),
		TrelloAPIKey:     os.Getenv("TRELLO_API_KEY"),
		TrelloToken:      os.Getenv("TRELLO_TOKEN"),

		// Google Calendar
		GoogleCredentialsFile: os.Getenv("GOOGLE_CREDENTIALS_FILE"),
		GoogleCalendarID:      getEnvWithDefault("GOOGLE_CALENDAR_ID", "primary"),

		// Google Tasks
		GoogleTasksListID: getEnvWithDefault("GOOGLE_TASKS_LIST_ID", "@default"),

		// Paths
		DatabasePath: getEnvWithDefault("DATABASE_PATH", "./dashboard.db"),
		MigrationDir: getEnvWithDefault("MIGRATION_DIR", "migrations"),
		TemplateDir:  getEnvWithDefault("TEMPLATE_DIR", "web/templates"),
		StaticDir:    getEnvWithDefault("STATIC_DIR", "web/static"),

		// Server
		Port:            getEnvWithDefault("PORT", "8080"),
		CacheTTLMinutes: getEnvAsInt("CACHE_TTL_MINUTES", 5),
		Debug:           getEnvAsBool("DEBUG", false),

		// Display
		Timezone: getEnvWithDefault("TIMEZONE", "Pacific/Honolulu"),

		// WebAuthn
		WebAuthnRPID:   os.Getenv("WEBAUTHN_RP_ID"),
		WebAuthnOrigin: os.Getenv("WEBAUTHN_ORIGIN"),
	}

	// Validate required fields
	if err := cfg.Validate(); err != nil {
		return nil, err
	}

	return cfg, nil
}

// Validate checks that required configuration is present
func (c *Config) Validate() error {
	// Require both Todoist and Trello (primary task management systems)
	if c.TodoistAPIKey == "" {
		return fmt.Errorf("TODOIST_API_KEY is required")
	}

	if c.TrelloAPIKey == "" {
		return fmt.Errorf("TRELLO_API_KEY is required")
	}

	if c.TrelloToken == "" {
		return fmt.Errorf("TRELLO_TOKEN is required")
	}

	return nil
}

// HasPlanToEat checks if PlanToEat is configured (API key or session)
func (c *Config) HasPlanToEat() bool {
	return c.PlanToEatAPIKey != "" || c.PlanToEatSession != ""
}

// HasPlanToEatSession checks if PlanToEat session cookie is configured
func (c *Config) HasPlanToEatSession() bool {
	return c.PlanToEatSession != ""
}

// HasTrello checks if Trello is configured
func (c *Config) HasTrello() bool {
	return c.TrelloAPIKey != "" && c.TrelloToken != ""
}

// HasGoogleCalendar checks if Google Calendar is configured
func (c *Config) HasGoogleCalendar() bool {
	return c.GoogleCredentialsFile != ""
}

// HasGoogleTasks checks if Google Tasks is configured
func (c *Config) HasGoogleTasks() bool {
	return c.GoogleCredentialsFile != "" && c.GoogleTasksListID != ""
}

// getEnvWithDefault returns environment variable value or default if not set
func getEnvWithDefault(key, defaultValue string) string {
	if value := os.Getenv(key); value != "" {
		return value
	}
	return defaultValue
}

// getEnvAsInt returns environment variable as int or default if not set or invalid
func getEnvAsInt(key string, defaultValue int) int {
	valueStr := os.Getenv(key)
	if valueStr == "" {
		return defaultValue
	}

	value, err := strconv.Atoi(valueStr)
	if err != nil {
		return defaultValue
	}

	return value
}

// getEnvAsBool returns environment variable as bool or default if not set
func getEnvAsBool(key string, defaultValue bool) bool {
	valueStr := os.Getenv(key)
	if valueStr == "" {
		return defaultValue
	}

	value, err := strconv.ParseBool(valueStr)
	if err != nil {
		return defaultValue
	}

	return value
}