Первый блин комом
This commit is contained in:
parent
aff73b5b43
commit
68fbcb15c1
4 changed files with 70 additions and 0 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -21,3 +21,7 @@
|
|||
# Go workspace file
|
||||
go.work
|
||||
|
||||
#Images dir
|
||||
images/
|
||||
|
||||
main
|
||||
|
|
14
Dockerfile
Normal file
14
Dockerfile
Normal 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
9
docker-compose.yml
Normal file
|
@ -0,0 +1,9 @@
|
|||
version: "3"
|
||||
|
||||
services:
|
||||
picran:
|
||||
build: .
|
||||
ports:
|
||||
- "3666:3666"
|
||||
volumes:
|
||||
- "./images:/app/images"
|
43
main.go
Normal file
43
main.go
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue