From 1e7913074f75e354cad341ecb5d67dfc1e1a6d94 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Wed, 21 Jun 2017 00:33:14 +0200 Subject: [PATCH] implemented configuration from config.json file --- .gitignore | 1 + README.md | 32 ++++++++++++++++++++++++++++++++ config.json | 12 ++++++++++++ handlers.go | 2 +- imageOperations.go | 2 +- ipFilter.go | 13 ++++++++++--- main.go | 3 +++ readConfig.go | 26 ++++++++++++++++++++++++++ 8 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 config.json create mode 100644 readConfig.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..027271b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +files diff --git a/README.md b/README.md index 8580d11..1d861b8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,16 @@ # goImgCDN CDN for images, written in Go lang +This is a server to post images and serve images. +File type accepted: +``` +- PNG +- JPG +- JPEG +``` +All the files are converted to PNG, and resized to the configured sizes. + +## Use upload example (with curl): ``` curl -F file=@./image1.png http://127.0.0.1:3050/image @@ -10,3 +20,25 @@ to get image: ``` http://127.0.0.1:3050/images/image1.png ``` + +## Configuration +Example configuration file (config.json): +``` +{ + "folder": "files", + "blockedIPs": [ + "192.168.1.3", + "147.116.48.158" + ], + "allowedIPs": [ + "127.0.0.1" + ], + "imgWidth": 200, + "imgHeigh": 0 +} +``` +The "allowedIPs" are the IPs allowed to post images. + +"blockedIPs" are the IPs blocked from posting images. + +Also, all IPs not present in the "allowedIPs", will be blocked. diff --git a/config.json b/config.json new file mode 100644 index 0000000..04e457d --- /dev/null +++ b/config.json @@ -0,0 +1,12 @@ +{ + "folder": "files", + "blockedIPs": [ + "192.168.1.3", + "147.116.48.158" + ], + "allowedIPs": [ + "127.0.0.1" + ], + "imgWidth": 200, + "imgHeigh": 0 +} diff --git a/handlers.go b/handlers.go index 329d3c9..d4d9ae4 100644 --- a/handlers.go +++ b/handlers.go @@ -58,7 +58,7 @@ func NewImage(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Fprintln(w, "error al processar la imatge") } - err = ioutil.WriteFile(fileName, data, 0777) + err = ioutil.WriteFile(config.Folder+"/"+fileName, data, 0777) if err != nil { fmt.Println(err) } diff --git a/imageOperations.go b/imageOperations.go index c409ad0..0d2806a 100644 --- a/imageOperations.go +++ b/imageOperations.go @@ -59,6 +59,6 @@ func imageToPNG(img image.Image) ([]byte, error) { } func Resize(img image.Image) image.Image { - r := resize.Resize(160, 0, img, resize.Lanczos3) + r := resize.Resize(uint(config.ImgWidth), uint(config.ImgHeigh), img, resize.Lanczos3) return r } diff --git a/ipFilter.go b/ipFilter.go index 8cc3d21..f4c8428 100644 --- a/ipFilter.go +++ b/ipFilter.go @@ -10,10 +10,17 @@ import ( func ipFilter(r *http.Request) error { var err error fmt.Println(r.RemoteAddr) - ip := strings.Split(r.RemoteAddr, ":")[0] - if ip != "127.0.0.1" { - err = errors.New("ip not allowed to post images") + reqIP := strings.Split(r.RemoteAddr, ":")[0] + for _, ip := range config.BlockedIPs { + if reqIP == ip { + err = errors.New("ip not allowed to post images") + } } + for _, ip := range config.AllowedIPs { + if reqIP != ip { + err = errors.New("ip not allowed to post images") + } + } return err } diff --git a/main.go b/main.go index 73dcf92..5a08f94 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,14 @@ package main import ( + "fmt" "log" "net/http" ) func main() { + readConfig("config.json") + fmt.Println(config) router := NewRouter() diff --git a/readConfig.go b/readConfig.go new file mode 100644 index 0000000..234a329 --- /dev/null +++ b/readConfig.go @@ -0,0 +1,26 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" +) + +type Config struct { + Folder string `json:"foldIPs` + BlockedIPs []string `json:"blockedIPs"` + AllowedIPs []string `json:"allowedIPs"` + ImgWidth int `json:"imgWidth"` + ImgHeigh int `json:"imgHeigh"` +} + +var config Config + +func readConfig(path string) { + file, err := ioutil.ReadFile(path) + if err != nil { + fmt.Println("error: ", err) + } + content := string(file) + json.Unmarshal([]byte(content), &config) +}