Add .env file support

This commit is contained in:
Lucas Schumacher 2024-09-01 20:23:31 -04:00
parent 14b3863f8e
commit 56ce9fa2f8
5 changed files with 39 additions and 8 deletions

13
.env.template Normal file
View File

@ -0,0 +1,13 @@
# Cryptographic key for generating secure CSRF protection tokens
LENSLOCKED_CSRF_KEY=
# Postgresql DB c nnection settings
# Make sure to enable ssl when deploying to production
LENSLOCKED_DB_STRING="host=localhost port=5432 user=changeme dbname=lenslocked sslmode=disable"
# SMTP Email settings
LENSLOCKED_EMAIL_HOST=
LENSLOCKED_EMAIL_PORT=
LENSLOCKED_EMAIL_USERNAME=
LENSLOCKED_EMAIL_PASSWORD=
LENSLOCKED_EMAIL_FROM=

1
go.mod
View File

@ -20,6 +20,7 @@ require (
github.com/jackc/pgproto3/v2 v2.3.3 // indirect github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect github.com/jackc/pgtype v1.14.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect github.com/mfridman/interpolate v0.0.2 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect github.com/sethvargo/go-retry v0.2.4 // indirect
go.uber.org/multierr v1.11.0 // indirect go.uber.org/multierr v1.11.0 // indirect

2
go.sum
View File

@ -78,6 +78,8 @@ github.com/jackc/pgx/v4 v4.18.3/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=

26
main.go
View File

@ -6,6 +6,7 @@ import (
"io/fs" "io/fs"
"net/http" "net/http"
"os" "os"
"strconv"
userctx "git.kealoha.me/lks/lenslocked/context" userctx "git.kealoha.me/lks/lenslocked/context"
ctrlrs "git.kealoha.me/lks/lenslocked/controllers" ctrlrs "git.kealoha.me/lks/lenslocked/controllers"
@ -13,6 +14,7 @@ import (
"git.kealoha.me/lks/lenslocked/models" "git.kealoha.me/lks/lenslocked/models"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/gorilla/csrf" "github.com/gorilla/csrf"
"github.com/joho/godotenv"
"github.com/pressly/goose/v3" "github.com/pressly/goose/v3"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
@ -53,20 +55,38 @@ func MigrateDB(db *sql.DB, subfs fs.FS) error {
} }
func main() { func main() {
csrfKey := []byte(os.Getenv("LENSLOCKED_CSRF_KEY")) err := godotenv.Load()
if err != nil {
fmt.Println("Warning: Could not load .env file")
}
var (
email_host = os.Getenv("LENSLOCKED_EMAIL_HOST")
email_port_str = os.Getenv("LENSLOCKED_EMAIL_PORT")
email_username = os.Getenv("LENSLOCKED_EMAIL_USERNAME")
email_pass = os.Getenv("LENSLOCKED_EMAIL_PASSWORD")
email_sender = os.Getenv("LENSLOCKED_EMAIL_FROM")
csrfKey = []byte(os.Getenv("LENSLOCKED_CSRF_KEY"))
)
if len(csrfKey) < 32 { if len(csrfKey) < 32 {
panic("Error: bad csrf protection key\nPlease set a key with the LENSLOCKED_CSRF_KEY env var.") panic("Error: no or bad csrf protection key\nPlease set the LENSLOCKED_CSRF_KEY env var to a key at least 32 characters long.")
}
email_port, err := strconv.Atoi(email_port_str)
if err != nil {
fmt.Println("Warning: Invalid STMP port set in LENSLOCKED_EMAIL_PORT. Using port 587")
email_port = 587
} }
db := ConnectDB() db := ConnectDB()
defer db.Close() defer db.Close()
err := MigrateDB(db, migrations.FS) err = MigrateDB(db, migrations.FS)
if err != nil { if err != nil {
panic(err) panic(err)
} }
userService := models.UserService{DB: db} userService := models.UserService{DB: db}
sessionService := models.SessionService{DB: db} sessionService := models.SessionService{DB: db}
_ = models.NewEmailService(email_host, email_port, email_username, email_pass, email_sender)
var usersCtrlr ctrlrs.Users = ctrlrs.Default(&userService, &sessionService) var usersCtrlr ctrlrs.Users = ctrlrs.Default(&userService, &sessionService)
umw := userctx.UserMiddleware{SS: &sessionService} umw := userctx.UserMiddleware{SS: &sessionService}

View File

@ -2,15 +2,10 @@ package models
import ( import (
"fmt" "fmt"
"os"
"github.com/go-mail/mail/v2" "github.com/go-mail/mail/v2"
) )
var (
defaultSender = os.Getenv("LENSLOCKED_EMAIL_FROM")
)
type Email struct { type Email struct {
To string To string
Subject string Subject string