44 lines
1.4 KiB
Java
44 lines
1.4 KiB
Java
package io.kamax.mxisd.backend.firebase;
|
|
|
|
import com.google.auth.oauth2.GoogleCredentials;
|
|
import com.google.firebase.FirebaseApp;
|
|
import com.google.firebase.FirebaseOptions;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
|
|
public abstract class GoogleFirebaseBackend {
|
|
protected boolean enabled;
|
|
protected String backendName;
|
|
protected String credentialsPath;
|
|
protected String databaseUrl;
|
|
|
|
public GoogleFirebaseBackend(boolean isEnabled, String backendName, String credsPath, String db) {
|
|
this.enabled = isEnabled;
|
|
this.backendName = backendName;
|
|
this.credentialsPath = credsPath;
|
|
this.databaseUrl = db;
|
|
if (isEnabled) {
|
|
try {
|
|
initializeFirebase();
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("Failed to initialize Firebase", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void initializeFirebase() throws IOException {
|
|
FileInputStream serviceAccount = new FileInputStream(credentialsPath);
|
|
|
|
FirebaseOptions options = new FirebaseOptions.Builder()
|
|
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
|
|
.setDatabaseUrl(databaseUrl)
|
|
.build();
|
|
|
|
if (FirebaseApp.getApps().isEmpty()) { // Check if Firebase has been initialized already
|
|
FirebaseApp.initializeApp(options);
|
|
}
|
|
}
|
|
|
|
// Additional methods for GoogleFirebaseBackend
|
|
}
|