Add .env file support
This commit is contained in:
parent
14b3863f8e
commit
56ce9fa2f8
13
.env.template
Normal file
13
.env.template
Normal 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
1
go.mod
@ -20,6 +20,7 @@ require (
|
||||
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // 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/sethvargo/go-retry v0.2.4 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@ -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-20190608224051-11cab39313c9/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/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=
|
||||
|
||||
26
main.go
26
main.go
@ -6,6 +6,7 @@ import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
userctx "git.kealoha.me/lks/lenslocked/context"
|
||||
ctrlrs "git.kealoha.me/lks/lenslocked/controllers"
|
||||
@ -13,6 +14,7 @@ import (
|
||||
"git.kealoha.me/lks/lenslocked/models"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/gorilla/csrf"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/pressly/goose/v3"
|
||||
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
@ -53,20 +55,38 @@ func MigrateDB(db *sql.DB, subfs fs.FS) error {
|
||||
}
|
||||
|
||||
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 {
|
||||
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()
|
||||
defer db.Close()
|
||||
err := MigrateDB(db, migrations.FS)
|
||||
err = MigrateDB(db, migrations.FS)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
userService := models.UserService{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)
|
||||
|
||||
umw := userctx.UserMiddleware{SS: &sessionService}
|
||||
|
||||
@ -2,15 +2,10 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/go-mail/mail/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultSender = os.Getenv("LENSLOCKED_EMAIL_FROM")
|
||||
)
|
||||
|
||||
type Email struct {
|
||||
To string
|
||||
Subject string
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user