mirror of
https://github.com/Sosokker/openweathermap-dashboard.git
synced 2025-12-18 21:54:05 +01:00
fix: change method to enable cors
This commit is contained in:
parent
7eccf2e462
commit
525cccf344
@ -2,4 +2,4 @@ module github.com/Sosokker/openweather-dashboard
|
|||||||
|
|
||||||
go 1.23.5
|
go 1.23.5
|
||||||
|
|
||||||
require github.com/joho/godotenv v1.5.1
|
require github.com/rs/cors v1.11.1
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
||||||
|
|||||||
34
cmd/main.go
34
cmd/main.go
@ -11,7 +11,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/rs/cors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Coordinate struct {
|
type Coordinate struct {
|
||||||
@ -56,15 +56,8 @@ func (d *DataEntry) normalizeRainPerHour(min, max float32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadEnv() string {
|
func loadEnv() string {
|
||||||
apiKeys, err := godotenv.Read()
|
apiKey := os.Getenv("OPENWEATHERMAP_API_KEY")
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Error: can't load environment or .env file")
|
|
||||||
}
|
|
||||||
|
|
||||||
apiKey, exist := apiKeys["OPENWEATHERMAP_API_KEY"]
|
|
||||||
if !exist {
|
|
||||||
log.Fatalln("Error: OPENWEATHERMAP_API_KEY does not existed!")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(apiKey) == "" {
|
if strings.TrimSpace(apiKey) == "" {
|
||||||
log.Fatalln("Error: OPENWEATHERMAP_API_KEY can't be empty!")
|
log.Fatalln("Error: OPENWEATHERMAP_API_KEY can't be empty!")
|
||||||
}
|
}
|
||||||
@ -132,20 +125,14 @@ func readCoordData(filepath string, coordCh chan<- Coordinate) {
|
|||||||
close(coordCh)
|
close(coordCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.stackhawk.com/blog/golang-cors-guide-what-it-is-and-how-to-enable-it/
|
|
||||||
func enableCors(w *http.ResponseWriter) {
|
|
||||||
(*w).Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
}
|
|
||||||
|
|
||||||
// lat, lon, rain+place
|
// lat, lon, rain+place
|
||||||
// state-id, rainfall (scale 100)
|
// state-id, rainfall (scale 100)
|
||||||
|
|
||||||
func rawDataHandler(w http.ResponseWriter, r *http.Request) {
|
func rawDataHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
enableCors(&w)
|
|
||||||
apiKey := loadEnv()
|
apiKey := loadEnv()
|
||||||
|
|
||||||
coordCh := make(chan Coordinate)
|
coordCh := make(chan Coordinate)
|
||||||
go readCoordData("data/data.csv", coordCh)
|
go readCoordData("data/test.csv", coordCh)
|
||||||
|
|
||||||
var max float32 = 0.0
|
var max float32 = 0.0
|
||||||
var min float32 = 10000000.0
|
var min float32 = 10000000.0
|
||||||
@ -178,6 +165,17 @@ func rawDataHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/api/data", rawDataHandler) // /api/data?scale=100
|
// https://www.stackhawk.com/blog/golang-cors-guide-what-it-is-and-how-to-enable-it/
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/api/data", rawDataHandler) // /api/data?scale=100
|
||||||
|
|
||||||
|
handler := cors.Default().Handler(mux)
|
||||||
|
c := cors.New(cors.Options{
|
||||||
|
AllowedOrigins: []string{"*"},
|
||||||
|
AllowCredentials: true,
|
||||||
|
AllowedHeaders: []string{"Authorization", "Content-Type", "Access-Control-Allow-Origin"},
|
||||||
|
AllowedMethods: []string{"GET", "UPDATE", "PUT", "POST", "DELETE"},
|
||||||
|
})
|
||||||
|
handler = c.Handler(handler)
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", handler))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user