Browse Source

serve images runs ok

master
arnaucode 7 years ago
parent
commit
ab69ef235d
8 changed files with 228 additions and 0 deletions
  1. +19
    -0
      .gitignore
  2. +9
    -0
      errors.go
  3. +16
    -0
      imageOperations.go
  4. +19
    -0
      log.go
  5. +27
    -0
      main.go
  6. +68
    -0
      serverConfig.go
  7. +5
    -0
      serverConfig.json
  8. +65
    -0
      serverRoutes.go

+ 19
- 0
.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

+ 9
- 0
errors.go

@ -0,0 +1,9 @@
package main
import "log"
func check(err error) {
if err != nil {
log.Println(err)
}
}

+ 16
- 0
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
}

+ 19
- 0
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)
}

+ 27
- 0
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)))
}

+ 68
- 0
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
}

+ 5
- 0
serverConfig.json

@ -0,0 +1,5 @@
{
"serverIP": "127.0.0.1",
"serverPort": "3025",
"imgsFolder": "imgs"
}

+ 65
- 0
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))
}

Loading…
Cancel
Save