Weather and Category
This commit is contained in:
@@ -5,9 +5,27 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
const apiKey = "abb35e21bdcbad6d1b00141a2b25cf5a"
|
||||
var apiKey string
|
||||
|
||||
func init() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
fmt.Println("Error loading .env file:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
apiKey = os.Getenv("API_KEY")
|
||||
|
||||
if apiKey == "" {
|
||||
fmt.Println("API_KEY environment variable not set.")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
type WeatherData struct {
|
||||
Coord struct {
|
||||
@@ -74,24 +92,13 @@ func WeatherHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{
|
||||
"title": title,
|
||||
"weatherInfo": weatherInfo,
|
||||
"categories": CategoryMenu,
|
||||
}
|
||||
|
||||
err := templates.ExecuteTemplate(w, "fullweatherHTML", data)
|
||||
err := templates.ExecuteTemplate(w, "weatherHTML", data)
|
||||
if err != nil {
|
||||
fmt.Println("Error executing template:", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
for _, info := range weatherInfo {
|
||||
widgetData := map[string]interface{}{
|
||||
"Temperature": info.Main.Temp,
|
||||
"City": info.Name,
|
||||
}
|
||||
err := templates.ExecuteTemplate(w, "weatherwidgetHTML", widgetData)
|
||||
if err != nil {
|
||||
fmt.Println("Error executing template:", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user