Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
4 changes: 4 additions & 0 deletions example/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"GONSUL" : "Hi, this is gonsul",
"VERSION" : "version 1.0"
}
4 changes: 3 additions & 1 deletion example/go.mod
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
18 changes: 16 additions & 2 deletions example/main.go
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{

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

Host: "http://localhost:8500/v1/kv",
Path: "gonsul",
File: "./example.json",
HttpClient: http.Client{
Timeout: time.Second * 30,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use const

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or as param?

},
}
err := x.InitFromFile()
fmt.Println(err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err check

}
74 changes: 74 additions & 0 deletions gonsul/client.go
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
}
86 changes: 86 additions & 0 deletions gonsul/domain.go
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
}
14 changes: 14 additions & 0 deletions gonsul/errors.go
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")
)
5 changes: 0 additions & 5 deletions init.go

This file was deleted.