Skip to content

Commit

Permalink
chore: chart version bump (#98)
Browse files Browse the repository at this point in the history
* chore: chart version bump

fix: response body to string

* chore: use unmarshall and location interface for decode json

* chore: render json response
  • Loading branch information
saidsef authored Apr 6, 2024
1 parent 40939f7 commit 63b3c5d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion charts/reverse-geocoding/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: reverse-geocoding
description: Reverse GeoCode Helm chart for Kubernetes
type: application
version: &version 0.2.13
version: &version 0.2.14
appVersion: *version
kubeVersion: ">= 1.22"
keywords:
Expand Down
2 changes: 1 addition & 1 deletion charts/reverse-geocoding/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ replicaCount: 1
image:
repository: ghcr.io/saidsef/faas-reverse-geocoding
pullPolicy: IfNotPresent
tag: "v2024.02"
tag: "v2024.04"

imagePullSecrets: []
nameOverride: "geocode"
Expand Down
14 changes: 9 additions & 5 deletions geocode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"flag"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
Expand All @@ -26,6 +25,9 @@ var (
// port defines the port on which the server listens. It can be set via command-line flag.
port int

// location response json interface
location interface{}

// logger provides a logging instance prefixed with "[http]" and standard flags.
logger = log.New(os.Stdout, "[http] ", log.LstdFlags)

Expand Down Expand Up @@ -84,20 +86,22 @@ func latitudeLongitude(w http.ResponseWriter, r *http.Request) {
}
defer resp.Body.Close()

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
if err := json.NewDecoder(resp.Body).Decode(&location); err != nil {
http.Error(w, fmt.Sprintf("Error reading response body: %s", err), http.StatusInternalServerError)
return
}

if resp.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("External API error: %s", string(bodyBytes)), resp.StatusCode)
http.Error(w, fmt.Sprintf("External API error: %s", location), resp.StatusCode)
return
}

// Define a template that safely escapes data.
tmpl := template.Must(template.New("safeTemplate").Parse("{{.}}"))
tmpl.Execute(w, bodyBytes)
if err := tmpl.Execute(w, json.NewEncoder(w).Encode(location)); err != nil {
http.Error(w, "Error rendering template", http.StatusInternalServerError)
return
}
default:
http.Error(w, `{"status": "method not allowed"}`, http.StatusMethodNotAllowed)
}
Expand Down

0 comments on commit 63b3c5d

Please sign in to comment.