Первый блин комом

This commit is contained in:
B4D_US3R 2024-08-08 11:12:57 +05:00
parent aff73b5b43
commit 68fbcb15c1
4 changed files with 70 additions and 0 deletions

4
.gitignore vendored
View file

@ -21,3 +21,7 @@
# Go workspace file # Go workspace file
go.work go.work
#Images dir
images/
main

14
Dockerfile Normal file
View file

@ -0,0 +1,14 @@
FROM golang:1.22-alpine
WORKDIR /app
VOLUME /app/images
EXPOSE 3666
COPY main.go ./
RUN go build main.go
CMD ["/app/main"]

9
docker-compose.yml Normal file
View file

@ -0,0 +1,9 @@
version: "3"
services:
picran:
build: .
ports:
- "3666:3666"
volumes:
- "./images:/app/images"

43
main.go Normal file
View file

@ -0,0 +1,43 @@
package main
import (
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
)
func GetPicsArray() []string {
files, err := os.ReadDir("images")
if err != nil {
panic(err)
}
if len(files) == 0 {
return []string{"no images"}
}
var filesNames = make([]string, len(files))
for i, file := range files {
filesNames[i] = string(file.Name())
}
return filesNames
}
func GetRandImg() string {
picsArray := GetPicsArray()
return picsArray[rand.Intn(len(picsArray))]
}
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 main() {
log.Println("Server Start")
http.HandleFunc("/", ReturnPic)
http.ListenAndServe(":3666", nil)
}