Read settings from .env
This commit is contained in:
parent
ffb19acef7
commit
5c0d6180ed
5 changed files with 28 additions and 14 deletions
27
main.go
27
main.go
|
|
@ -3,11 +3,13 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/TheLovinator1/FeedVault/db"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -15,17 +17,22 @@ var (
|
|||
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() {
|
||||
ctx := context.Background()
|
||||
|
||||
// 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 {
|
||||
log.Fatalf("pgx.Connect(): %v", err)
|
||||
log.Fatalf("pgxpool.New(): %v", err)
|
||||
}
|
||||
|
||||
// Create a new DB object
|
||||
DB = db.New(dbpool)
|
||||
|
||||
// Test the connection
|
||||
|
|
@ -51,12 +58,20 @@ func main() {
|
|||
mux.HandleFunc("/upload_opml", UploadOpmlHandler)
|
||||
|
||||
// Create server
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8000"
|
||||
}
|
||||
host := os.Getenv("HOST")
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
server := &http.Server{
|
||||
Addr: "127.0.0.1:8000",
|
||||
Addr: host + ":" + port,
|
||||
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 {
|
||||
log.Fatalf("ListenAndServe(): %v", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue