Провел небольшой рефакторинг кода
This commit is contained in:
parent
bcd81911cd
commit
26bb547c43
3 changed files with 21 additions and 13 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -27,7 +27,7 @@ images/
|
|||
main
|
||||
node_modules
|
||||
dist
|
||||
bun.lockb
|
||||
bun.lock*
|
||||
package-lock.json
|
||||
config.yaml
|
||||
go.sum
|
||||
go.sum
|
||||
|
|
28
main.go
28
main.go
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
|
@ -10,6 +9,7 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
|
@ -19,7 +19,8 @@ import (
|
|||
func GetPicsArray() []string {
|
||||
files, err := os.ReadDir("images")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("Ошибка чтения директории: %v", err)
|
||||
return []string{}
|
||||
}
|
||||
if len(files) == 0 {
|
||||
return []string{"no images"}
|
||||
|
@ -32,7 +33,7 @@ func GetPicsArray() []string {
|
|||
}
|
||||
|
||||
func (conf *ConfHandler) GetRandImgS3() string {
|
||||
picsArray := GetObjectsFilenames(conf.ConfigHandler, conf.minioClient)
|
||||
picsArray := GetObjectsFilenames(conf.ConfigHandler, *conf.minioClient)
|
||||
return picsArray[rand.Intn(len(picsArray))]
|
||||
}
|
||||
|
||||
|
@ -49,12 +50,15 @@ func (conf *ConfHandler) ReturnHTMLS3(w http.ResponseWriter, req *http.Request)
|
|||
return
|
||||
}
|
||||
image := conf.GetRandImgS3()
|
||||
fmt.Println(image)
|
||||
log.Println(image)
|
||||
htmlOpen.Execute(w, image)
|
||||
}
|
||||
|
||||
func GetRandImg() string {
|
||||
picsArray := GetPicsArray()
|
||||
if len(picsArray) == 0 {
|
||||
return ""
|
||||
}
|
||||
return picsArray[rand.Intn(len(picsArray))]
|
||||
}
|
||||
|
||||
|
@ -71,8 +75,11 @@ func ReturnHTML(w http.ResponseWriter, req *http.Request) {
|
|||
return
|
||||
}
|
||||
image := GetRandImg()
|
||||
fmt.Println(image)
|
||||
htmlOpen.Execute(w, image)
|
||||
log.Println(image)
|
||||
if err := htmlOpen.Execute(w, image); err != nil {
|
||||
log.Printf("Ошибка рендеринга HTML: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
|
@ -85,7 +92,7 @@ type Config struct {
|
|||
|
||||
type ConfHandler struct {
|
||||
ConfigHandler Config
|
||||
minioClient minio.Client
|
||||
minioClient *minio.Client
|
||||
}
|
||||
|
||||
func InitConf(filename string) Config {
|
||||
|
@ -107,12 +114,12 @@ func GetObjectsFilenames(conf Config, minioClient minio.Client) []string {
|
|||
|
||||
objectCh := minioClient.ListObjects(ctx, conf.Bucket, minio.ListObjectsOptions{Recursive: false})
|
||||
|
||||
fmt.Println(objectCh)
|
||||
log.Println(objectCh)
|
||||
objectsKeyArr := []string{}
|
||||
|
||||
for object := range objectCh {
|
||||
if object.Err != nil {
|
||||
fmt.Println(object.Err)
|
||||
log.Println(object.Err)
|
||||
}
|
||||
objectsKeyArr = append(objectsKeyArr, "https://"+conf.Address+"/"+conf.Bucket+"/"+object.Key)
|
||||
}
|
||||
|
@ -121,6 +128,7 @@ func GetObjectsFilenames(conf Config, minioClient minio.Client) []string {
|
|||
|
||||
func main() {
|
||||
log.Println("Server Start")
|
||||
rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
conf := InitConf("config.yaml")
|
||||
imagesDir := http.FileServer(http.Dir("images"))
|
||||
staticDir := http.FileServer(http.Dir("dist/assets"))
|
||||
|
@ -140,7 +148,7 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
confHandler.minioClient = *minioClient
|
||||
confHandler.minioClient = minioClient
|
||||
confHandler.ConfigHandler = conf
|
||||
http.HandleFunc("/rand", confHandler.ReturnObjectName)
|
||||
http.HandleFunc("/", confHandler.ReturnHTMLS3)
|
||||
|
|
|
@ -13,6 +13,6 @@
|
|||
"axios": "1.7.9",
|
||||
"postcss": "^8.4.49",
|
||||
"tailwindcss": "^3.4.17",
|
||||
"vite": "^6.0.3"
|
||||
"vite": "^6.2.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue