@ -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 |
@ -0,0 +1,9 @@ |
|||
package main |
|||
|
|||
import "log" |
|||
|
|||
func check(err error) { |
|||
if err != nil { |
|||
log.Println(err) |
|||
} |
|||
} |
@ -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 |
|||
} |
@ -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) |
|||
} |
@ -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))) |
|||
} |
@ -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 |
|||
} |
@ -0,0 +1,5 @@ |
|||
{ |
|||
"serverIP": "127.0.0.1", |
|||
"serverPort": "3025", |
|||
"imgsFolder": "imgs" |
|||
} |
@ -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)) |
|||
} |