Skip to content

Commit

Permalink
Fixed issues with functions on firebase hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
srgtuszy committed Oct 21, 2024
1 parent 61b5d8c commit 94b5464
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion functions/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { onCall } = require("firebase-functions/v2/https");
admin.initializeApp();

const db = admin.firestore();
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });

exports.generateTips = functions.https.onRequest(async (req, res) => {
exports.triggerGenerateTips = onCall({cors: true}, async (req, res) => {
console.log("Generating tips");
console.log(await db.collection("electricity").get());
const measurements = await fetchMeasurements();
Expand Down
5 changes: 3 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Future<void> main() async {
FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);
}

FirebaseFunctions.instance;
FirebaseFirestore.instance.databaseId = "measurements";
runApp(const ElectricityAssistantApp());
}
Expand Down Expand Up @@ -125,8 +126,8 @@ class _HomePageState extends State<HomePage> {

try {
final HttpsCallable callable =
FirebaseFunctions.instance.httpsCallable('generateTips');
final HttpsCallableResult result = await callable();
FirebaseFunctions.instance.httpsCallable('triggerGenerateTips');
final result = await callable.call();
print('Function result: ${result.data}');
_showSuccessSnackBar('Tips generated successfully');
} catch (e) {
Expand Down
23 changes: 23 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>

<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.0.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/11.0.0/firebase-analytics.js";
import { getFunctions } from "https://www.gstatic.com/firebasejs/11.0.0/firebase-functions.js";

// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyCrOboisE3bcKQFdtcLXAXbK-UdTFwLI_o",
authDomain: "electricity-assistant-fb2f7.firebaseapp.com",
projectId: "electricity-assistant-fb2f7",
storageBucket: "electricity-assistant-fb2f7.appspot.com",
messagingSenderId: "278136132490",
appId: "1:278136132490:web:a88532c443f237b005ee73",
measurementId: "G-6N4TEWVZKY"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const functions = getFunctions(app);
</script>

<title>electricity_assistant</title>
<link rel="manifest" href="manifest.json">
</head>
Expand Down

0 comments on commit 94b5464

Please sign in to comment.