personal-website/main.go
2023-05-16 02:04:16 +03:00

192 lines
4.5 KiB
Go

package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"time"
"personalwebsite/apps"
"personalwebsite/apps/blogviewer"
"personalwebsite/apps/finder"
imgviewer "personalwebsite/apps/img-viewer"
"personalwebsite/apps/personalprops"
"personalwebsite/libs"
"personalwebsite/routewde"
"personalwebsite/wde"
"personalwebsite/webfilesystem"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/location"
"github.com/gin-gonic/gin"
"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())
}
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)
}
router := gin.New()
router.Use(location.Default())
router.LoadHTMLGlob("templates/**/*")
router.Static("/res", "resources")
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.GET("/", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "index.tmpl", gin.H{})
})
webfs := webfilesystem.NewWebFileSystem(client, dBName, webFsCollection)
webde := wde.NewWDE(webfs)
persPropsApp := personalprops.NewPersPropsApp(webfs)
// finderApp := finder.FinderApplication{}
finderApp := finder.NewFinderApplication(webfs)
imgViewerApp := imgviewer.NewImgViewerApp(webfs)
blogViewerApp := blogviewer.NewBlogViewerApp(webfs)
appsStorage := apps.NewApplicationsStorage(map[string]apps.WebDEApplication{}, webfs)
appsStorage.Apps["personal-properties"] = &persPropsApp
appsStorage.Apps["finder"] = finderApp
appsStorage.Apps["img-viewer"] = &imgViewerApp
appsStorage.Apps["blog-viewer"] = blogViewerApp
system := router.Group("system")
{
libsGroup := system.Group("libs")
{
imgLibGroup := libsGroup.Group("img")
{
imgLib := libs.NewImgLib(webfs)
imgLib.Route(imgLibGroup)
}
catLibGroup := libsGroup.Group("cat")
{
catLib := libs.NewCatLib(webfs)
catLib.Route(catLibGroup)
}
appsStorageGroup := libsGroup.Group("apps")
{
appsStorage.Route(appsStorageGroup)
}
}
wdeGroup := system.Group("wde")
{
routewde.Route(wdeGroup, webde)
}
apps := system.Group("applications") //TODO to libs
{
apps.GET("/:appid/:method", func(ctx *gin.Context) {
appId := ctx.Param("appid")
method := ctx.Param("method")
app, isExist := appsStorage.Apps[appId]
if !isExist {
ctx.Status(http.StatusNoContent)
return
}
switch method {
case "getmanifest":
ctx.JSON(http.StatusOK, app.GetManifest())
case "app.js":
ctx.File("resources/sys/" + appId + "/" + appId + ".js")
case "app.css":
ctx.File("resources/sys/" + appId + "/" + appId + ".css")
default:
ctx.Status(http.StatusBadRequest)
}
})
}
}
fs := router.Group("fs")
{
webfs.Route(fs)
}
app := router.Group("app")
{
persPropApp := app.Group("AboutMe")
{
persPropsApp.Route(persPropApp)
}
finderAppRoute := app.Group("Finder")
{
finderApp.Routes(finderAppRoute)
}
imgViewerRoute := app.Group("img-viewer")
{
imgViewerApp.Route(imgViewerRoute)
}
blogViewerRoute := app.Group("blog-viewer")
{
blogViewerApp.Route(blogViewerRoute)
}
router.Use(cors.New(cors.Config{
AllowAllOrigins: true,
// AllowOrigins: []string{"http://localhost:8080", "http://localhost:9090"},
// AllowMethods: []string{"PUT", "PATCH"},
// AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
// AllowOriginFunc: func(origin string) bool {
// return origin == "https://github.com"
// },
MaxAge: 12 * time.Hour,
}))
err = router.Run(":8080")
if err != nil {
log.Panicf("error: %s", err)
}
}
}
// func index(c *gin.Context) {
// c.HTML(http.StatusOK, "base.html", nil)
// }
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")
}
}