updated for using .env
This commit is contained in:
@@ -4,18 +4,18 @@ import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/joho/godotenv"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/patel-mann/poll-system/app/internal/models"
|
||||
"github.com/patel-mann/poll-system/app/internal/utils"
|
||||
)
|
||||
|
||||
var jwtKey = []byte("your-secret-key") //TODO: Move to env/config
|
||||
|
||||
// Helper function to get redirect URL based on role
|
||||
func getDefaultRedirectURL(role int) string {
|
||||
switch role {
|
||||
@@ -49,6 +49,18 @@ func renderRegisterError(w http.ResponseWriter, errorMsg string) {
|
||||
|
||||
// Helper function to create and sign JWT token
|
||||
func createJWTToken(userID, role int) (string, time.Time, error) {
|
||||
|
||||
err := godotenv.Load() // or specify path: godotenv.Load("/path/to/.env")
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading .env file: %v", err)
|
||||
}
|
||||
|
||||
// Get individual components from environment variables
|
||||
jwtSecret := os.Getenv("JWT_SECRET")
|
||||
|
||||
var jwtKey = []byte(jwtSecret) //TODO: Move to env/config
|
||||
|
||||
|
||||
expirationTime := time.Now().Add(12 * time.Hour)
|
||||
claims := &models.Claims{
|
||||
UserID: userID,
|
||||
|
||||
@@ -4,24 +4,40 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var DB *sql.DB
|
||||
|
||||
func InitDB() {
|
||||
var err error
|
||||
// Load .env file
|
||||
err := godotenv.Load() // or specify path: godotenv.Load("/path/to/.env")
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading .env file: %v", err)
|
||||
}
|
||||
|
||||
// Example DSN format for PostgreSQL:
|
||||
// "postgres://username:password@host:port/dbname?sslmode=disable"
|
||||
dsn := "postgres://mannpatel:Admin@localhost:5432/poll_database?sslmode=disable"
|
||||
// Get individual components from environment variables
|
||||
host := os.Getenv("DB_HOST")
|
||||
port := os.Getenv("DB_PORT")
|
||||
user := os.Getenv("DB_USER")
|
||||
password := os.Getenv("DB_PASSWORD")
|
||||
dbname := os.Getenv("DB_NAME")
|
||||
sslmode := os.Getenv("DB_SSLMODE") // optional, default "disable"
|
||||
|
||||
// Build connection string
|
||||
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
|
||||
host, port, user, password, dbname, sslmode)
|
||||
|
||||
// Connect to DB
|
||||
DB, err = sql.Open("postgres", dsn)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to DB: %v", err)
|
||||
}
|
||||
|
||||
// Test connection
|
||||
err = DB.Ping()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to ping DB: %v", err)
|
||||
|
||||
@@ -5,8 +5,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var jwtKey = []byte("your-secret-key") //TODO: Move to env/config
|
||||
|
||||
func GetCurrentUserID(w http.ResponseWriter, r *http.Request)(int){
|
||||
currentUserID := r.Context().Value("user_id").(int)
|
||||
return currentUserID
|
||||
|
||||
@@ -32,6 +32,9 @@
|
||||
<div class="w-9 h-9 bg-blue-500 rounded-full flex items-center justify-center text-white font-semibold">
|
||||
{{slice .UserName 0 1}}
|
||||
</div>
|
||||
<a href="/logout" class="p-2 hover:bg-gray-200 rounded">
|
||||
<i class="fas fa-external-link-alt text-gray-500"></i>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -48,15 +51,14 @@
|
||||
<!-- Right Side: User Info -->
|
||||
<div class="flex items-center gap-3">
|
||||
<span class="text-sm font-medium text-gray-600">Welcome, {{.UserName}}</span>
|
||||
<div class="w-9 h-9 bg-blue-500 rounded-full flex items-center justify-center text-white font-semibold">
|
||||
{{slice .UserName 0 1}}
|
||||
</div>
|
||||
<div class="w-9 h-9 bg-blue-500 rounded-full flex items-center justify-center text-white font-semibold">
|
||||
{{slice .UserName 0 1}}
|
||||
</div>
|
||||
<a href="/logout" class="p-2 hover:bg-gray-200 rounded">
|
||||
<i class="fas fa-external-link-alt text-gray-500"></i>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
<div class="flex h-full">
|
||||
<!-- Mobile Sidebar Overlay -->
|
||||
<div x-show="sidebarOpen"
|
||||
@@ -162,13 +164,6 @@
|
||||
<i class="fas fa-user-circle text-gray-400 mr-2"></i>
|
||||
<span>Profile</span>
|
||||
</a>
|
||||
|
||||
<a href="/logout"
|
||||
@click="sidebarOpen = false"
|
||||
class="flex items-center text-sm text-gray-600 hover:bg-gray-100 rounded px-2 py-1 {{if eq .ActiveSection "profile"}}bg-gray-100{{end}}">
|
||||
<i class="fas fa-sign-out-alt text-gray-400 mr-2"></i>
|
||||
<span>Logout</span>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -248,7 +243,7 @@
|
||||
</nav>
|
||||
|
||||
<!-- Hero Section -->
|
||||
<section id="home" class="max-w-4xl mx-auto px-4 pt-32 pb-32 text-center">
|
||||
<section id="home" class="max-w-4xl mx-auto h-[600px] px-4 pt-[200px] pb-32 text-center">
|
||||
<h1 class="text-3xl sm:text-4xl lg:text-5xl font-bold text-gray-900 mb-6 leading-tight">
|
||||
Streamline Your<br>
|
||||
<span class="text-blue-600">Polling Operations</span>
|
||||
|
||||
@@ -2,9 +2,14 @@ package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// Helper functions for templates
|
||||
@@ -44,9 +49,19 @@ var templateFuncs = template.FuncMap{
|
||||
}
|
||||
|
||||
func Render(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||
// Load .env file
|
||||
err := godotenv.Load() // or specify path: godotenv.Load("/path/to/.env")
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading .env file: %v", err)
|
||||
}
|
||||
|
||||
// Get individual components from environment variables
|
||||
TemplatesURL := os.Getenv("TEMPLATES_URL")
|
||||
fmt.Print(TemplatesURL)
|
||||
|
||||
// Paths for layout + page templates
|
||||
layout := filepath.Join("/Users/mannpatel/Desktop/Poll-system/app/internal/templates/", "layout.html")
|
||||
page := filepath.Join("/Users/mannpatel/Desktop/Poll-system/app/internal/templates/", tmpl)
|
||||
layout := filepath.Join(TemplatesURL, "layout.html")
|
||||
page := filepath.Join(TemplatesURL, tmpl)
|
||||
|
||||
// Parse files with helper functions
|
||||
tmpls, err := template.New("").Funcs(templateFuncs).ParseFiles(layout, page)
|
||||
|
||||
Reference in New Issue
Block a user