-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
init KV from .json file #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"GONSUL" : "Hi, this is gonsul", | ||
"VERSION" : "version 1.0" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module example | ||
|
||
replace github.com/rajatm99/gonsul => ../../gonsul | ||
go 1.22.2 | ||
|
||
require github.com/rajatm99/gonsul v0.0.0-00010101000000-000000000000 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
package main | ||
|
||
import "fmt" | ||
import ( | ||
"fmt" | ||
"github.com/rajatm99/gonsul/gonsul" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Hello World!") | ||
x := gonsul.Gonsul{ | ||
Host: "http://localhost:8500/v1/kv", | ||
Path: "gonsul", | ||
File: "./example.json", | ||
HttpClient: http.Client{ | ||
Timeout: time.Second * 30, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use const There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or as param? |
||
}, | ||
} | ||
err := x.InitFromFile() | ||
fmt.Println(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. err check |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package gonsul | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type consulResponse struct { | ||
Value string `json:"Value"` | ||
} | ||
|
||
type apiParams struct { | ||
method string | ||
body []byte | ||
} | ||
|
||
func (g Gonsul) callAPI(params apiParams) (httpResponse *http.Response, err error) { | ||
request, err := http.NewRequest(params.method, fmt.Sprintf("%s/%s", g.Host, g.Path), bytes.NewBuffer(params.body)) | ||
if err != nil { | ||
err = errUnableToCreateHttpRequest | ||
return | ||
} | ||
|
||
request.Header.Set("Content-Type", "application/json") | ||
|
||
httpResponse, err = g.HttpClient.Do(request) | ||
if err != nil { | ||
err = errUnableToCallConsulAPI | ||
} | ||
return | ||
} | ||
|
||
func (g Gonsul) getValues() (resp consulResponse, err error) { | ||
response, err := g.callAPI(apiParams{method: http.MethodGet, body: nil}) | ||
if err != nil { | ||
return | ||
} | ||
bodyBytes, err := io.ReadAll(response.Body) | ||
if err != nil { | ||
err = errUnableToReadConsulResponse | ||
return | ||
} | ||
|
||
if len(bodyBytes) == 0 { | ||
err = errConsulKeyNotFound | ||
return | ||
} | ||
var r []consulResponse | ||
err = json.Unmarshal(bodyBytes, &r) | ||
if err != nil { | ||
err = errUnableToReadConsulResponse | ||
return | ||
} | ||
if len(r) > 0 { | ||
return r[0], nil | ||
} | ||
return | ||
} | ||
|
||
func (g Gonsul) putValues(value []byte) (err error) { | ||
response, err := g.callAPI(apiParams{method: http.MethodPut, body: value}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if response.StatusCode != 200 { | ||
err = errConsulUpdateValueFailed | ||
return | ||
} | ||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package gonsul | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type Gonsul struct { | ||
Host string | ||
Path string | ||
File string | ||
HttpClient http.Client | ||
} | ||
|
||
func (g Gonsul) InitFromFile() (err error) { | ||
fileData, err := os.ReadFile(g.File) | ||
if err != nil { | ||
err = errFileNotFound | ||
return | ||
} | ||
|
||
data := make(map[string]interface{}) | ||
|
||
err = json.Unmarshal(fileData, &data) | ||
if err != nil { | ||
err = errUnableToReadInputFile | ||
return | ||
} | ||
kv, err := g.GetKV() | ||
if err != nil && !errors.Is(err, errConsulKeyNotFound) { | ||
return err | ||
} | ||
|
||
var flag bool | ||
for k, v := range data { | ||
if _, ok := kv[k]; !ok { | ||
flag = true | ||
kv[k] = v | ||
} | ||
} | ||
|
||
if flag == true { | ||
finalJSON, _err := json.MarshalIndent(data, "", " ") | ||
if _err != nil { | ||
err = _err | ||
return | ||
} | ||
|
||
err = g.putValues(finalJSON) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (g Gonsul) GetKV() (kv map[string]interface{}, err error) { | ||
kv = make(map[string]interface{}) | ||
|
||
values, err := g.getValues() | ||
if err != nil { | ||
return | ||
} | ||
|
||
if len(values.Value) == 0 { | ||
err = errValueNotFound | ||
} | ||
|
||
decodeString, err := base64.StdEncoding.DecodeString(values.Value) | ||
if err != nil { | ||
err = errUnableToReadConsulResponse | ||
return | ||
} | ||
|
||
err = json.Unmarshal(decodeString, &kv) | ||
if err != nil { | ||
err = errUnableToReadConsulResponse | ||
return | ||
} | ||
|
||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package gonsul | ||
|
||
import "errors" | ||
|
||
var ( | ||
errUnableToCreateHttpRequest = errors.New("GONSUL_UNABLE_TO_CREATE_HTTP_REQUEST") | ||
errUnableToCallConsulAPI = errors.New("GONSUL_UNABLE_TO_CALL_CONSUL_API") | ||
errUnableToReadConsulResponse = errors.New("GONSUL_UNABLE_TO_READ_CONSUL_RESPONSE") | ||
errConsulKeyNotFound = errors.New("GONSUL_CONSUL_KEY_NOT_FOUND") | ||
errConsulUpdateValueFailed = errors.New("GONSUL_CONSUL_UPDATE_VALUE_FAILED") | ||
errUnableToReadInputFile = errors.New("GONSUL_UNABLE_TO_READ_INPUT_FILE") | ||
errFileNotFound = errors.New("GONSUL_FILE_NOT_FOUND") | ||
errValueNotFound = errors.New("GONSUL_VALUE_NOT_FOUND") | ||
) |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x => consul
any other variable name also works