personal-website/main.go

95 lines
2.4 KiB
Go

package main
import (
"context"
"errors"
"log"
"os"
"personalwebsite/apps"
"personalwebsite/routes"
"personalwebsite/wde"
"personalwebsite/webfilesystem"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
if err := godotenv.Load(); err != nil {
println("No .env file found")
}
mongoConnect, err := FindEnv("MONGO_CONNECT")
if err != nil {
panic(err.Error())
}
dBName, err := FindEnv("DATABASE")
if err != nil {
panic(err.Error())
}
webFsCollection, err := FindEnv("COLLECTION_WEBFS")
if err != nil {
panic(err.Error())
}
// publicPort, err := FindEnv("PUBLIC_PORT")
// if err != nil {
// panic(err.Error())
// }
privatePort, err := FindEnv("PRIVATE_PORT")
if err != nil {
panic(err.Error())
}
clientOptions := options.Client().ApplyURI(mongoConnect)
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
webfs := webfilesystem.NewWebFileSystem(client, dBName, webFsCollection)
appsStorage := apps.NewApplicationsStorage(map[string]apps.WebDEApplication{}, webfs)
webde := wde.NewWDE(webfs)
//TODO Split to different apps init for private and public?
// persPropsApp := aboutme.NewAboutMeApp(webfs)
// finderApp := finder.NewFinderApplication(webfs)
// imgViewerApp := imgviewer.NewImgViewerApp(webfs)
// blogViewerApp := blogviewer.NewBlogViewerApp(webfs)
// blogWriterApp := blogwriter.NewBlogWriterApp(webfs)
// sunBoardApp := sunboard.NewSunboardApp(webfs, webde, appsStorage)
// appsStorage.Apps["personal-properties"] = persPropsApp
// appsStorage.Apps["finder"] = finderApp
// appsStorage.Apps["img-viewer"] = imgViewerApp
// appsStorage.Apps[blogViewerApp.GetAppID()] = blogViewerApp
// appsStorage.Apps["BlogWriter"] = blogWriterApp
// appsStorage.Apps["Sunboard"] = sunBoardApp
// go routes.PublicRoutes(publicPort, webfs, webde, appsStorage)
routes.PrivateRoutes(privatePort, webfs, webde, appsStorage)
}
// TODO to for loop with aal vars in slice
func FindEnv(parameter string) (string, error) {
path, exists := os.LookupEnv(parameter)
if exists {
println("[ENV] Requsted " + parameter + " = " + path) //FIXME Only in Debug log
return path, nil
} else {
panic("[ENV] Requsted " + parameter + " not found")
return "", errors.New("env parameter not found")
}
}