Initial commit

This commit is contained in:
Maxim Samoilov
2020-07-07 12:12:13 +03:00
commit cd8dd67e5b
16 changed files with 256 additions and 0 deletions

1
app/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tmp-image

9
app/Dockerfile Normal file
View File

@ -0,0 +1,9 @@
FROM golang:1.14
WORKDIR /app
COPY . .
RUN go build -o app
CMD /app/app

5
app/go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/CSSSR/my-app
go 1.14
require github.com/logrusorgru/aurora v2.0.3+incompatible

2
app/go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=

72
app/main.go Normal file
View File

@ -0,0 +1,72 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/logrusorgru/aurora"
)
type appConfig struct {
Port string
ImagePath string
}
var config appConfig
func init() {
config.Port = os.Getenv("PORT")
if config.Port == "" {
config.Port = "3000"
}
config.ImagePath = os.Getenv("IMAGE_PATH")
if config.ImagePath == "" {
config.ImagePath = "tmp-image"
}
}
func uploadFile(w http.ResponseWriter, r *http.Request) {
file, _, err := r.FormFile("image")
if err != nil {
log.Fatalf("Error Retrieving the File %v", err)
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
log.Fatalf("Error reading file %v", err)
}
ioutil.WriteFile(config.ImagePath, fileBytes, os.FileMode(0600))
log.Println(aurora.Cyan("Successfully Uploaded File"))
fmt.Fprintf(w, "Successfully Uploaded File\n")
}
func image(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, config.ImagePath)
}
func livenessProbe(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
}
func readinessProbe(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
}
func main() {
http.HandleFunc("/upload", uploadFile)
http.HandleFunc("/image", image)
http.HandleFunc("/healthz/liveness", livenessProbe)
http.HandleFunc("/healthz/readiness", readinessProbe)
err := http.ListenAndServe(fmt.Sprintf(":%s", config.Port), nil)
if err != nil {
log.Fatal(err)
}
}