First commit

This commit is contained in:
corrado.mulas
2024-11-20 00:51:56 +01:00
parent 68de7bf0b1
commit d5b34a9f82
2 changed files with 25 additions and 12 deletions

View File

@@ -3,12 +3,9 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"github.com/joho/godotenv"
) )
// RegistrationRequest represents the incoming registration request. // RegistrationRequest represents the incoming registration request.
@@ -112,9 +109,3 @@ func validateEnvVars(vars []string) error {
} }
return nil return nil
} }
func loadEnv() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found, using system environment variables")
}
}

28
main.go
View File

@@ -6,6 +6,8 @@ import (
"os" "os"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/rs/cors"
) )
func loadEnv() { func loadEnv() {
@@ -18,13 +20,33 @@ func main() {
// Load environment variables // Load environment variables
loadEnv() loadEnv()
http.HandleFunc("/register", registerHandler) // Validate required environment variables
requiredVars := []string{"ALLOWED_DOMAINS", "CAPTCHA_SERVER_KEY", "OL_INSTANCE", "OL_ADMIN_EMAIL", "OL_ADMIN_PASSWORD"}
if err := validateEnvVars(requiredVars); err != nil {
log.Fatalf("Environment validation failed: %v", err)
}
// Set up HTTP routes
mux := http.NewServeMux()
mux.HandleFunc("/register", registerHandler)
// Apply CORS middleware
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // Replace "*" with specific frontend origins, if needed
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
AllowedHeaders: []string{"Content-Type", "Authorization"},
AllowCredentials: true,
})
// Wrap your handler with CORS middleware
handler := c.Handler(mux)
// Start the server
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port == "" { if port == "" {
port = "3000" // Default to port 3000 port = "3000"
} }
log.Printf("Starting server on port %s...", port) log.Printf("Starting server on port %s...", port)
log.Fatal(http.ListenAndServe(":"+port, nil)) log.Fatal(http.ListenAndServe(":"+port, handler))
} }