mirror of
https://github.com/aykhans/dodo.git
synced 2025-04-16 09:53:12 +00:00
✨ Added http support to 'JSONConfigReader' function
This commit is contained in:
parent
f721abb583
commit
cc2a6eb367
9
go.mod
9
go.mod
@ -3,25 +3,22 @@ module github.com/aykhans/dodo
|
|||||||
go 1.24
|
go 1.24
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/fatih/color v1.18.0
|
||||||
github.com/go-playground/validator/v10 v10.25.0
|
github.com/go-playground/validator/v10 v10.25.0
|
||||||
github.com/jedib0t/go-pretty/v6 v6.6.7
|
github.com/jedib0t/go-pretty/v6 v6.6.7
|
||||||
github.com/valyala/fasthttp v1.59.0
|
github.com/valyala/fasthttp v1.59.0
|
||||||
golang.org/x/net v0.37.0
|
golang.org/x/net v0.37.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.1.1 // indirect
|
github.com/andybalholm/brotli v1.1.1 // indirect
|
||||||
github.com/fatih/color v1.18.0
|
|
||||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/klauspost/compress v1.17.11 // indirect
|
github.com/klauspost/compress v1.17.11 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.16 // indirect
|
github.com/mattn/go-runewidth v0.0.16 // indirect
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
|
@ -2,17 +2,46 @@ package readers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/aykhans/dodo/config"
|
"github.com/aykhans/dodo/config"
|
||||||
customerrors "github.com/aykhans/dodo/custom_errors"
|
customerrors "github.com/aykhans/dodo/custom_errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func JSONConfigReader(filePath string) (*config.JSONConfig, error) {
|
func JSONConfigReader(filePath string) (*config.JSONConfig, error) {
|
||||||
data, err := os.ReadFile(filePath)
|
var (
|
||||||
if err != nil {
|
data []byte
|
||||||
return nil, customerrors.OSErrorFormater(err)
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://") {
|
||||||
|
client := &http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Get(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to fetch JSON config from %s", filePath)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
data, err = io.ReadAll(io.Reader(resp.Body))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read JSON config from %s", filePath)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data, err = os.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, customerrors.OSErrorFormater(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonConf := config.NewJSONConfig(
|
jsonConf := config.NewJSONConfig(
|
||||||
config.NewConfig("", 0, 0, 0, nil),
|
config.NewConfig("", 0, 0, 0, nil),
|
||||||
nil, nil, nil, nil, nil,
|
nil, nil, nil, nil, nil,
|
||||||
@ -32,5 +61,6 @@ func JSONConfigReader(filePath string) (*config.JSONConfig, error) {
|
|||||||
}
|
}
|
||||||
return nil, customerrors.NewInvalidFileError(filePath, err)
|
return nil, customerrors.NewInvalidFileError(filePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return jsonConf, nil
|
return jsonConf, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user