From ab69ef235d551f483ffe69cd08a29b68686a14ad Mon Sep 17 00:00:00 2001 From: arnaucode Date: Tue, 8 Aug 2017 14:43:20 +0200 Subject: [PATCH] serve images runs ok --- .gitignore | 19 +++++++++++++ errors.go | 9 ++++++ imageOperations.go | 16 +++++++++++ log.go | 19 +++++++++++++ main.go | 27 ++++++++++++++++++ serverConfig.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++ serverConfig.json | 5 ++++ serverRoutes.go | 65 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 228 insertions(+) create mode 100644 .gitignore create mode 100644 errors.go create mode 100644 imageOperations.go create mode 100644 log.go create mode 100644 main.go create mode 100644 serverConfig.go create mode 100644 serverConfig.json create mode 100644 serverRoutes.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6157fc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# Binaries for programs and plugins +*.exe +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ + + +/*.conf +logs +imgs diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..e997ec1 --- /dev/null +++ b/errors.go @@ -0,0 +1,9 @@ +package main + +import "log" + +func check(err error) { + if err != nil { + log.Println(err) + } +} diff --git a/imageOperations.go b/imageOperations.go new file mode 100644 index 0000000..8af06b3 --- /dev/null +++ b/imageOperations.go @@ -0,0 +1,16 @@ +package main + +import ( + "bytes" + "image" + "image/png" +) + +func dataToPNG(data []byte, imageName string) (image.Image, error) { + reader := bytes.NewReader(data) + img, err := png.Decode(reader) + if err != nil { + return img, err + } + return img, err +} diff --git a/log.go b/log.go new file mode 100644 index 0000000..d12c469 --- /dev/null +++ b/log.go @@ -0,0 +1,19 @@ +package main + +import ( + "io" + "log" + "os" + "time" +) + +func savelog() { + timeS := time.Now().String() + _ = os.Mkdir("logs", os.ModePerm) + logFile, err := os.OpenFile("logs/log-"+timeS+".log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) + if err != nil { + panic(err) + } + mw := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(mw) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..98d85da --- /dev/null +++ b/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "log" + "net/http" + + "github.com/gorilla/handlers" +) + +func main() { + savelog() + log.Println("goCaptcha started") + + //start the server + //http server start + readServerConfig("./serverConfig.json") + + log.Println("server running") + log.Print("port: ") + log.Println(serverConfig.ServerPort) + router := NewRouter() + + headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"}) + originsOk := handlers.AllowedOrigins([]string{"*"}) + methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"}) + log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, handlers.CORS(originsOk, headersOk, methodsOk)(router))) +} diff --git a/serverConfig.go b/serverConfig.go new file mode 100644 index 0000000..2d56dac --- /dev/null +++ b/serverConfig.go @@ -0,0 +1,68 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "time" + + "github.com/gorilla/mux" +) + +type Route struct { + Name string + Method string + Pattern string + HandlerFunc http.HandlerFunc +} + +//server config +type ServerConfig struct { + ServerIP string `json:"serverIP"` + ServerPort string `json:"serverPort"` + ImgsFolder string `json:"imgsFolder"` +} + +var serverConfig ServerConfig + +func readServerConfig(path string) { + file, err := ioutil.ReadFile(path) + if err != nil { + fmt.Println("error: ", err) + } + content := string(file) + json.Unmarshal([]byte(content), &serverConfig) +} + +func Logger(inner http.Handler, name string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + + inner.ServeHTTP(w, r) + + log.Printf( + "%s\t%s\t%s\t%s", + r.Method, + r.RequestURI, + name, + time.Since(start), + ) + }) +} +func NewRouter() *mux.Router { + router := mux.NewRouter().StrictSlash(true) + for _, route := range routes { + var handler http.Handler + handler = route.HandlerFunc + handler = Logger(handler, route.Name) + + router. + Methods(route.Method). + Path(route.Pattern). + Name(route.Name). + Handler(handler) + } + return router +} diff --git a/serverConfig.json b/serverConfig.json new file mode 100644 index 0000000..d3f940b --- /dev/null +++ b/serverConfig.json @@ -0,0 +1,5 @@ +{ + "serverIP": "127.0.0.1", + "serverPort": "3025", + "imgsFolder": "imgs" +} diff --git a/serverRoutes.go b/serverRoutes.go new file mode 100644 index 0000000..a455080 --- /dev/null +++ b/serverRoutes.go @@ -0,0 +1,65 @@ +package main + +import ( + "encoding/json" + "fmt" + "image/jpeg" + "io/ioutil" + "net/http" + + "github.com/gorilla/mux" +) + +type Routes []Route + +var routes = Routes{ + Route{ + "Index", + "GET", + "/", + Index, + }, + Route{ + "GetImage", + "GET", + "/image/{imageName}", + GetImage, + }, + Route{ + "GetCaptcha", + "GET", + "/captcha", + GetCaptcha, + }, +} + +//ROUTES + +func Index(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "ask for images in /r") +} + +func GetImage(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + imageName := vars["imageName"] + + file, err := ioutil.ReadFile(serverConfig.ImgsFolder + "/" + imageName) + if err != nil { + fmt.Fprintln(w, err) + } + + img, err := dataToPNG(file, imageName) + + if err != nil { + fmt.Fprintln(w, "image "+imageName+" does not exist in server") + } else { + jpeg.Encode(w, img, nil) // Write to the ResponseWriter + } +} +func GetCaptcha(w http.ResponseWriter, r *http.Request) { + + resp := "" + jsonResp, err := json.Marshal(resp) + check(err) + fmt.Fprintln(w, string(jsonResp)) +}