Skip to content

Commit

Permalink
REST api boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Vijaya Prakash Masilamani authored and Vijaya Prakash Masilamani committed Oct 24, 2024
1 parent 94707ed commit 56dfea5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
12 changes: 12 additions & 0 deletions weather-process/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BINARY := weather-notify
INPUT_FILES := $(wildcard *.go)

all: ${BINARY}

${BINARY}: ${INPUT_FILES}
@echo '--------- Building $@.... ------'
go build -o $@ *.go
@echo '=============== DONE ==========='

clean:
rm -rf ${BINARY}
31 changes: 10 additions & 21 deletions weather-process/main.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
package main

import (
"encoding/json"
"fmt"

weathersubs "github.com/saanvijay/weathernotify/weather-subs"
"net/http"
)

func main() {

location, err := weathersubs.GetCurrentLocation()
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
Host := "localhost"
Port := "8080"

fmt.Println(location)
mux := http.NewServeMux()
mux.HandleFunc("GET /getlocation", GetLocation)
mux.HandleFunc("GET /getforecast/{latitude}/{longitude}", GetForecast)

grid, err := weathersubs.GetGridPoint(location)
endpoint := fmt.Sprintf("%s:%s", Host, Port)
fmt.Printf("Server listening %s\n", endpoint)
err := http.ListenAndServe(endpoint, mux)
if err != nil {
fmt.Printf("Error: %s\n", err)
fmt.Printf("Error starting the server: %s\n", err)
return
}
fmt.Println(string(grid))

forecast, err := weathersubs.GetForeCast(location)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
jsonData, _ := json.MarshalIndent(forecast.Properties.Periods, "", " ")

fmt.Println(string(jsonData))

}
58 changes: 58 additions & 0 deletions weather-process/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"encoding/json"
"fmt"
"net/http"

weathersubs "github.com/saanvijay/weathernotify/weather-subs"
)

type Response struct {
Message string `json:"message"`
Status int `json:"status"`
}

func GetLocation(w http.ResponseWriter, r *http.Request) {
location, err := weathersubs.GetCurrentLocation()
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
res := Response{
Message: location,
Status: 200,
}
jsonData, err := json.Marshal(res)
if err != nil {
http.Error(w, "Error converting to JSON", http.StatusInternalServerError)
return
}

fmt.Println(location)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonData)

}

func GetForecast(w http.ResponseWriter, r *http.Request) {
latitude := r.PathValue("latitude")
longitude := r.PathValue("longitude")
location := fmt.Sprintf("%s, %s", latitude, longitude)

fmt.Printf("location is %s\n", location)

forecast, err := weathersubs.GetForeCast(location)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
jsonData, _ := json.MarshalIndent(forecast.Properties.Periods, "", " ")

fmt.Println(string(jsonData))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonData)
}

0 comments on commit 56dfea5

Please sign in to comment.