Сделал так, что S3 клиент инициализируется и передается в функции, а не инициализируется каждый раз при вызове функции

This commit is contained in:
B4D_US3R 2025-01-16 09:38:50 +05:00
parent d7952f1d12
commit 8b5d80e212

82
main.go
View file

@ -31,39 +31,46 @@ func GetPicsArray() []string {
return filesNames return filesNames
} }
func GetRandImg(conf Config) string { func (conf *ConfHandler) GetRandImgS3() string {
picsArray := GetObjectsFilenames(conf.ConfigHandler, conf.minioClient)
var picsArray []string
if conf.UseS3 == true {
picsArray = GetObjectsFilenames(conf)
} else {
picsArray = GetPicsArray()
}
return picsArray[rand.Intn(len(picsArray))] return picsArray[rand.Intn(len(picsArray))]
} }
func (conf *ConfHandler) ReturnPicFilename(w http.ResponseWriter, req *http.Request) { func (conf *ConfHandler) ReturnObjectName(w http.ResponseWriter, req *http.Request) {
var picFilename io.Reader = strings.NewReader(GetRandImg(conf.ConfigHandler)) var picFilename io.Reader = strings.NewReader(conf.GetRandImgS3())
log.Println("Картинка отдана") log.Println("Картинка отдана")
io.Copy(w, picFilename) io.Copy(w, picFilename)
} }
/*func ReturnPic(w http.ResponseWriter, req *http.Request) { func (conf *ConfHandler) ReturnHTMLS3(w http.ResponseWriter, req *http.Request) {
picture, _ := os.Open(fmt.Sprintf("images/%v", GetRandImg()))
var pictureReader io.Reader = picture
io.Copy(w, pictureReader)
log.Println("Картинка отдана")
}*/
func (conf *ConfHandler) ReturnHTML(w http.ResponseWriter, req *http.Request) {
htmlOpen, err := template.ParseFiles("dist/index.html") htmlOpen, err := template.ParseFiles("dist/index.html")
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
return return
} }
image := GetRandImg(conf.ConfigHandler) image := conf.GetRandImgS3()
fmt.Println(image)
htmlOpen.Execute(w, image)
}
func GetRandImg() string {
picsArray := GetPicsArray()
return picsArray[rand.Intn(len(picsArray))]
}
func ReturnPicFilename(w http.ResponseWriter, req *http.Request) {
var picFilename io.Reader = strings.NewReader(GetRandImg())
log.Println("Картинка отдана")
io.Copy(w, picFilename)
}
func ReturnHTML(w http.ResponseWriter, req *http.Request) {
htmlOpen, err := template.ParseFiles("dist/index.html")
if err != nil {
log.Println(err.Error())
return
}
image := GetRandImg()
fmt.Println(image) fmt.Println(image)
htmlOpen.Execute(w, image) htmlOpen.Execute(w, image)
} }
@ -78,6 +85,7 @@ type Config struct {
type ConfHandler struct { type ConfHandler struct {
ConfigHandler Config ConfigHandler Config
minioClient minio.Client
} }
func InitConf(filename string) Config { func InitConf(filename string) Config {
@ -91,15 +99,7 @@ func InitConf(filename string) Config {
return conf return conf
} }
func GetObjectsFilenames(conf Config) []string { func GetObjectsFilenames(conf Config, minioClient minio.Client) []string {
minioClient, err := minio.New(conf.Address, &minio.Options{
Creds: credentials.NewStaticV4(conf.AccessKey, conf.SecretKey, ""),
Secure: true,
})
if err != nil {
log.Fatalln(err)
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -122,13 +122,29 @@ func GetObjectsFilenames(conf Config) []string {
func main() { func main() {
log.Println("Server Start") log.Println("Server Start")
conf := InitConf("config.yaml") conf := InitConf("config.yaml")
confHandler := ConfHandler{ConfigHandler: conf}
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"))
http.Handle("/images/", http.StripPrefix("/images/", imagesDir)) http.Handle("/images/", http.StripPrefix("/images/", imagesDir))
http.Handle("/assets/", http.StripPrefix("/assets/", staticDir)) http.Handle("/assets/", http.StripPrefix("/assets/", staticDir))
http.HandleFunc("/rand", confHandler.ReturnPicFilename) if !conf.UseS3 {
http.HandleFunc("/", confHandler.ReturnHTML) http.HandleFunc("/rand", ReturnPicFilename)
http.HandleFunc("/", ReturnHTML)
} else {
var confHandler ConfHandler
minioClient, err := minio.New(conf.Address, &minio.Options{
Creds: credentials.NewStaticV4(conf.AccessKey, conf.SecretKey, ""),
Secure: true,
})
if err != nil {
log.Fatalln(err)
}
confHandler.minioClient = *minioClient
confHandler.ConfigHandler = conf
http.HandleFunc("/rand", confHandler.ReturnObjectName)
http.HandleFunc("/", confHandler.ReturnHTMLS3)
}
http.ListenAndServe(":3666", nil) http.ListenAndServe(":3666", nil)
} }