From dbfd1c0248e15ccc4a85014147b58a27704bff1c Mon Sep 17 00:00:00 2001
From: rick <rick@gnous.eu>
Date: Sun, 16 Mar 2025 21:07:46 +0100
Subject: [PATCH] first commit

add backend, init script for SQL
---
 .gitignore         |  66 +++++++++++++++++++
 back/go.mod        |  27 ++++++++
 back/go.sum        |  44 +++++++++++++
 back/main.go       | 158 +++++++++++++++++++++++++++++++++++++++++++++
 docker-compose.yml |  15 +++++
 sql/init.sql       |  22 +++++++
 6 files changed, 332 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 back/go.mod
 create mode 100644 back/go.sum
 create mode 100644 back/main.go
 create mode 100644 docker-compose.yml
 create mode 100644 sql/init.sql

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..6d0eabd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,66 @@
+# Created by https://www.toptal.com/developers/gitignore/api/go,vim,vue,vuejs
+# Edit at https://www.toptal.com/developers/gitignore?templates=go,vim,vue,vuejs
+
+### Go ###
+# If you prefer the allow list template instead of the deny list, see community template:
+# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
+#
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, built with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+# Dependency directories (remove the comment below to include it)
+# vendor/
+
+# Go workspace file
+go.work
+
+### Vim ###
+# Swap
+[._]*.s[a-v][a-z]
+!*.svg  # comment out if you don't need vector files
+[._]*.sw[a-p]
+[._]s[a-rt-v][a-z]
+[._]ss[a-gi-z]
+[._]sw[a-p]
+
+# Session
+Session.vim
+Sessionx.vim
+
+# Temporary
+.netrwhist
+*~
+# Auto-generated tag files
+tags
+# Persistent undo
+[._]*.un~
+
+### Vue ###
+# gitignore template for Vue.js projects
+# Recommended template: Node.gitignore
+
+# TODO: where does this rule come from?
+docs/_book
+
+# TODO: where does this rule come from?
+test/
+
+### Vuejs ###
+# Recommended template: Node.gitignore
+
+node_modules/
+dist/
+npm-debug.log
+yarn-error.log
+
+# End of https://www.toptal.com/developers/gitignore/api/go,vim,vue,vuejs
diff --git a/back/go.mod b/back/go.mod
new file mode 100644
index 0000000..172b73e
--- /dev/null
+++ b/back/go.mod
@@ -0,0 +1,27 @@
+module git.gnous.eu/Rick/bdd_wankil
+
+go 1.24.0
+
+require (
+	filippo.io/edwards25519 v1.1.0 // indirect
+	github.com/andybalholm/brotli v1.1.1 // indirect
+	github.com/fxamacker/cbor/v2 v2.7.0 // indirect
+	github.com/go-sql-driver/mysql v1.9.0 // indirect
+	github.com/gofiber/fiber/v3 v3.0.0-beta.4 // indirect
+	github.com/gofiber/schema v1.3.0 // indirect
+	github.com/gofiber/utils/v2 v2.0.0-beta.7 // indirect
+	github.com/google/uuid v1.6.0 // indirect
+	github.com/klauspost/compress v1.18.0 // indirect
+	github.com/mattn/go-colorable v0.1.14 // indirect
+	github.com/mattn/go-isatty v0.0.20 // indirect
+	github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
+	github.com/tinylib/msgp v1.2.5 // indirect
+	github.com/valyala/bytebufferpool v1.0.0 // indirect
+	github.com/valyala/fasthttp v1.59.0 // indirect
+	github.com/valyala/tcplisten v1.0.0 // indirect
+	github.com/x448/float16 v0.8.4 // indirect
+	golang.org/x/crypto v0.36.0 // indirect
+	golang.org/x/net v0.37.0 // indirect
+	golang.org/x/sys v0.31.0 // indirect
+	golang.org/x/text v0.23.0 // indirect
+)
diff --git a/back/go.sum b/back/go.sum
new file mode 100644
index 0000000..d5d6f65
--- /dev/null
+++ b/back/go.sum
@@ -0,0 +1,44 @@
+filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
+filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
+github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
+github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
+github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
+github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
+github.com/go-sql-driver/mysql v1.9.0 h1:Y0zIbQXhQKmQgTp44Y1dp3wTXcn804QoTptLZT1vtvo=
+github.com/go-sql-driver/mysql v1.9.0/go.mod h1:pDetrLJeA3oMujJuvXc8RJoasr589B6A9fwzD3QMrqw=
+github.com/gofiber/fiber/v3 v3.0.0-beta.4 h1:KzDSavvhG7m81NIsmnu5l3ZDbVS4feCidl4xlIfu6V0=
+github.com/gofiber/fiber/v3 v3.0.0-beta.4/go.mod h1:/WFUoHRkZEsGHyy2+fYcdqi109IVOFbVwxv1n1RU+kk=
+github.com/gofiber/schema v1.3.0 h1:K3F3wYzAY+aivfCCEHPufCthu5/13r/lzp1nuk6mr3Q=
+github.com/gofiber/schema v1.3.0/go.mod h1:YYwj01w3hVfaNjhtJzaqetymL56VW642YS3qZPhuE6c=
+github.com/gofiber/utils/v2 v2.0.0-beta.7 h1:NnHFrRHvhrufPABdWajcKZejz9HnCWmT/asoxRsiEbQ=
+github.com/gofiber/utils/v2 v2.0.0-beta.7/go.mod h1:J/M03s+HMdZdvhAeyh76xT72IfVqBzuz/OJkrMa7cwU=
+github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
+github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
+github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
+github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
+github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
+github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
+github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c h1:dAMKvw0MlJT1GshSTtih8C2gDs04w8dReiOGXrGLNoY=
+github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
+github.com/tinylib/msgp v1.2.5 h1:WeQg1whrXRFiZusidTQqzETkRpGjFjcIhW6uqWH09po=
+github.com/tinylib/msgp v1.2.5/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0=
+github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
+github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
+github.com/valyala/fasthttp v1.59.0 h1:Qu0qYHfXvPk1mSLNqcFtEk6DpxgA26hy6bmydotDpRI=
+github.com/valyala/fasthttp v1.59.0/go.mod h1:GTxNb9Bc6r2a9D0TWNSPwDz78UxnTGBViY3xZNEqyYU=
+github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
+github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
+github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
+github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
+github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
+golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
+golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
+golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
+golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
+golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
+golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
+golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
+golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
diff --git a/back/main.go b/back/main.go
new file mode 100644
index 0000000..1b4b736
--- /dev/null
+++ b/back/main.go
@@ -0,0 +1,158 @@
+package main
+
+import (
+	"database/sql"
+	"fmt"
+	"log"
+
+	"github.com/go-sql-driver/mysql"
+	"github.com/gofiber/fiber/v3"
+	"github.com/gofiber/fiber/v3/middleware/static"
+)
+
+type Video struct {
+	Id  int64
+	Url string
+}
+
+type Subtitle struct {
+	Id       int64
+	Timecode int64
+	Subtitle string
+	Video    int64
+}
+
+type Response struct {
+	Video    string
+	Timecode []int64
+}
+
+var db *sql.DB
+
+func formatResponse(c fiber.Ctx) error {
+	regex := c.Query("query")
+
+	var ret []Response
+	subtitles := getSubtitles(regex)
+
+	var lastVideoId int64 = -1
+	var currVideoId int64 = -1
+
+	var listTimecode []int64 = nil
+
+	for _, element := range subtitles {
+		currVideoId = element.Video
+
+		//fmt.Println("curr %d et last %d", currVideoId, lastVideoId)
+		if currVideoId != lastVideoId {
+			if lastVideoId != -1 {
+				//fmt.Println("inside")
+				tmpVideo := getVideo(lastVideoId)
+				ret = append(ret, Response{tmpVideo.Url, listTimecode})
+			}
+
+			listTimecode = nil
+			lastVideoId = currVideoId
+		}
+
+		listTimecode = append(listTimecode, element.Timecode)
+		//fmt.Println(listTimecode)
+	}
+
+	tmpVideo := getVideo(lastVideoId)
+	ret = append(ret, Response{tmpVideo.Url, listTimecode})
+
+	//fmt.Println("================")
+	//fmt.Println(ret)
+
+	return c.JSON(ret)
+}
+
+func getVideo(id int64) Video {
+	var ret Video
+	row := db.QueryRow("SELECT * FROM video WHERE id = ?", id)
+
+	//defer row.Close()
+
+	if err := row.Scan(&ret.Id, &ret.Url); err != nil {
+		fmt.Println(err)
+		return ret
+	}
+
+	return ret
+}
+
+func getSubtitles(regex string) []Subtitle {
+	//fmt.Println(regex)
+	var ret []Subtitle
+	//rows, err := db.Query("SELECT id, timecode, video, MATCH (subtitle) AGAINST (?)   FROM subtitle GROUP BY video, timecode, subtitle, id", regex)
+	rows, err := db.Query("SELECT * FROM subtitle WHERE MATCH (subtitle) AGAINST (?) GROUP BY video, id ORDER BY video, timecode", regex)
+	if err != nil {
+		fmt.Println(err)
+		return nil
+	}
+
+	//fmt.Println(rows)
+
+	defer rows.Close()
+
+	for rows.Next() {
+		var subtitle Subtitle
+		if err := rows.Scan(&subtitle.Id, &subtitle.Timecode, &subtitle.Subtitle, &subtitle.Video); err != nil {
+			//if err := rows.Scan(&subtitle.Id, &subtitle.Timecode, &subtitle.Subtitle, &subtitle.Video); err != nil {
+			fmt.Println(err)
+			return nil
+		}
+
+		ret = append(ret, subtitle)
+		//fmt.Println(ret)
+	}
+
+	if err := rows.Err(); err != nil {
+		fmt.Println(err)
+	}
+
+	return ret
+}
+
+func main() {
+	cfg := mysql.Config{
+		//User:   os.Getenv("DBUSER"),
+		//Passwd: os.Getenv("DBPASS"),
+		User:   "test",
+		Passwd: "test",
+		Net:    "tcp",
+		Addr:   "127.0.0.1:3306",
+		DBName: "db",
+	}
+	// Get a database handle.
+	var err error
+	db, err = sql.Open("mysql", cfg.FormatDSN())
+	if err != nil {
+		log.Fatal(err)
+
+	}
+
+	pingErr := db.Ping()
+	if pingErr != nil {
+		log.Fatal(pingErr)
+
+	}
+
+	fmt.Println("Connected!")
+
+	app := fiber.New()
+
+	/*
+		app.Get("/", func(c fiber.Ctx) error {
+			return c.SendString("Hello, World")
+		})
+	*/
+
+	//app.Static("/", "../frontend/.output/public")
+	app.Get("/*", static.New("../front/.output/public"))
+
+	app.Get("/search", formatResponse)
+
+	app.Listen(":8080")
+}
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..a3e21ff
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,15 @@
+services:
+  db:
+    image: mysql:9
+    environment:
+      MYSQL_ROOT_PASSWORD: test
+      MYSQL_USER: test
+      MYSQL_PASSWORD: test
+      MYSQL_DATABASE: db
+    volumes:
+      - mysql:/var/lib/mysql
+    ports:
+      - 3306:3306
+
+volumes:
+  mysql: {}
diff --git a/sql/init.sql b/sql/init.sql
new file mode 100644
index 0000000..c8335fb
--- /dev/null
+++ b/sql/init.sql
@@ -0,0 +1,22 @@
+USE db;
+
+CREATE TABLE `video` (
+	`id` INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
+	`url` VARCHAR(16) UNIQUE,
+	PRIMARY KEY(`id`)
+);
+
+
+CREATE TABLE `subtitle` (
+	`id` INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
+	`timecode` INTEGER,
+	`subtitle` TEXT(65535),
+	`video` INTEGER,
+	PRIMARY KEY(`id`),
+        FULLTEXT INDEX subtext(subtitle)
+);
+
+
+ALTER TABLE `subtitle`
+ADD FOREIGN KEY(`video`) REFERENCES `video`(`id`)
+ON UPDATE NO ACTION ON DELETE NO ACTION;