implemented configuration from config.json file

This commit is contained in:
arnaucode
2017-06-21 00:33:14 +02:00
parent e72905e6b2
commit 1e7913074f
8 changed files with 86 additions and 5 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
files

View File

@@ -1,6 +1,16 @@
# goImgCDN # goImgCDN
CDN for images, written in Go lang 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): upload example (with curl):
``` ```
curl -F file=@./image1.png http://127.0.0.1:3050/image 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 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.

12
config.json Normal file
View File

@@ -0,0 +1,12 @@
{
"folder": "files",
"blockedIPs": [
"192.168.1.3",
"147.116.48.158"
],
"allowedIPs": [
"127.0.0.1"
],
"imgWidth": 200,
"imgHeigh": 0
}

View File

@@ -58,7 +58,7 @@ func NewImage(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
fmt.Fprintln(w, "error al processar la imatge") 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 { if err != nil {
fmt.Println(err) fmt.Println(err)
} }

View File

@@ -59,6 +59,6 @@ func imageToPNG(img image.Image) ([]byte, error) {
} }
func Resize(img image.Image) image.Image { 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 return r
} }

View File

@@ -10,10 +10,17 @@ import (
func ipFilter(r *http.Request) error { func ipFilter(r *http.Request) error {
var err error var err error
fmt.Println(r.RemoteAddr) fmt.Println(r.RemoteAddr)
ip := strings.Split(r.RemoteAddr, ":")[0] reqIP := strings.Split(r.RemoteAddr, ":")[0]
if ip != "127.0.0.1" { for _, ip := range config.BlockedIPs {
err = errors.New("ip not allowed to post images") 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 return err
} }

View File

@@ -1,11 +1,14 @@
package main package main
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
) )
func main() { func main() {
readConfig("config.json")
fmt.Println(config)
router := NewRouter() router := NewRouter()

26
readConfig.go Normal file
View File

@@ -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)
}