arnaucube
arnaucube/goImgServerPublic Self-hosted repository
implemented configuration from config.json file
arnaucode idoctnef@openmailbox.org
Wed, 21 Jun 2017 00:33:14 +0200
commit

1e7913074f75e354cad341ecb5d67dfc1e1a6d94

parent

e72905e6b227fcfb5078f2786fd0994e0886ee4d

8 files changed, 86 insertions(+), 5 deletions(-)

jump to
A .gitignore

@@ -0,0 +1,1 @@

+files
M README.md → 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.
A 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 +}
M handlers.go → handlers.go

@@ -58,7 +58,7 @@ data, err = imageToPNG(img)

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) }
M imageOperations.go → imageOperations.go

@@ -59,6 +59,6 @@ return buf.Bytes(), err

} 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 }
M ipFilter.go → ipFilter.go

@@ -10,10 +10,17 @@

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 }
M main.go → main.go

@@ -1,11 +1,14 @@

package main import ( + "fmt" "log" "net/http" ) func main() { + readConfig("config.json") + fmt.Println(config) router := NewRouter()
A 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) +}