fixed
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
GO_ENV=development
|
||||
DASHBOARD_URL=http://localhost:3000
|
||||
JWT_SECRET_KEY=test-secret-key
|
||||
GOOGLE_CLIENT_ID=483085235984-jng6gvp1r4olkb6jap8sct98bgh36b9k.apps.googleusercontent.com
|
||||
GOOGLE_CLIENT_SECRET=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
|
||||
BACKEND_URL=http://localhost:8080
|
||||
@@ -1 +1,2 @@
|
||||
*.env
|
||||
tmp/
|
||||
@@ -8,20 +8,13 @@ import (
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// DB is the global database connection pool
|
||||
var DB *sql.DB
|
||||
|
||||
func InitDB() (*sql.DB, error) {
|
||||
// Load environment variables from .env file
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading .env file: %v", err)
|
||||
}
|
||||
|
||||
// Get connection details from environment variables
|
||||
// Get connection details from environment variables (loaded in main)
|
||||
connStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true",
|
||||
os.Getenv("DB_USER"),
|
||||
os.Getenv("DB_PASSWORD"),
|
||||
@@ -31,6 +24,7 @@ func InitDB() (*sql.DB, error) {
|
||||
)
|
||||
|
||||
// Open the database connection
|
||||
var err error
|
||||
DB, err = sql.Open("mysql", connStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening database: %v", err)
|
||||
|
||||
+24
-10
@@ -20,8 +20,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
)
|
||||
@@ -32,17 +32,33 @@ var DashboardBaseURL string
|
||||
|
||||
// init initializes the Google OAuth2 configuration by loading environment variables
|
||||
// from a .env file. If the .env file cannot be loaded, it logs a fatal error.
|
||||
// Note: This init runs AFTER .env is loaded in main() init
|
||||
// But we need to load .env here too since init order is package-based
|
||||
func init() {
|
||||
cwd, _ := os.Getwd()
|
||||
log.Printf("[google_auth.init] Current working directory: %s", cwd)
|
||||
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
helper.LogError(err, "Error loading .env file")
|
||||
log.Fatalf("Error loading .env file: %v", err)
|
||||
log.Printf("[google_auth.init] Failed to load .env: %v, trying .env explicitly", err)
|
||||
err = godotenv.Load(".env")
|
||||
if err != nil {
|
||||
log.Printf("[google_auth.init] Failed to load .env explicitly: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
clientID := os.Getenv("GOOGLE_CLIENT_ID")
|
||||
clientSecret := os.Getenv("GOOGLE_CLIENT_SECRET")
|
||||
backendURL := os.Getenv("BACKEND_URL")
|
||||
|
||||
log.Printf("[google_auth.init] GOOGLE_CLIENT_ID: '%s' (length: %d)", clientID, len(clientID))
|
||||
log.Printf("[google_auth.init] GOOGLE_CLIENT_SECRET: '%s' (length: %d)", clientSecret, len(clientSecret))
|
||||
log.Printf("[google_auth.init] BACKEND_URL: '%s'", backendURL)
|
||||
|
||||
googleOauthConfig = oauth2.Config{
|
||||
ClientID: os.Getenv("GOOGLE_CLIENT_ID"),
|
||||
ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
|
||||
RedirectURL: fmt.Sprintf("%s/v1/auth/callback", os.Getenv("BACKEND_URL")),
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
RedirectURL: fmt.Sprintf("%s/v1/auth/callback", backendURL),
|
||||
Scopes: []string{
|
||||
"https://www.googleapis.com/auth/userinfo.email",
|
||||
"https://www.googleapis.com/auth/userinfo.profile",
|
||||
@@ -51,13 +67,11 @@ func init() {
|
||||
}
|
||||
|
||||
if googleOauthConfig.ClientID == "" {
|
||||
helper.LogError(errors.New("GOOGLE_CLIENT_ID is not set"), "GOOGLE_CLIENT_ID is not set in environment variables")
|
||||
log.Fatalf("GOOGLE_CLIENT_ID is not set in environment variables")
|
||||
log.Fatal("GOOGLE_CLIENT_ID is not set in environment variables")
|
||||
}
|
||||
|
||||
if googleOauthConfig.ClientSecret == "" {
|
||||
helper.LogError(errors.New("GOOGLE_CLIENT_SECRET is not set"), "GOOGLE_CLIENT_SECRET is not set in environment variables")
|
||||
log.Fatalf("GOOGLE_CLIENT_SECRET is not set in environment variables")
|
||||
log.Fatal("GOOGLE_CLIENT_SECRET is not set in environment variables")
|
||||
}
|
||||
|
||||
DashboardBaseURL = os.Getenv("DASHBOARD_URL")
|
||||
|
||||
+2
-9
@@ -15,7 +15,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var jwtSecretKey []byte
|
||||
@@ -23,15 +22,11 @@ var jwtSecretKey []byte
|
||||
// init initializes the JWT secret key by loading environment variables from a .env file.
|
||||
// If the .env file cannot be loaded, it logs an error message.
|
||||
// If the JWT_SECRET_KEY is not set in the .env file, it logs a warning message.
|
||||
// Note: .env file is loaded in main() before this init runs
|
||||
func init() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
helper.LogError(err, "Error loading .env file")
|
||||
}
|
||||
|
||||
jwtSecretKey = []byte(os.Getenv("JWT_SECRET_KEY"))
|
||||
if len(jwtSecretKey) == 0 {
|
||||
helper.LogError(nil, "JWT_SECRET_KEY not set in .env file")
|
||||
log.Fatal("JWT_SECRET_KEY not set in environment variables")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -814,5 +809,3 @@ func CheckEmailInDB(email string) (bool, error) {
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/rs/cors"
|
||||
@@ -152,8 +153,28 @@ func allowOnlyGrafana(next http.Handler, allowedIP string) http.Handler {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Initialize Sentry
|
||||
// Load environment variables from .env file first
|
||||
// Get current working directory for debugging
|
||||
cwd, _ := os.Getwd()
|
||||
log.Printf("Current working directory: %s", cwd)
|
||||
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Failed to load .env file from default location: %v", err)
|
||||
// Try with explicit path
|
||||
err = godotenv.Load(".env")
|
||||
if err != nil {
|
||||
log.Fatalf("FATAL: Could not load .env file: %v. Tried paths: default and ./.env", err)
|
||||
}
|
||||
log.Println(".env file loaded successfully from ./.env")
|
||||
} else {
|
||||
log.Println(".env file loaded successfully")
|
||||
}
|
||||
|
||||
// Verify GO_ENV is loaded
|
||||
goEnv := os.Getenv("GO_ENV")
|
||||
log.Printf("GO_ENV value after loading .env: '%s'", goEnv)
|
||||
|
||||
if goEnv == "" {
|
||||
log.Fatal("GO_ENV is not set in main. Please set the GO_ENV environment variable.")
|
||||
}
|
||||
@@ -163,7 +184,7 @@ func main() {
|
||||
log.Fatal("Sentry DSN is not set. Please set the DSN environment variable.")
|
||||
}
|
||||
|
||||
err := sentry.Init(sentry.ClientOptions{
|
||||
err = sentry.Init(sentry.ClientOptions{
|
||||
Dsn: os.Getenv("DSN"),
|
||||
TracesSampleRate: 1.0,
|
||||
Environment: goEnv,
|
||||
@@ -223,4 +244,3 @@ func main() {
|
||||
}
|
||||
log.Fatal(server.ListenAndServe())
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"authentication/redisclient"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -26,13 +25,6 @@ var (
|
||||
Mu sync.Mutex
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
helper.LogWarn("Warning: Could not load .env file, using system environment variables.")
|
||||
}
|
||||
}
|
||||
|
||||
func JWTMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
|
||||
@@ -8,14 +8,13 @@ import (
|
||||
func InsertAccessLogLogin(log models.UserAccessLog) error {
|
||||
query := `INSERT INTO access_log (
|
||||
user_id,
|
||||
participant_id,
|
||||
activity_type,
|
||||
ip_address,
|
||||
field_updated,
|
||||
time)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`
|
||||
VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
_, err := db.DB.Exec(query, log.UserID, log.ParticipantID, log.ActivityType, log.IPAddress, log.FieldUpdated, log.Time)
|
||||
_, err := db.DB.Exec(query, log.UserID, log.ActivityType, log.IPAddress, log.FieldUpdated, log.Time)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
func GetUser(email string) (string, *string, *string, string, error) {
|
||||
log.Print(email)
|
||||
query := `SELECT id, first_name, last_name, email_address FROM users WHERE email_address = ? AND is_deleted = 0 LIMIT 1;`
|
||||
query := `SELECT user_id, first_name, last_name, email_address FROM users WHERE email_address = ? AND is_deleted = 0 LIMIT 1;`
|
||||
var id string
|
||||
var firstName *string
|
||||
var lastName *string
|
||||
@@ -21,7 +21,7 @@ func GetUser(email string) (string, *string, *string, string, error) {
|
||||
|
||||
func GetUserID(email string) (string, error) {
|
||||
log.Print(email)
|
||||
query := `SELECT id, FROM users WHERE email_address = ? AND is_deleted = 0 LIMIT 1;`
|
||||
query := `SELECT user_id, FROM users WHERE email_address = ? AND is_deleted = 0 LIMIT 1;`
|
||||
var id string
|
||||
err := db.DB.QueryRow(query, email).Scan(&id)
|
||||
if err != nil {
|
||||
@@ -43,9 +43,9 @@ func CheckEmailInDB(email string) (bool, error) {
|
||||
|
||||
func GetUserIDFromEmail(email string) (string, error) {
|
||||
log.Print(email)
|
||||
query := `SELECT id
|
||||
query := `SELECT user_id
|
||||
FROM (
|
||||
SELECT id, 1 AS priority
|
||||
SELECT user_id, 1 AS priority
|
||||
FROM users
|
||||
WHERE email_address = ?
|
||||
AND is_deleted = 0
|
||||
|
||||
Reference in New Issue
Block a user