Browse Source

implemented configuration from config.json file

master
arnaucode 7 years ago
parent
commit
1e7913074f
8 changed files with 86 additions and 5 deletions
  1. +1
    -0
      .gitignore
  2. +32
    -0
      README.md
  3. +12
    -0
      config.json
  4. +1
    -1
      handlers.go
  5. +1
    -1
      imageOperations.go
  6. +10
    -3
      ipFilter.go
  7. +3
    -0
      main.go
  8. +26
    -0
      readConfig.go

+ 1
- 0
.gitignore

@ -0,0 +1 @@
files

+ 32
- 0
README.md

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

+ 1
- 1
handlers.go

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

+ 1
- 1
imageOperations.go

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

+ 10
- 3
ipFilter.go

@ -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]
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 return err
} }

+ 3
- 0
main.go

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

Loading…
Cancel
Save