From c732fdec46b2bb5cf207b66b17a742b4ae418110 Mon Sep 17 00:00:00 2001 From: pro100ton Date: Sat, 2 Nov 2024 14:09:44 +0300 Subject: [PATCH] Initial migration --- .air.toml | 46 +++++++++++++++++++++++++ .gitignore | 25 ++++++++++++++ Dockerfile | 11 ++++++ README.md | 27 +++++++++++++++ cmd/main.go | 24 +++++++++++++ compose.yaml | 17 ++++++++++ go.mod | 21 ++++++++++++ go.sum | 33 ++++++++++++++++++ models/exerciseModels.go | 18 ++++++++++ routes/routes.go | 16 +++++++++ services/exercises.go | 51 ++++++++++++++++++++++++++++ services/sandbox.go | 27 +++++++++++++++ utils/exercise_generator.go | 45 ++++++++++++++++++++++++ views/exercises/exercises.html | 25 ++++++++++++++ views/forms/exercise_enter_form.html | 23 +++++++++++++ views/index.html | 34 +++++++++++++++++++ views/training/equipment.html | 4 +++ views/training/training_table.html | 15 ++++++++ 18 files changed, 462 insertions(+) create mode 100644 .air.toml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 cmd/main.go create mode 100644 compose.yaml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 models/exerciseModels.go create mode 100644 routes/routes.go create mode 100644 services/exercises.go create mode 100644 services/sandbox.go create mode 100644 utils/exercise_generator.go create mode 100644 views/exercises/exercises.html create mode 100644 views/forms/exercise_enter_form.html create mode 100644 views/index.html create mode 100644 views/training/equipment.html create mode 100644 views/training/training_table.html diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..4c5b156 --- /dev/null +++ b/.air.toml @@ -0,0 +1,46 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./tmp/main" + cmd = "go build -o ./tmp/main ./cmd/main.go" + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + post_cmd = [] + pre_cmd = [] + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = false + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + time = false + +[misc] + clean_on_exit = false + +[screen] + clear_on_rebuild = false + keep_scroll = true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..35c6692 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# ---> 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 +.eslintrc.js +tmp/* +tmp diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..504ac01 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.23-alpine + +WORKDIR /app +RUN go install github.com/air-verse/air@latest + +COPY go.mod go.sum ./ + +RUN go mod download && go mod verify + +CMD ["air", "-c", ".air.toml"] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..fc5fde9 --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# go_templates + +Go templates playground project + +# Описание + +Данный проект использует фреймворк fiber + стандартные темплейты go - "html" + +# Полезные команды + +Сборка проекта + +``` +docker compose build +``` + +Сборка без кэша + +``` +docker compose build --no-cache +``` + +Чтобы провалиться во внутрь контейнера + +``` +docker exec -ti sh +``` diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..44251b8 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/gofiber/fiber/v2" + "github.com/gofiber/template/html/v2" + + "git.pro100code.ru/pro100ton/go_templates/routes" +) + +type TrainingEquipment struct { + Name []string +} + +func main() { + engine := html.New("./views", ".html") + + app := fiber.New(fiber.Config{ + Views: engine, + }) + + routes.RegisterRoutes(app) + + app.Listen(":3000") +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..aa1fc59 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,17 @@ +services: + fiber: + build: + context: . + dockerfile: Dockerfile + ports: + - target: 3000 + published: 3001 + networks: + - fiber-net + volumes: + - type: bind + source: . + target: /app + +networks: + fiber-net: diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ebb94ca --- /dev/null +++ b/go.mod @@ -0,0 +1,21 @@ +module git.pro100code.ru/pro100ton/go_templates + +go 1.22.5 + +require ( + github.com/andybalholm/brotli v1.0.5 // indirect + github.com/gofiber/fiber/v2 v2.52.5 // indirect + github.com/gofiber/template v1.8.3 // indirect + github.com/gofiber/template/html/v2 v2.1.2 // indirect + github.com/gofiber/utils v1.1.0 // indirect + github.com/google/uuid v1.5.0 // indirect + github.com/klauspost/compress v1.17.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.51.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.15.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c531f6c --- /dev/null +++ b/go.sum @@ -0,0 +1,33 @@ +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= +github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= +github.com/gofiber/template v1.8.3 h1:hzHdvMwMo/T2kouz2pPCA0zGiLCeMnoGsQZBTSYgZxc= +github.com/gofiber/template v1.8.3/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8= +github.com/gofiber/template/html/v2 v2.1.2 h1:wkK/mYJ3nIhongTkG3t0QgV4ADdgOYJYVSAF2AHnh8Y= +github.com/gofiber/template/html/v2 v2.1.2/go.mod h1:E98Z/FzvpaSib06aWEgYk6GXNf3ctoyaJH8yW5ay5ak= +github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM= +github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= +github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +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/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +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.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= +github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/models/exerciseModels.go b/models/exerciseModels.go new file mode 100644 index 0000000..cddea3d --- /dev/null +++ b/models/exerciseModels.go @@ -0,0 +1,18 @@ +package models + +// TODO: Create actual models instead of simple structs +// TODO: Add correct types +type ExerciseSandbox struct { + Name string + Weight string + Reps string +} + +type ExerciseAttempt struct { + Weight float32 + Reps int32 +} + +type Exercise struct { + Name string +} diff --git a/routes/routes.go b/routes/routes.go new file mode 100644 index 0000000..b098bb7 --- /dev/null +++ b/routes/routes.go @@ -0,0 +1,16 @@ +package routes + +import ( + "github.com/gofiber/fiber/v2" + + "git.pro100code.ru/pro100ton/go_templates/services" +) + +func RegisterRoutes(app *fiber.App) { + app.Get("/", services.TrainingItemsRenderer) + app.Get("/training/equipment", services.TrainingEquipmentJSONGetter) + app.Get("/training/equipment/render", services.TrainingEquipmentRenderer) + // app.Get("/exercises/render", services.ExercisesRenderer) + app.Get("/exercises/form", services.ExercisesFormRenderer) + app.Post("/exercises/form/submit", services.ExercisesFormHanlder) +} diff --git a/services/exercises.go b/services/exercises.go new file mode 100644 index 0000000..d68b4d2 --- /dev/null +++ b/services/exercises.go @@ -0,0 +1,51 @@ +package services + +import ( + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/log" + + "git.pro100code.ru/pro100ton/go_templates/models" + // "git.pro100code.ru/pro100ton/go_templates/utils" +) + +var exercise []models.ExerciseSandbox + +// func ExercisesJSONGetter(c *fiber.Ctx) error { +// exercies := utils.GenerateExercises() +// return c.JSON(exercies) +// } +// +// func ExercisesRenderer(c *fiber.Ctx) error { +// exercies := utils.GenerateExercises() +// return c.Render("exercises/exercises", fiber.Map{ +// "data": exercies, +// }) +// } + +func ExercisesFormRenderer(c *fiber.Ctx) error { + return c.Render("forms/exercise_enter_form", nil) +} + +func ExercisesFormHanlder(c *fiber.Ctx) error { + log.Info(c.FormValue("exercise-name")) + log.Info(c.FormValue("attempt-weight-name")) + log.Info(c.FormValue("attempt-reps-name")) + exerciseName := c.FormValue("exercise-name") + attemptWeight := c.FormValue("attempt-weight-name") + attemptReps := c.FormValue("attempt-reps-name") + log.Info(exerciseName, attemptWeight, attemptReps) + exercise = append( + exercise, + models.ExerciseSandbox{ + Name: exerciseName, + + Weight: attemptWeight, + Reps: attemptReps, + }, + ) + log.Info("---") + log.Info(exercise) + return c.Render("training/training_table", fiber.Map{ + "data": exercise, + }) +} diff --git a/services/sandbox.go b/services/sandbox.go new file mode 100644 index 0000000..8177820 --- /dev/null +++ b/services/sandbox.go @@ -0,0 +1,27 @@ +package services + +import "github.com/gofiber/fiber/v2" + +type TrainingEquipment struct { + Name []string +} + +func TrainingItemsRenderer(c *fiber.Ctx) error { + trainDevices := [3]string{"Гравитроныч", "Тяга горизонтального блока", "Тяга вертикального блока"} + return c.Render("index", fiber.Map{ + "TrainingItems": trainDevices, + }) +} + +func TrainingEquipmentJSONGetter(c *fiber.Ctx) error { + equipment_data := TrainingEquipment{ + Name: []string{"Гравитроныч", "Тяга вертикального блока", "Тяга горизонтального блока"}, + } + return c.JSON(equipment_data) +} + +func TrainingEquipmentRenderer(c *fiber.Ctx) error { + return c.Render("training/equipment", fiber.Map{ + "anchor": "some kek data", + }) +} diff --git a/utils/exercise_generator.go b/utils/exercise_generator.go new file mode 100644 index 0000000..14018b9 --- /dev/null +++ b/utils/exercise_generator.go @@ -0,0 +1,45 @@ +// package utils +// +// import "git.pro100code.ru/pro100ton/go_templates/models" +// +// func GenerateExercises() []models.ExerciseSandbox { +// exercises := []models.ExerciseSandbox{ +// { +// Name: "Гравитрон", +// Weight: 30, +// Reps: 12, +// }, +// { +// Name: "Гравитрон", +// Weight: 10, +// Reps: 10, +// }, +// { +// Name: "Гравитрон", +// Weight: 10, +// Reps: 10, +// }, +// { +// Name: "Жим гантели в наклоне", +// Weight: 28, +// Reps: 12, +// }, +// { +// Name: "Жим гантели в наклоне", +// Weight: 28, +// Reps: 10, +// }, +// { +// Name: "Жим гантели в наклоне", +// Weight: 26, +// Reps: 10, +// }, +// { +// Name: "Жим гантели в наклонe", +// Weight: 26, +// Reps: 8, +// }, +// } +// +// return exercises +// } diff --git a/views/exercises/exercises.html b/views/exercises/exercises.html new file mode 100644 index 0000000..83a8b5c --- /dev/null +++ b/views/exercises/exercises.html @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + {{range .data}} + + + + + + {{end}} +
ИмяВесПовторения
{{.Name}}{{.Weight}}{{.Reps}}
+ + diff --git a/views/forms/exercise_enter_form.html b/views/forms/exercise_enter_form.html new file mode 100644 index 0000000..8601c0d --- /dev/null +++ b/views/forms/exercise_enter_form.html @@ -0,0 +1,23 @@ + + + + + + + + Exercise form + + +

Please, enter exercise details

+
+
+
+
+
+
+
+ +
+
+ + diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..5689879 --- /dev/null +++ b/views/index.html @@ -0,0 +1,34 @@ + + + + + + + + + + + Some fancy template index page + + +

Some Hello {{ .Username}}!!

+ +
+ + +
+ +
+ + + diff --git a/views/training/equipment.html b/views/training/equipment.html new file mode 100644 index 0000000..7e6543b --- /dev/null +++ b/views/training/equipment.html @@ -0,0 +1,4 @@ +

1

+

2

+

{{ .anchor }}

+

4

diff --git a/views/training/training_table.html b/views/training/training_table.html new file mode 100644 index 0000000..09f1cb5 --- /dev/null +++ b/views/training/training_table.html @@ -0,0 +1,15 @@ + + + + + + + {{range .data}} + + + + + + {{end}} + +
Имя упражненияВесПовторения
{{.Name}}{{.Weight}}{{.Reps}}