diff --git a/.gitignore b/.gitignore
index 1a709a0..997aece 100644
--- a/.gitignore
+++ b/.gitignore
@@ -28,4 +28,6 @@ main
node_modules
dist
bun.lockb
-package-lock.json
\ No newline at end of file
+package-lock.json
+config.yaml
+go.sum
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
index 8065587..4b3f48d 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -14,9 +14,9 @@ VOLUME /app/images
EXPOSE 3666
-COPY main.go ./
+COPY main.go go.mod ./
-RUN go build main.go
+RUN go get && go build main.go
CMD ["/app/main"]
diff --git a/config.yaml.example b/config.yaml.example
new file mode 100644
index 0000000..163198f
--- /dev/null
+++ b/config.yaml.example
@@ -0,0 +1,5 @@
+use_s3: true
+address: endpoint
+bucket: bucket_name
+access_key:
+secret_key:
\ No newline at end of file
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..b4d6835
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,31 @@
+module picRan
+
+go 1.21.0
+
+toolchain go1.23.4
+
+require gopkg.in/yaml.v3 v3.0.1
+
+require (
+ github.com/aws/aws-sdk-go-v2/service/s3 v1.72.2
+ github.com/minio/minio-go/v7 v7.0.38
+)
+
+require (
+ github.com/dustin/go-humanize v1.0.0 // indirect
+ github.com/google/uuid v1.3.0 // indirect
+ github.com/json-iterator/go v1.1.12 // indirect
+ github.com/klauspost/compress v1.15.9 // indirect
+ github.com/klauspost/cpuid/v2 v2.1.0 // indirect
+ github.com/minio/md5-simd v1.1.2 // indirect
+ github.com/minio/sha256-simd v1.0.0 // indirect
+ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
+ github.com/modern-go/reflect2 v1.0.2 // indirect
+ github.com/rs/xid v1.4.0 // indirect
+ github.com/sirupsen/logrus v1.9.0 // indirect
+ golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
+ golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
+ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
+ golang.org/x/text v0.3.7 // indirect
+ gopkg.in/ini.v1 v1.66.6 // indirect
+)
diff --git a/index.html b/index.html
index aa0272e..4b62bea 100644
--- a/index.html
+++ b/index.html
@@ -37,7 +37,7 @@
-

+
diff --git a/main.go b/main.go
index d6f480e..bd9cf28 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "context"
"fmt"
"io"
"log"
@@ -9,6 +10,10 @@ import (
"os"
"strings"
"text/template"
+
+ "github.com/minio/minio-go/v7"
+ "github.com/minio/minio-go/v7/pkg/credentials"
+ "gopkg.in/yaml.v3"
)
func GetPicsArray() []string {
@@ -21,48 +26,109 @@ func GetPicsArray() []string {
}
var filesNames = make([]string, len(files))
for i, file := range files {
- filesNames[i] = string(file.Name())
+ filesNames[i] = string("images/" + file.Name())
}
return filesNames
}
-func GetRandImg() string {
- picsArray := GetPicsArray()
+func GetRandImg(conf Config) string {
+
+ var picsArray []string
+
+ if conf.UseS3 == true {
+ picsArray = GetObjectsFilenames(conf)
+ } else {
+ picsArray = GetPicsArray()
+ }
+
return picsArray[rand.Intn(len(picsArray))]
}
-func ReturnPicFilename(w http.ResponseWriter, req *http.Request) {
- var picFilename io.Reader = strings.NewReader(GetRandImg())
+func (conf *ConfHandler) ReturnPicFilename(w http.ResponseWriter, req *http.Request) {
+ var picFilename io.Reader = strings.NewReader(GetRandImg(conf.ConfigHandler))
log.Println("Картинка отдана")
io.Copy(w, picFilename)
}
-func ReturnPic(w http.ResponseWriter, req *http.Request) {
+/*func ReturnPic(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 ReturnHTML(w http.ResponseWriter, req *http.Request) {
+func (conf *ConfHandler) ReturnHTML(w http.ResponseWriter, req *http.Request) {
htmlOpen, err := template.ParseFiles("dist/index.html")
if err != nil {
log.Println(err.Error())
return
}
- image := GetRandImg()
+ image := GetRandImg(conf.ConfigHandler)
fmt.Println(image)
htmlOpen.Execute(w, image)
}
+type Config struct {
+ UseS3 bool `yaml:"use_s3"`
+ Address string `yaml:"address"`
+ Bucket string `yaml:"bucket"`
+ AccessKey string `yaml:"access_key"`
+ SecretKey string `yaml:"secret_key"`
+}
+
+type ConfHandler struct {
+ ConfigHandler Config
+}
+
+func InitConf(filename string) Config {
+ var conf Config
+ configFile, _ := os.ReadFile(filename)
+ err := yaml.Unmarshal(configFile, &conf)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ return conf
+}
+
+func GetObjectsFilenames(conf Config) []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())
+
+ defer cancel()
+
+ objectCh := minioClient.ListObjects(ctx, conf.Bucket, minio.ListObjectsOptions{Recursive: false})
+
+ fmt.Println(objectCh)
+ objectsKeyArr := []string{}
+
+ for object := range objectCh {
+ if object.Err != nil {
+ fmt.Println(object.Err)
+ }
+ objectsKeyArr = append(objectsKeyArr, "https://"+conf.Address+"/"+conf.Bucket+"/"+object.Key)
+ }
+ return objectsKeyArr
+}
+
func main() {
log.Println("Server Start")
+ conf := InitConf("config.yaml")
+ confHandler := ConfHandler{ConfigHandler: conf}
imagesDir := http.FileServer(http.Dir("images"))
staticDir := http.FileServer(http.Dir("dist/assets"))
http.Handle("/images/", http.StripPrefix("/images/", imagesDir))
http.Handle("/assets/", http.StripPrefix("/assets/", staticDir))
- http.HandleFunc("/rand", ReturnPicFilename)
- http.HandleFunc("/", ReturnHTML)
+ http.HandleFunc("/rand", confHandler.ReturnPicFilename)
+ http.HandleFunc("/", confHandler.ReturnHTML)
http.ListenAndServe(":3666", nil)
}
diff --git a/src/main.js b/src/main.js
index fd20aea..d24192a 100644
--- a/src/main.js
+++ b/src/main.js
@@ -4,6 +4,6 @@ let pictureId = document.getElementById("CatGirlPicture");
pictureId.onclick = function() {
axios.get(location.href + "rand").then(function (response) {
console.log(response.data)
- document.getElementById("CatGirlPicture").setAttribute("src", "images/"+response.data);
+ document.getElementById("CatGirlPicture").setAttribute("src", response.data);
});
}