Skip to content

Commit

Permalink
Authenticate backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeetii committed Oct 6, 2024
1 parent de097ea commit 2ecf46f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 141 deletions.
28 changes: 0 additions & 28 deletions backend/functions/airviro-webcam.go

This file was deleted.

41 changes: 41 additions & 0 deletions backend/functions/fetchWebcam.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
8 changes: 7 additions & 1 deletion backend/functions/fetchWebcams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions backend/functions/skistar-webcam.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/PuerkitoBio/goquery"
)

type Input struct {
type SkistarInput struct {
WebcamId string
Location []float64
}
Expand All @@ -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}},
Expand Down
8 changes: 5 additions & 3 deletions backend/functions/trafikverket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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",
}
Expand Down
12 changes: 11 additions & 1 deletion backend/lib/cloudStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
108 changes: 2 additions & 106 deletions frontend/src/lib/components/Map.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,13 @@
}, 600);
};
type WebcamData = {
imageUrl: string;
coordinates: number[];
};
interface Webcam {
url: string
location: Feature<Geometry, GeoJsonProperties>
}
function addWebcamsToMap(map: maplibregl.Map, webcams: WebcamData[]) {
function addWebcamsToMap(map: maplibregl.Map) {
fetch("http://localhost:8080/listFiles")
.then((response) => response.json())
.then((data: Webcam[]) => {
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 2ecf46f

Please sign in to comment.