Changing from js to golang

This commit is contained in:
2024-01-29 14:55:20 +01:00
parent 30d8ca73da
commit f4a2251178
40 changed files with 1174 additions and 438 deletions

View File

@@ -2,11 +2,12 @@ package server
import (
"fmt"
"github.com/gorilla/mux"
"gitlab.com/kbr4/svevijesti/internal/database"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"gitlab.com/kbr4/svevijesti/internal/database"
)
func rootHandler(wr http.ResponseWriter, req *http.Request) {

View File

@@ -0,0 +1,34 @@
package server
import (
"net/http"
"time"
"gitlab.com/kbr4/svevijesti/internal/database"
"gitlab.com/kbr4/svevijesti/internal/model"
)
func categoryHandler(wr http.ResponseWriter, r *http.Request) {
store, err := database.Connect()
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
}
articles, err := database.ArticleByCategory(store, time.Now())
articlesByCategory := make(map[string][]model.DisplayArticle)
for _, article := range articles {
articlesByCategory[article.Category] = append(articlesByCategory[article.Category], article)
}
for category, articles := range articlesByCategory {
data := map[string]interface{}{"Category": category, "Articles": articles}
err := templates.ExecuteTemplate(wr, "categoryHTML", data)
if err != nil {
panic(err)
}
}
}

View File

@@ -2,11 +2,12 @@ package server
import (
"fmt"
"github.com/gorilla/mux"
"html/template"
"io/ioutil"
"path/filepath"
"strings"
"github.com/gorilla/mux"
)
var tPath = "./web/tpl/"
@@ -42,5 +43,7 @@ func CreateRoutes() *mux.Router {
r.HandleFunc("/dan/{date}", dailyArticlesHandler)
r.HandleFunc("/{id:[0-9]+}/{slug}", articleHandler)
r.HandleFunc("/", rootHandler)
r.HandleFunc("/weather", WeatherHandler)
r.HandleFunc("/{category}", categoryHandler)
return r
}

View File

@@ -0,0 +1,97 @@
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const apiKey = "abb35e21bdcbad6d1b00141a2b25cf5a"
type WeatherData struct {
Coord struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
} `json:"coord"`
Weather []struct {
Description string `json:"description"`
Icon string `json:"icon"`
} `json:"weather"`
Main struct {
Temp float64 `json:"temp"`
FellsLike float64 `json:"fells_like"`
Preassure int `json:"preassure"`
Humidity int `json:"humidity"`
TempMin float64 `json:"temp_min"`
TempMax float64 `json:"temp_max"`
} `json:"main"`
Wind struct {
Speed float64 `json:"speed"`
Deg float64 `json:"deg"`
} `json:"wind"`
Clouds struct {
All int `json:"all"`
} `json:"clouds"`
Name string `json:"name"`
}
func getWeather(city string) (WeatherData, error) {
url := fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric&lang=hr", city, apiKey)
resp, err := http.Get(url)
if err != nil {
return WeatherData{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return WeatherData{}, err
}
var weatherData WeatherData
err = json.Unmarshal(body, &weatherData)
if err != nil {
return WeatherData{}, err
}
return weatherData, nil
}
func WeatherHandler(w http.ResponseWriter, r *http.Request) {
cities := []string{"Sarajevo", "Banja Luka", "Zenica", "Tuzla", "Mostar"}
var weatherInfo []WeatherData
for _, city := range cities {
data, err := getWeather(city)
if err != nil {
fmt.Printf("Error fetching weather for %s: %v\n", city, err)
continue
}
weatherInfo = append(weatherInfo, data)
}
title := "Vremenska Prognoza"
data := map[string]interface{}{
"title": title,
"weatherInfo": weatherInfo,
}
err := templates.ExecuteTemplate(w, "fullweatherHTML", 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
}
}
}