diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..061d467 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +# Variables +BACKEND_DIR=backend +BACKEND_BINARY=backend + +# Règles +all: build-backend start-backend + +build-backend: + @echo "Building backend..." + cd $(BACKEND_DIR) && go build -o $(BACKEND_BINARY) + +start-backend: + @echo "Starting backend..." + cd $(BACKEND_DIR) && ./$(BACKEND_BINARY) + +.PHONY: all build-backend start-backend \ No newline at end of file diff --git a/backend/main.go b/backend/main.go index 741aca9..4bb7e00 100644 --- a/backend/main.go +++ b/backend/main.go @@ -16,26 +16,18 @@ type LinkAndDate struct { } func getDayAndLink(c *gin.Context) { - //Charge le fichier .env err := godotenv.Load() if err != nil { log.Fatalf("Error loading .env file") } - //Récupération du chemin du fichier contenant les dates et les liens depuis le .env + fileName := os.Getenv("FILE_NAME") if fileName == "" { log.Fatalf("FILE_NAME not set in .env") } - //Récupération de la date du jour date := time.Now().Format("02-01-2006") - //Lis le fichier de contenant les dates et les liens (date_link.txt par exemple) à son chemin spécifié dans le .env - //Si la date du jour est présente dans le fichier, renvoie le lien associé - //Le fichier doit être sous la forme suivante : - //01-12-2024;https://instagram.com/appen_isen - //Sinon, renvoie un lien par défaut - //Le lien par défault est https://instagram.com/appen_isen file, err := os.Open(fileName) if err != nil { log.Fatalf("Error opening file: %v", err) @@ -43,11 +35,10 @@ func getDayAndLink(c *gin.Context) { defer func(file *os.File) { err := file.Close() if err != nil { - + log.Fatalf("Error closing file: %v", err) } }(file) - //Crée une carte pour stocker les dates et les liens links := make(map[string]string) scanner := bufio.NewScanner(file) for scanner.Scan() { @@ -61,7 +52,6 @@ func getDayAndLink(c *gin.Context) { log.Fatalf("Error reading file: %v", err) } - //Cherche le lien associé à la date du jour link, exists := links[date] if !exists { link = "https://instagram.com/appen_isen" @@ -74,11 +64,19 @@ func main() { gin.SetMode(gin.ReleaseMode) router := gin.Default() - //Définition de la route /get_day avec la méthode GET et la fonction getDayAndLink qui renvoie le jour sous la forme d'un string (14-12-2024) et un lien sous la forme d'un string (https://www.google.com) + // Serve static files from the frontend directory + router.Static("/static", "./frontend") + + // API route router.GET("/api/get_day", getDayAndLink) + // Serve the index.html file + router.NoRoute(func(c *gin.Context) { + c.File("../frontend/index.html") + }) + err := router.Run(":8080") if err != nil { - return + log.Fatalf("Error starting server: %v", err) } } diff --git a/frontend/index.html b/frontend/index.html new file mode 100644 index 0000000..ffd019e --- /dev/null +++ b/frontend/index.html @@ -0,0 +1,10 @@ + + +
+ +