Skip to content

Commit

Permalink
simplify samples
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfriesen committed May 28, 2024
1 parent 664f47e commit 49a88c8
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 271 deletions.
9 changes: 5 additions & 4 deletions apps/curl-geoweather/geoweather.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
# Function to get public IP
get_public_ip() {
local ip
ip=$(curl -s https://httpbin.org/ip | jq -r .origin)
ip=$(curl -s icanhazip.com)
if [ -z "$ip" ]; then
echo "Unable to get public IP"
exit 1
fi

echo "$ip"
}

# Function to get location from IP
get_location() {
local ip="$1"
local location
location=$(curl -s "http://ip-api.com/json/{$ip}")
location=$(curl -s "https://ipwho.is/{$ip}?fields=country,city,latitude,longitude")
if [ -z "$location" ]; then
echo "Unable to get location"
exit 1
Expand Down Expand Up @@ -50,8 +51,8 @@ main() {
local city country latitude longitude
city=$(echo "$location" | jq -r .city)
country=$(echo "$location" | jq -r .country)
latitude=$(echo "$location" | jq -r .lat)
longitude=$(echo "$location" | jq -r .lon)
latitude=$(echo "$location" | jq -r .latitude)
longitude=$(echo "$location" | jq -r .longitude)
echo -e "\n2. Location fetched for IP $public_ip:\n\t$city, $country\n\t($latitude, $longitude)"

if [ -z "$latitude" ] || [ -z "$longitude" ]; then
Expand Down
22 changes: 10 additions & 12 deletions apps/go-geoweather/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import (
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"strings"
)

type IPResponse struct {
Origin string `json:"origin"`
}

type LocationResponse struct {
City string `json:"city"`
CountryName string `json:"country_name"`
Latitude float64 `json:"lat"`
Longitude float64 `json:"lon"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}

type WeatherResponse struct {
Expand All @@ -26,22 +24,22 @@ type WeatherResponse struct {
}

func getPublicIP() (string, error) {
resp, err := http.Get("https://httpbin.org/ip")
resp, err := http.Get("https://icanhazip.com")
if err != nil {
return "", fmt.Errorf("unable to get public IP: %v", err)
}
defer resp.Body.Close()

var ipResp IPResponse
if err := json.NewDecoder(resp.Body).Decode(&ipResp); err != nil {
return "", fmt.Errorf("unable to parse IP response: %v", err)
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("reading response body: %w", err)
}

return ipResp.Origin, nil
return strings.TrimSpace(string(b)), nil
}

func getLocation(ip string) (LocationResponse, error) {
url := fmt.Sprintf("http://ip-api.com/json/%s", ip)
url := fmt.Sprintf("https://ipwho.is/%s?fields=country,city,latitude,longitude", ip)
resp, err := http.Get(url)
if err != nil {
return LocationResponse{}, fmt.Errorf("unable to get location: %v", err)
Expand Down
31 changes: 0 additions & 31 deletions apps/php-geoweather/Dockerfile

This file was deleted.

70 changes: 0 additions & 70 deletions apps/php-geoweather/README.md

This file was deleted.

99 changes: 0 additions & 99 deletions apps/php-geoweather/index.php

This file was deleted.

10 changes: 4 additions & 6 deletions apps/python-geoweather/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# Use an official Python runtime as a parent image
FROM python:3.12-slim
FROM python:3.12-alpine

# Get latest CA's
ARG TOKEN
RUN apt-get update && \
apt-get install -y curl ca-certificates && \
RUN apk --no-cache add curl ca-certificates && \
if [ -n "$TOKEN" ]; then \
curl -s https://api.qpoint.io/deploy/certificate -H "Authorization: Bearer $TOKEN" > /usr/local/share/ca-certificates/qpoint-ca.crt && \
update-ca-certificates; \
fi && \
rm -rf /var/lib/apt/lists/*
fi

# Set the working directory to /app
WORKDIR /app
Expand All @@ -18,7 +16,7 @@ WORKDIR /app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install Flask[async] requests httpx h2
RUN pip install flask requests

# Make port 4000 available to the world outside this container
EXPOSE 4000
Expand Down
Loading

0 comments on commit 49a88c8

Please sign in to comment.