mirror of
https://github.com/arnaucube/goImgServer.git
synced 2026-02-07 03:26:43 +01:00
implemented configuration from config.json file
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
files
|
||||
32
README.md
32
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.
|
||||
|
||||
12
config.json
Normal file
12
config.json
Normal 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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
11
ipFilter.go
11
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" {
|
||||
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
|
||||
}
|
||||
|
||||
3
main.go
3
main.go
@@ -1,11 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
readConfig("config.json")
|
||||
fmt.Println(config)
|
||||
|
||||
router := NewRouter()
|
||||
|
||||
|
||||
26
readConfig.go
Normal file
26
readConfig.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user