From 68fbcb15c15d390ba772d9d5254a8f922ff011da Mon Sep 17 00:00:00 2001 From: B4D_US3R Date: Thu, 8 Aug 2024 11:12:57 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20=D0=B1?= =?UTF-8?q?=D0=BB=D0=B8=D0=BD=20=D0=BA=D0=BE=D0=BC=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++++ Dockerfile | 14 ++++++++++++++ docker-compose.yml | 9 +++++++++ main.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 main.go diff --git a/.gitignore b/.gitignore index adf8f72..84159ec 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,7 @@ # Go workspace file go.work +#Images dir +images/ + +main diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a248c9c --- /dev/null +++ b/Dockerfile @@ -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"] + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6a4985d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: "3" + +services: + picran: + build: . + ports: + - "3666:3666" + volumes: + - "./images:/app/images" \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..34c4ad8 --- /dev/null +++ b/main.go @@ -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) +}