chore: added env loading and example env 82a69edc
Steve Simkins · 2026-07-05 09:54 4 file(s) · +37 −0
.env.example (added) +6 −0
1 +
# Base URL used for og:url / og:image and absolute links
2 +
BASE_URL=http://localhost:4000
3 +
4 +
# Host and port the server binds to
5 +
HOST=127.0.0.1
6 +
PORT=4000
.gitignore +1 −0
1 1
feeds
2 +
.env
app.go +28 −0
90 90
	}
91 91
	return fallback
92 92
}
93 +
94 +
// loadDotEnv reads KEY=VALUE pairs from a .env file in the working directory
95 +
// and sets them as environment variables, without overriding vars already set.
96 +
func loadDotEnv(path string) {
97 +
	data, err := os.ReadFile(path)
98 +
	if err != nil {
99 +
		return
100 +
	}
101 +
	for line := range strings.SplitSeq(string(data), "\n") {
102 +
		line = strings.TrimSpace(line)
103 +
		if line == "" || strings.HasPrefix(line, "#") {
104 +
			continue
105 +
		}
106 +
		key, value, ok := strings.Cut(line, "=")
107 +
		if !ok {
108 +
			continue
109 +
		}
110 +
		key = strings.TrimSpace(key)
111 +
		value = strings.TrimSpace(value)
112 +
		value = strings.Trim(value, `"'`)
113 +
		if key == "" {
114 +
			continue
115 +
		}
116 +
		if _, exists := os.LookupEnv(key); !exists {
117 +
			os.Setenv(key, value)
118 +
		}
119 +
	}
120 +
}
main.go +2 −0
11 11
)
12 12
13 13
func main() {
14 +
	loadDotEnv(".env")
15 +
14 16
	logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
15 17
16 18
	tmpl := template.Must(template.New("").ParseFS(appFS, "templates/*.html"))