Read settings from .env
This commit is contained in:
parent
ffb19acef7
commit
5c0d6180ed
5 changed files with 28 additions and 14 deletions
10
.env.example
10
.env.example
|
|
@ -1,11 +1,5 @@
|
||||||
|
|
||||||
PORT=8000
|
PORT=8000
|
||||||
DATABASE_URL=sqlite:///db.sqlite3
|
HOST=127.0.0.1
|
||||||
ADMIN_EMAIL=
|
DATABASE_URL=postgresql://localhost/feedvault?user=feedvault&password=feedvault
|
||||||
EMAIL_HOST_USER=
|
|
||||||
EMAIL_HOST_PASSWORD=
|
|
||||||
EMAIL_HOST=gmail.com
|
|
||||||
EMAIL_PORT=587
|
|
||||||
DISCORD_WEBHOOK_URL=
|
DISCORD_WEBHOOK_URL=
|
||||||
APP_ENV=development
|
|
||||||
USER_AGENT=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0
|
|
||||||
|
|
|
||||||
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
|
|
@ -27,6 +27,7 @@
|
||||||
"feedvault",
|
"feedvault",
|
||||||
"gaierror",
|
"gaierror",
|
||||||
"giga",
|
"giga",
|
||||||
|
"godotenv",
|
||||||
"gofeed",
|
"gofeed",
|
||||||
"gomod",
|
"gomod",
|
||||||
"gorm",
|
"gorm",
|
||||||
|
|
@ -36,6 +37,7 @@
|
||||||
"huaweimobilewifi",
|
"huaweimobilewifi",
|
||||||
"isready",
|
"isready",
|
||||||
"jackc",
|
"jackc",
|
||||||
|
"joho",
|
||||||
"ldflags",
|
"ldflags",
|
||||||
"leftright",
|
"leftright",
|
||||||
"levelname",
|
"levelname",
|
||||||
|
|
|
||||||
1
go.mod
1
go.mod
|
|
@ -4,6 +4,7 @@ go 1.22.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/jackc/pgx/v5 v5.5.3
|
github.com/jackc/pgx/v5 v5.5.3
|
||||||
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/mmcdole/gofeed v1.2.1
|
github.com/mmcdole/gofeed v1.2.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -14,6 +14,8 @@ github.com/jackc/pgx/v5 v5.5.3 h1:Ces6/M3wbDXYpM8JyyPD57ivTtJACFZJd885pdIaV2s=
|
||||||
github.com/jackc/pgx/v5 v5.5.3/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
|
github.com/jackc/pgx/v5 v5.5.3/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
|
||||||
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
|
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
|
||||||
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||||
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
github.com/mmcdole/gofeed v1.2.1 h1:tPbFN+mfOLcM1kDF1x2c/N68ChbdBatkppdzf/vDe1s=
|
github.com/mmcdole/gofeed v1.2.1 h1:tPbFN+mfOLcM1kDF1x2c/N68ChbdBatkppdzf/vDe1s=
|
||||||
|
|
|
||||||
27
main.go
27
main.go
|
|
@ -3,11 +3,13 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/TheLovinator1/FeedVault/db"
|
"github.com/TheLovinator1/FeedVault/db"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
_ "github.com/joho/godotenv/autoload"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -15,17 +17,22 @@ var (
|
||||||
DB *db.Queries
|
DB *db.Queries
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() { log.SetFlags(log.LstdFlags | log.Lshortfile) }
|
// Connect to our PostgreSQL database and store the connection pool in the DB variable that we can use throughout our application.
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// Open a database connection
|
// Open a database connection
|
||||||
dbpool, err := pgxpool.New(ctx, "postgresql://localhost/feedvault?user=feedvault&password=feedvault")
|
databaseURL := os.Getenv("DATABASE_URL")
|
||||||
|
if databaseURL == "" {
|
||||||
|
databaseURL = "postgresql://localhost/feedvault?user=feedvault&password=feedvault"
|
||||||
|
}
|
||||||
|
log.Printf("Connecting to database: %s", databaseURL)
|
||||||
|
dbpool, err := pgxpool.New(ctx, databaseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("pgx.Connect(): %v", err)
|
log.Fatalf("pgxpool.New(): %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a new DB object
|
||||||
DB = db.New(dbpool)
|
DB = db.New(dbpool)
|
||||||
|
|
||||||
// Test the connection
|
// Test the connection
|
||||||
|
|
@ -51,12 +58,20 @@ func main() {
|
||||||
mux.HandleFunc("/upload_opml", UploadOpmlHandler)
|
mux.HandleFunc("/upload_opml", UploadOpmlHandler)
|
||||||
|
|
||||||
// Create server
|
// Create server
|
||||||
|
port := os.Getenv("PORT")
|
||||||
|
if port == "" {
|
||||||
|
port = "8000"
|
||||||
|
}
|
||||||
|
host := os.Getenv("HOST")
|
||||||
|
if host == "" {
|
||||||
|
host = "127.0.0.1"
|
||||||
|
}
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: "127.0.0.1:8000",
|
Addr: host + ":" + port,
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Print("Server started on http://localhost:8000/ <Ctrl-C> to stop")
|
log.Print("Server started on http://" + host + ":" + port + " <Ctrl-C> to stop")
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
log.Fatalf("ListenAndServe(): %v", err)
|
log.Fatalf("ListenAndServe(): %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue