From c1ba99bb77acbcfd0ea3dc831e3432bcb99ee51e Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Wed, 24 May 2023 02:19:21 +0300 Subject: [PATCH] Routes now get ports number from .env --- .env | 4 +- main.go | 123 +++++----------------------------------------- routes/private.go | 5 +- routes/public.go | 4 +- 4 files changed, 20 insertions(+), 116 deletions(-) diff --git a/.env b/.env index 7134cee..6037dff 100644 --- a/.env +++ b/.env @@ -1,3 +1,5 @@ MONGO_CONNECT=mongodb://localhost:27017 DATABASE=personal-website -COLLECTION_WEBFS=webfs2 \ No newline at end of file +COLLECTION_WEBFS=webfs2 +PUBLIC_PORT=7070 +PRIVATE_PORT=8080 \ No newline at end of file diff --git a/main.go b/main.go index fabc3ff..c7b63e1 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,16 @@ func main() { 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 { @@ -53,16 +63,6 @@ func main() { webfs := webfilesystem.NewWebFileSystem(client, dBName, webFsCollection) appsStorage := apps.NewApplicationsStorage(map[string]apps.WebDEApplication{}, webfs) - // 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{}) - // }) webde := wde.NewWDE(webfs) @@ -78,109 +78,10 @@ func main() { appsStorage.Apps[blogViewerApp.GetAppID()] = blogViewerApp appsStorage.Apps["BlogWriter"] = blogWriterApp - go routes.PublicRoutes(webfs, webde, appsStorage) - routes.PrivateRoutes(webfs, webde, appsStorage) - - // system := router.Group("system") - // { - // libsGroup := system.Group("libs") - // { - // imgLibGroup := libsGroup.Group("img") - // { - // imgLib := libs.NewImgLib(webfs) - // imgLib.PublicRoutes(imgLibGroup) - // } - - // catLibGroup := libsGroup.Group("cat") - // { - // catLib := libs.NewCatLib(webfs) - // catLib.PublicRoutes(catLibGroup) - // } - - // appsStorageGroup := libsGroup.Group("apps") - // { - // appsStorage.Route(appsStorageGroup) - // } - // } - - // wdeGroup := system.Group("wde") - // { - // routewde.PublicRoutes(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") - // { - // fsGroup := systemGroup.Group("fs") - // { - // webfs.PublicRoutes(fsGroup) - // } - // } - // 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) - // } - // } + go routes.PublicRoutes(publicPort, webfs, webde, appsStorage) + routes.PrivateRoutes(privatePort, webfs, webde, appsStorage) } -// func index(c *gin.Context) { -// c.HTML(http.StatusOK, "base.html", nil) -// } - func FindEnv(parameter string) (string, error) { path, exists := os.LookupEnv(parameter) diff --git a/routes/private.go b/routes/private.go index 1641513..9b81766 100644 --- a/routes/private.go +++ b/routes/private.go @@ -18,7 +18,7 @@ import ( "github.com/thinkerou/favicon" ) -func PrivateRoutes(webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorage *apps.ApplicationsStorage) { +func PrivateRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorage *apps.ApplicationsStorage) { router := gin.New() router.Use(location.Default()) router.Use(favicon.New("./res/dev-fs/wde/icons/ohno.png")) @@ -125,7 +125,8 @@ func PrivateRoutes(webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStora // }, MaxAge: 12 * time.Hour, })) - err := router.Run(":8080") + + err := router.Run(":" + port) if err != nil { log.Panicf("error: %s", err) } diff --git a/routes/public.go b/routes/public.go index 451ceda..443d9e7 100644 --- a/routes/public.go +++ b/routes/public.go @@ -15,7 +15,7 @@ import ( "github.com/thinkerou/favicon" ) -func PublicRoutes(webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorage *apps.ApplicationsStorage) { +func PublicRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorage *apps.ApplicationsStorage) { router := gin.New() router.Use(location.Default()) router.LoadHTMLGlob("templates/**/*") @@ -96,7 +96,7 @@ func PublicRoutes(webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorag } } - err := router.Run(":7070") + err := router.Run(":" + port) if err != nil { log.Panicf("error: %s", err) }