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