mirror of
https://github.com/ssep1ol/overleaf-registration-be.git
synced 2026-07-21 20:40:21 +00:00
First commit
This commit is contained in:
@@ -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
28
main.go
@@ -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))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user