Добавил S3, немного переделал логику, добавил поддержку конфига, немного переделал index.html и main.js.
This commit is contained in:
parent
6e042a05c0
commit
d7952f1d12
7 changed files with 120 additions and 16 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -28,4 +28,6 @@ main
|
|||
node_modules
|
||||
dist
|
||||
bun.lockb
|
||||
package-lock.json
|
||||
package-lock.json
|
||||
config.yaml
|
||||
go.sum
|
|
@ -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"]
|
||||
|
||||
|
|
5
config.yaml.example
Normal file
5
config.yaml.example
Normal file
|
@ -0,0 +1,5 @@
|
|||
use_s3: true
|
||||
address: endpoint
|
||||
bucket: bucket_name
|
||||
access_key:
|
||||
secret_key:
|
31
go.mod
Normal file
31
go.mod
Normal file
|
@ -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
|
||||
)
|
|
@ -37,7 +37,7 @@
|
|||
</div>
|
||||
</div>
|
||||
<div class="flex-none w-96">
|
||||
<img id="CatGirlPicture" src="images/{{ . }}">
|
||||
<img id="CatGirlPicture" src="{{ . }}">
|
||||
</div>
|
||||
<div class="flex-1">
|
||||
|
||||
|
|
88
main.go
88
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)
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue