personal-website/main.go

215 lines
5.1 KiB
Go
Raw Normal View History

2023-03-15 00:05:24 +00:00
package main
import (
2023-04-25 09:37:42 +00:00
"context"
"errors"
2023-05-01 12:32:41 +00:00
"fmt"
2023-03-15 00:05:24 +00:00
"log"
"net/http"
2023-04-25 09:37:42 +00:00
"os"
2023-05-02 00:12:43 +00:00
2023-05-03 21:50:08 +00:00
"personalwebsite/apps"
"personalwebsite/apps/blogviewer"
"personalwebsite/apps/finder"
imgviewer "personalwebsite/apps/img-viewer"
"personalwebsite/apps/personalprops"
2023-03-17 01:16:51 +00:00
"personalwebsite/routewde"
2023-04-29 13:58:39 +00:00
"personalwebsite/wde"
2023-04-25 09:37:42 +00:00
"personalwebsite/webfilesystem"
2023-03-15 00:05:24 +00:00
2023-05-02 00:59:11 +00:00
"github.com/gin-contrib/location"
2023-03-15 00:05:24 +00:00
"github.com/gin-gonic/gin"
2023-04-25 09:37:42 +00:00
"github.com/joho/godotenv"
2023-04-29 10:17:25 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-04-25 09:37:42 +00:00
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
2023-03-15 00:05:24 +00:00
)
func main() {
2023-04-25 09:37:42 +00:00
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()
2023-05-02 00:59:11 +00:00
router.Use(location.Default())
2023-03-18 00:34:56 +00:00
router.LoadHTMLGlob("templates/**/*")
2023-03-15 00:05:24 +00:00
router.Static("/res", "resources")
2023-05-01 12:32:41 +00:00
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
2023-03-15 12:32:41 +00:00
2023-03-18 00:34:56 +00:00
router.GET("/", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "index.tmpl", gin.H{})
2023-03-15 00:05:24 +00:00
})
2023-03-18 00:34:56 +00:00
2023-04-25 09:37:42 +00:00
webfs := webfilesystem.NewWebFileSystem(client, dBName, webFsCollection)
2023-05-01 12:32:41 +00:00
webde := wde.NewWDE(webfs)
2023-05-01 12:32:41 +00:00
persPropsApp := personalprops.NewPersPropsApp(webfs)
2023-04-29 13:58:39 +00:00
// finderApp := finder.FinderApplication{}
finderApp := finder.NewFinderApplication(webfs)
2023-04-29 16:47:12 +00:00
imgViewerApp := imgviewer.NewImgViewerApp(webfs)
2023-04-29 12:02:25 +00:00
blogViewerApp := blogviewer.NewBlogViewerApp(webfs)
2023-05-03 21:50:08 +00:00
appsStorage := apps.ApplicationsStorage{
Apps: map[string]apps.WebDEApplication{},
2023-03-17 01:16:51 +00:00
}
appsStorage.Apps["personal-properties"] = &persPropsApp
2023-04-29 13:58:39 +00:00
appsStorage.Apps["finder"] = finderApp
2023-04-12 21:05:23 +00:00
appsStorage.Apps["img-viewer"] = &imgViewerApp
2023-04-14 15:53:23 +00:00
appsStorage.Apps["blog-viewer"] = blogViewerApp
2023-05-01 12:32:41 +00:00
2023-05-02 00:12:43 +00:00
router.POST("/upload", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
2023-05-01 12:32:41 +00:00
// single file
2023-05-02 00:12:43 +00:00
file, _ := ctx.FormFile("file")
2023-05-03 19:50:43 +00:00
if file == nil {
ctx.String(http.StatusBadRequest, "file is nil")
}
2023-05-02 00:12:43 +00:00
// generateMins := c.Param("generateMins")
2023-05-01 12:32:41 +00:00
// log.Println(file.Filename)
// Upload the file to specific dst.
dst := "./test-img/" + file.Filename
2023-05-02 00:12:43 +00:00
ctx.SaveUploadedFile(file, dst)
2023-05-01 12:32:41 +00:00
2023-05-02 00:12:43 +00:00
err := webfs.UploadFile(dst, path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
return
}
2023-05-01 12:32:41 +00:00
2023-05-02 00:12:43 +00:00
// webFsCollection.CreateMiniatures("./test-img/", file.Filename)
2023-05-01 12:32:41 +00:00
2023-05-02 00:12:43 +00:00
// webfs.CreateFile(&img, "/home/user/")
2023-05-01 12:32:41 +00:00
2023-05-02 00:12:43 +00:00
ctx.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
2023-05-01 12:32:41 +00:00
})
2023-03-17 01:16:51 +00:00
system := router.Group("system")
2023-03-15 12:32:41 +00:00
{
2023-05-02 00:12:43 +00:00
libsGroup := system.Group("libs")
2023-05-01 12:32:41 +00:00
{
2023-05-02 00:12:43 +00:00
imgLibGroup := libsGroup.Group("img")
{
imgLibGroup.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
file, err := webfs.Read(path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
}
2023-05-03 19:50:43 +00:00
data := file.Data.(primitive.Binary)
2023-05-02 00:12:43 +00:00
2023-05-03 19:50:43 +00:00
ctx.Data(http.StatusOK, "image/jpeg", data.Data)
2023-05-02 00:12:43 +00:00
})
}
2023-05-01 12:32:41 +00:00
}
2023-04-29 13:58:39 +00:00
wdeGroup := system.Group("wde")
2023-03-17 01:16:51 +00:00
{
2023-05-01 12:32:41 +00:00
routewde.Route(wdeGroup, webde)
2023-03-17 01:16:51 +00:00
}
2023-05-03 21:50:08 +00:00
// apps := system.Group("applications")
// {
// 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)
// }
// })
// }
// apps.Route(apps.Group("/storage"), &appsStorage)
2023-03-15 12:32:41 +00:00
}
2023-03-17 01:16:51 +00:00
2023-04-25 09:37:42 +00:00
fs := router.Group("fs")
{
2023-05-03 21:50:08 +00:00
webfs.Route(fs)
2023-04-25 09:37:42 +00:00
}
2023-03-17 01:16:51 +00:00
app := router.Group("application")
{
persPropApp := app.Group("personal-properties")
{
2023-05-03 21:50:08 +00:00
persPropsApp.Route(persPropApp)
2023-03-17 01:16:51 +00:00
}
2023-03-22 18:00:04 +00:00
finderAppRoute := app.Group("finder")
{
2023-05-03 21:50:08 +00:00
finderApp.Routes(finderAppRoute)
2023-03-22 18:00:04 +00:00
}
2023-04-12 21:05:23 +00:00
imgViewerRoute := app.Group("img-viewer")
{
2023-05-03 21:50:08 +00:00
imgViewerApp.Route(imgViewerRoute)
2023-04-12 21:05:23 +00:00
}
2023-04-14 15:53:23 +00:00
blogViewerRoute := app.Group("blog-viewer")
{
2023-05-03 21:50:08 +00:00
blogViewerApp.Route(blogViewerRoute)
2023-04-14 15:53:23 +00:00
}
2023-03-15 00:05:24 +00:00
2023-05-03 21:50:08 +00:00
err = router.Run(":8080")
if err != nil {
log.Panicf("error: %s", err)
}
2023-03-15 00:05:24 +00:00
}
}
2023-05-03 21:50:08 +00:00
// func index(c *gin.Context) {
// c.HTML(http.StatusOK, "base.html", nil)
// }
2023-04-25 09:37:42 +00:00
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")
}
}