From 2ecf46f6a44beb786949bac7565c36dfd5f3349a Mon Sep 17 00:00:00 2001 From: Erik Magnusson Date: Sun, 6 Oct 2024 22:43:50 +0200 Subject: [PATCH] Authenticate backend --- backend/functions/airviro-webcam.go | 28 ------- backend/functions/fetchWebcam.go | 41 ++++++++++ backend/functions/fetchWebcams.go | 8 +- backend/functions/skistar-webcam.go | 4 +- backend/functions/trafikverket.go | 8 +- backend/lib/cloudStore.go | 12 ++- frontend/src/lib/components/Map.svelte | 108 +------------------------ 7 files changed, 68 insertions(+), 141 deletions(-) delete mode 100644 backend/functions/airviro-webcam.go create mode 100644 backend/functions/fetchWebcam.go diff --git a/backend/functions/airviro-webcam.go b/backend/functions/airviro-webcam.go deleted file mode 100644 index e2535b6..0000000 --- a/backend/functions/airviro-webcam.go +++ /dev/null @@ -1,28 +0,0 @@ -package functions - -import ( - "fmt" - "net/http" - - "github.com/Yeetii/live-weather/lib" - - "github.com/GoogleCloudPlatform/functions-framework-go/functions" -) - -func init() { - functions.HTTP("fetchAirviroWebcams", collectAirviroWebcams) -} - -func collectAirviroWebcams(w http.ResponseWriter, r *http.Request) { - inputs := []Input{{WebcamId: "borga", Location: []float64{15.03789571840728, 64.84199155484801}}, - {WebcamId: "helags", Location: []float64{12.505582249386759, 62.917014196762445}}, - {WebcamId: "ramundberget", Location: []float64{12.37264481898198, 62.69248269325625}}, - {WebcamId: "bydalen", Location: []float64{13.75263354936005, 63.10759607237622}}, - } - - for _, input := range inputs { - var fileName = fmt.Sprintf("airviro-webcam-%s.jpg", input.WebcamId) - var url = fmt.Sprintf("https://www.airviro.com/%s/webcam/latestimg.jpg", input.WebcamId) - lib.UploadToFirebaseStorage(url, fileName, input.Location) - } -} diff --git a/backend/functions/fetchWebcam.go b/backend/functions/fetchWebcam.go new file mode 100644 index 0000000..efa220f --- /dev/null +++ b/backend/functions/fetchWebcam.go @@ -0,0 +1,41 @@ +package functions + +import ( + "fmt" + "net/http" + + "github.com/Yeetii/live-weather/lib" + + "github.com/GoogleCloudPlatform/functions-framework-go/functions" +) + +type CommonInput struct { + WebcamId string + Location []float64 + ImageUrl string +} + +func init() { + functions.HTTP("fetchWebcams", collectWebcams) +} + +func collectWebcams(w http.ResponseWriter, r *http.Request) { + inputs := []CommonInput{{WebcamId: "borga", Location: []float64{15.03789571840728, 64.84199155484801}, ImageUrl: "https://www.airviro.com/borga/webcam/latestimg.jpg"}, + {WebcamId: "helags", Location: []float64{12.505582249386759, 62.917014196762445}, ImageUrl: "https://www.airviro.com/helags/webcam/latestimg.jpg"}, + {WebcamId: "ramundberget", Location: []float64{12.37264481898198, 62.69248269325625}, ImageUrl: "https://www.airviro.com/ramundberget/webcam/latestimg.jpg"}, + {WebcamId: "bydalen", Location: []float64{13.75263354936005, 63.10759607237622}, ImageUrl: "https://www.airviro.com/bydalen/webcam/latestimg.jpg"}, + {WebcamId: "trillevallen", Location: []float64{13.206262037974694, 63.25443937020817}, ImageUrl: "https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39635528.Jpeg?type=fullsize&maxage=140"}, + {WebcamId: "gevsjön", Location: []float64{12.702011476723557, 63.36705212550013}, ImageUrl: "https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39635384.Jpeg?type=fullsize&maxage=140"}, + {WebcamId: "handöl", Location: []float64{12.382941421804786, 63.26835926669674}, ImageUrl: "https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39635520.Jpeg?type=fullsize&maxage=140"}, + {WebcamId: "medstugan", Location: []float64{12.407996503920517, 63.519546112666376}, ImageUrl: "https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39626819.Jpeg?type=fullsize&maxage=140"}, + {WebcamId: "storlien", Location: []float64{12.088252196720449, 63.31759038262924}, ImageUrl: "https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39636227.Jpeg?type=fullsize&maxage=140"}, + {WebcamId: "nedalshytta", Location: []float64{12.101315126910368, 62.97826646239796}, ImageUrl: "https://metnet.no/custcams/nedalshytta/laget/webcam_hd.jpg"}, + {WebcamId: "meråker", Location: []float64{11.679622045416139, 63.456829044603644}, ImageUrl: "https://metnet.no/custcams/merakeralpin2/laget/webcam_hd.jpg"}, + } + + for _, input := range inputs { + var fileName = fmt.Sprintf("webcam-%s.jpg", input.WebcamId) + var url = fmt.Sprintf(input.ImageUrl) + lib.UploadToFirebaseStorage(url, fileName, input.Location) + } +} diff --git a/backend/functions/fetchWebcams.go b/backend/functions/fetchWebcams.go index ac79b0f..266a4b2 100644 --- a/backend/functions/fetchWebcams.go +++ b/backend/functions/fetchWebcams.go @@ -9,6 +9,7 @@ import ( firebase "firebase.google.com/go" "github.com/GoogleCloudPlatform/functions-framework-go/functions" + "github.com/Yeetii/live-weather/lib" geojson "github.com/paulmach/go.geojson" "google.golang.org/api/iterator" "google.golang.org/api/option" @@ -20,7 +21,12 @@ func init() { var err error ctx := context.Background() conf := &firebase.Config{StorageBucket: "live-weather-eefc5.appspot.com"} - firebaseApp, err = firebase.NewApp(ctx, conf, option.WithCredentialsFile("service-account.json")) + var opt option.ClientOption + if lib.IsLocal() { + opt = option.WithCredentialsFile("service-account.json") + } + + firebaseApp, err = firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalf("error initializing Firebase app: %v", err) } diff --git a/backend/functions/skistar-webcam.go b/backend/functions/skistar-webcam.go index c2774c2..cf27060 100644 --- a/backend/functions/skistar-webcam.go +++ b/backend/functions/skistar-webcam.go @@ -11,7 +11,7 @@ import ( "github.com/PuerkitoBio/goquery" ) -type Input struct { +type SkistarInput struct { WebcamId string Location []float64 } @@ -30,7 +30,7 @@ func init() { // Förberget = 49 func main(w http.ResponseWriter, r *http.Request) { - inputs := []Input{{WebcamId: "46", Location: []float64{13.061854, 63.386158}}, + inputs := []SkistarInput{{WebcamId: "46", Location: []float64{13.061854, 63.386158}}, {WebcamId: "61", Location: []float64{12.97008963763537, 63.410562604101635}}, {WebcamId: "62", Location: []float64{13.063774, 63.436330}}, {WebcamId: "63", Location: []float64{13.112216429602679, 63.40863152963157}}, diff --git a/backend/functions/trafikverket.go b/backend/functions/trafikverket.go index 86cf83e..eaa9a87 100644 --- a/backend/functions/trafikverket.go +++ b/backend/functions/trafikverket.go @@ -13,6 +13,7 @@ import ( firebase "firebase.google.com/go" "github.com/GoogleCloudPlatform/functions-framework-go/functions" + "github.com/Yeetii/live-weather/lib" "google.golang.org/api/option" ) @@ -23,9 +24,10 @@ func init() { // Firebase Function to fetch from Trafikverket API and store in Firestore func FetchAndStoreToFirebase(w http.ResponseWriter, r *http.Request) { ctx := context.Background() - - // Initialize Firebase App - opt := option.WithCredentialsFile("service-account.json") + var opt option.ClientOption + if lib.IsLocal() { + opt = option.WithCredentialsFile("service-account.json") + } config := &firebase.Config{ DatabaseURL: "https://live-weather-eefc5.firebaseio.com", } diff --git a/backend/lib/cloudStore.go b/backend/lib/cloudStore.go index 2bfba95..735c6c7 100644 --- a/backend/lib/cloudStore.go +++ b/backend/lib/cloudStore.go @@ -6,17 +6,27 @@ import ( "io" "log" "net/http" + "os" firebase "firebase.google.com/go" "google.golang.org/api/option" ) +func IsLocal() bool { + // Check if the environment variable 'K_SERVICE' is set + return os.Getenv("K_SERVICE") == "" +} + func UploadToFirebaseStorage(url string, fileName string, location []float64) error { fmt.Println("Uploading image to Firebase Storage...") // Initialize Firebase app ctx := context.Background() conf := &firebase.Config{StorageBucket: "live-weather-eefc5.appspot.com"} - app, err := firebase.NewApp(ctx, conf, option.WithCredentialsFile("service-account.json")) + var opt option.ClientOption + if IsLocal() { + opt = option.WithCredentialsFile("service-account.json") + } + app, err := firebase.NewApp(ctx, conf, opt) if err != nil { return fmt.Errorf("error initializing Firebase app: %w", err) } diff --git a/frontend/src/lib/components/Map.svelte b/frontend/src/lib/components/Map.svelte index ee918da..82bb149 100644 --- a/frontend/src/lib/components/Map.svelte +++ b/frontend/src/lib/components/Map.svelte @@ -38,18 +38,13 @@ }, 600); }; - type WebcamData = { - imageUrl: string; - coordinates: number[]; - }; - interface Webcam { url: string location: Feature } - function addWebcamsToMap(map: maplibregl.Map, webcams: WebcamData[]) { + function addWebcamsToMap(map: maplibregl.Map) { fetch("http://localhost:8080/listFiles") .then((response) => response.json()) .then((data: Webcam[]) => { @@ -109,74 +104,6 @@ }); }); }); - - - webcams.forEach((webcam, index) => { - const imageId = `webcam-image-${index}`; // Unique image ID for each image - - map - .loadImage(webcam.imageUrl) - .then((image) => { - // Add each image with a unique ID - if (!map.hasImage(imageId)) { - map.addImage(imageId, image.data); - } - - // Add GeoJSON source for each webcam - const sourceId = `webcam-point-${index}`; - if (!map.getSource(sourceId)) { - map.addSource(sourceId, { - type: 'geojson', - data: { - type: 'FeatureCollection', - features: [ - { - type: 'Feature', - geometry: { - type: 'Point', - coordinates: webcam.coordinates - }, - properties: { - name: `Webcam ${index + 1}` - } - } - ] - } - }); - } - - console.log(map.getSource(sourceId)) - - // Add a layer to display the image at each webcam's coordinates - const layerId = `webcam-layer-${index}`; - if (!map.getLayer(layerId)) { - map.addLayer({ - id: layerId, - type: 'symbol', - source: sourceId, - layout: { - 'icon-image': imageId, - 'icon-size': [ - 'interpolate', - ['linear'], - ['zoom'], - 0, - 0, // At zoom level 0, size is 0 - 10, - 0.01, // At zoom level 10, size is 0.01 - 22, - 1 // At zoom level 22, size is 1 - ] - } - }); - } - - console.log(map.getLayer(layerId)) - }) - .catch((error: any) => { - console.error(`Error loading image for webcam ${index + 1}:`, error); - }); - }); } onMount(() => { @@ -215,39 +142,8 @@ }); map.on('load', () => { - const webcams = [ - { - imageUrl: - 'https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39635528.Jpeg?type=fullsize&maxage=140', - coordinates: [13.206262037974694, 63.25443937020817] - }, - { - imageUrl: - 'https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39635384.Jpeg?type=fullsize&maxage=140', - coordinates: [12.702011476723557, 63.36705212550013] - }, - { - imageUrl: - 'https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39635520.Jpeg?type=fullsize&maxage=140', - coordinates: [12.382941421804786, 63.26835926669674] - }, - { - imageUrl: - 'https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39626819.Jpeg?type=fullsize&maxage=140', - coordinates: [12.407996503920517, 63.519546112666376] - }, - { - imageUrl: - 'https://api.trafikinfo.trafikverket.se/v2/Images/RoadConditionCamera_39636227.Jpeg?type=fullsize&maxage=140', - coordinates: [12.088252196720449, 63.31759038262924] - }, - { - imageUrl: 'https://metnet.no/custcams/nedalshytta/laget/webcam_hd.jpg', - coordinates: [12.101315126910368, 62.97826646239796] - } - ]; - addWebcamsToMap(map, webcams); + addWebcamsToMap(map); map.addSource('text-source', { type: 'geojson',