Browse Source

got the embedded file option working, filesystem html and cdn files are not working properly for some reason though

aeth 10 tháng trước cách đây
mục cha
commit
2e30121a1e

+ 32 - 57
cmd/webserver/webserver.go

@@ -4,8 +4,10 @@ import (
 	"database/sql"
 	"flag"
 	"fmt"
+	"io/fs"
 	"log"
 	"os"
+	"path"
 
 	"github.com/gin-contrib/multitemplate"
 	"github.com/gin-gonic/gin"
@@ -21,82 +23,56 @@ import (
 var DOMAIN_NAME string
 
 func main() {
-	htmlSrc := flag.String("html-src", "", "Force the server to serve embedded content, for production use")
+	embedPtr := flag.Bool("embed", false, "Force the server to serve embedded content, for production use")
+	fsPtr := flag.Bool("fs", false, "Force the server to serve embedded content, for production use")
+	envPtr := flag.String("env", ".env", "pass specific ..env file to the program startup")
 	flag.Parse()
-	args := os.Args
-	err := env.LoadAndVerifyEnv(args[1], env.REQUIRED_VARS)
+	err := env.LoadAndVerifyEnv(*envPtr, env.REQUIRED_VARS)
 	if err != nil {
 		log.Fatal("Error when loading env file: ", err)
 	}
 	REDIS_PORT := os.Getenv("REDIS_PORT")
 	REDIS_ADDR := os.Getenv("REDIS_ADDR")
 	var srcOpt webpages.ServiceOption
-	if *htmlSrc == "filesystem" {
-		srcOpt = webpages.FILESYSTEM
-	}
-	if *htmlSrc == "embed" {
+	var htmlReader fs.FS
+	if *embedPtr == true {
 		srcOpt = webpages.EMBED
 	}
-	fmt.Println(srcOpt, *htmlSrc)
-	// htmlReader := webpages.NewContentLayer(webpages.ServiceOption(webpages.FILESYSTEM))
-	htmlReader := webpages.FilesystemWebpages{Webroot: os.Getenv("WEB_ROOT")}
+	if *fsPtr == true {
+		srcOpt = webpages.FILESYSTEM
+	}
+	htmlReader = webpages.NewContentLayer(srcOpt)
 	renderer := multitemplate.NewDynamic()
-	renderer.AddFromString(
-		"head",
-		webpages.ReadToString(htmlReader, "head.html"),
-	)
-	renderer.AddFromString(
-		"navigation",
-		webpages.ReadToString(htmlReader, "navigation.html"),
-	)
-	renderer.AddFromString(
+	templateNames := []string{
 		"home",
-		webpages.ReadToString(htmlReader, "home.html"),
-	)
-	renderer.AddFromString(
 		"blogpost",
-		webpages.ReadToString(htmlReader, "blogpost.html"),
-	)
-	renderer.AddFromString(
 		"digital_art",
-		webpages.ReadToString(htmlReader, "digital_art.html"),
-	)
-	renderer.AddFromString(
 		"login",
-		webpages.ReadToString(htmlReader, "login.html"),
-	)
-	renderer.AddFromString(
 		"admin",
-		webpages.ReadToString(htmlReader, "admin.html"),
-	)
-	renderer.AddFromString(
 		"blogpost_editor",
-		webpages.ReadToString(htmlReader, "blogpost_editor.html"),
-	)
-	renderer.AddFromString(
 		"new_blogpost",
-		webpages.ReadToString(htmlReader, "new_blogpost.html"),
-	)
-	renderer.AddFromString(
-		"upload_status",
-		webpages.ReadToString(htmlReader, "upload_status.html"),
-	)
-	renderer.AddFromString(
 		"unhandled_error",
-		webpages.ReadToString(htmlReader, "unhandled_error.html"),
-	)
-	renderer.AddFromString(
 		"upload",
-		webpages.ReadToString(htmlReader, "upload.html"),
-	)
-	renderer.AddFromString(
+		"upload_status",
 		"writing",
-		webpages.ReadToString(htmlReader, "writing.html"),
-	)
-	renderer.AddFromString(
 		"listing",
-		webpages.ReadToString(htmlReader, "listing.html"),
-	)
+	}
+	if srcOpt == webpages.FILESYSTEM {
+		for i := range templateNames {
+			name := templateNames[i]
+			filePath := path.Join(os.Getenv("WEB_ROOT"), "html", fmt.Sprintf("%s.html", name))
+			fmt.Println(filePath)
+			renderer.AddFromFiles(name, filePath)
+		}
+	} else {
+		for i := range templateNames {
+			name := templateNames[i]
+			renderer.AddFromString(
+				name,
+				webpages.ReadToString(htmlReader, path.Join("html", name+".html")),
+			)
+		}
+	}
 	e := gin.Default()
 	dbfile := "sqlite.db"
 	db, err := sql.Open("sqlite3", dbfile)
@@ -104,13 +80,12 @@ func main() {
 		log.Fatal(err)
 	}
 	e.HTMLRender = renderer
-	// 	e.LoadHTMLGlob("pkg/webpages/html/*.html")
 	webserverDb := helpers.NewSQLiteRepo(db)
 	err = webserverDb.Migrate()
 	if err != nil {
 		log.Fatal(err)
 	}
-	routes.Register(e, DOMAIN_NAME, REDIS_PORT, REDIS_ADDR, webserverDb)
+	routes.Register(e, DOMAIN_NAME, REDIS_PORT, REDIS_ADDR, webserverDb, htmlReader)
 	if os.Getenv("SSL_MODE") == "ON" {
 		e.RunTLS(fmt.Sprintf("%s:%s", os.Getenv("HOST_ADDR"), os.Getenv("HOST_PORT")),
 			os.Getenv(env.CHAIN), os.Getenv(env.KEY))

+ 17 - 9
pkg/controller/admin_handlers.go

@@ -9,7 +9,6 @@ import (
 
 const AUTH_COOKIE_NAME = "X-Server-Auth"
 
-
 // @Name ServeLogin
 // @Summary serves the HTML login page
 // @Tags admin
@@ -49,13 +48,19 @@ func (c *Controller) Auth(ctx *gin.Context) {
 		return
 	}
 	ctx.SetCookie(AUTH_COOKIE_NAME, cookie, 3600, "/", c.Domain, false, false)
-	ctx.Header("HX-Redirect", "/admin/panel")
 
-}
+	ctx.HTML(http.StatusOK, "admin", gin.H{
+		"navigation": gin.H{
+			"headers": c.database.GetNavBarLinks(),
+			"menu":    c.database.GetDropdownElements(),
+		},
+		"Tables": c.database.GetAdminTables().Tables,
+	})
 
+}
 
 /*
-@Name AddAdminTableEntry 
+@Name AddAdminTableEntry
 @Summary add an entry to the admin table
 @Tags admin
 @Router /admin/panel
@@ -93,13 +98,15 @@ func (c *Controller) AddAdminTableEntry(ctx *gin.Context) {
 */
 func (c *Controller) AddMenuItem(ctx *gin.Context) {
 	var item helpers.MenuLinkPair
-	err := ctx.ShouldBind(&item); if err != nil {
+	err := ctx.ShouldBind(&item)
+	if err != nil {
 		ctx.JSON(400, map[string]string{
 			"Error": err.Error(),
 		})
 		return
 	}
-	err = c.database.AddMenuItem(item); if err != nil {
+	err = c.database.AddMenuItem(item)
+	if err != nil {
 		ctx.JSON(400, map[string]string{
 			"Error": err.Error(),
 		})
@@ -117,13 +124,15 @@ func (c *Controller) AddMenuItem(ctx *gin.Context) {
 func (c *Controller) AddNavbarItem(ctx *gin.Context) {
 
 	var item helpers.NavBarItem
-	err := ctx.ShouldBind(&item); if err != nil {
+	err := ctx.ShouldBind(&item)
+	if err != nil {
 		ctx.JSON(400, map[string]string{
 			"Error": err.Error(),
 		})
 		return
 	}
-	err = c.database.AddNavbarItem(item); if err != nil {
+	err = c.database.AddNavbarItem(item)
+	if err != nil {
 		ctx.JSON(400, map[string]string{
 			"Error": err.Error(),
 		})
@@ -132,7 +141,6 @@ func (c *Controller) AddNavbarItem(ctx *gin.Context) {
 	ctx.Data(200, "text", []byte("navbar item added."))
 }
 
-
 // @Name AdminPanel
 // @Summary serve the admin panel page
 // @Tags admin

+ 11 - 1
pkg/controller/cdn_handlers.go

@@ -2,6 +2,7 @@ package controller
 
 import (
 	"fmt"
+	"io"
 	"net/http"
 	"os"
 	"path"
@@ -82,7 +83,7 @@ func (c *Controller) ServeGeneric(ctx *gin.Context) {
 	default:
 		ctype = "text"
 	}
-	b, err := os.ReadFile(path.Join("html", f))
+	fh, err := c.FileIO.Open(path.Join("cdn", f))
 	if err != nil {
 		ctx.JSON(500, map[string]string{
 			"Error": "Could not serve the requested file",
@@ -90,5 +91,14 @@ func (c *Controller) ServeGeneric(ctx *gin.Context) {
 		})
 		return
 	}
+	b, err := io.ReadAll(fh)
+	if err != nil {
+		ctx.JSON(500, map[string]string{
+			"Error": "Could not serve the requested file",
+			"msg":   err.Error(),
+		})
+		return
+
+	}
 	ctx.Data(200, ctype, b)
 }

+ 5 - 1
pkg/controller/controller.go

@@ -1,6 +1,8 @@
 package controller
 
 import (
+	"io/fs"
+
 	"git.aetherial.dev/aeth/keiji/pkg/helpers"
 )
 
@@ -9,14 +11,16 @@ type Controller struct {
 	database    helpers.DocumentIO
 	RedisConfig helpers.RedisConf
 	Cache       *helpers.AllCache
+	FileIO      fs.FS
 }
 
-func NewController(domain string, redisPort string, redisAddr string, database helpers.DocumentIO) *Controller {
+func NewController(domain string, redisPort string, redisAddr string, database helpers.DocumentIO, files fs.FS) *Controller {
 	return &Controller{Cache: helpers.NewCache(),
 		Domain: domain, RedisConfig: helpers.RedisConf{
 			Port: redisPort,
 			Addr: redisAddr,
 		},
 		database: database,
+		FileIO:   files,
 	}
 }

+ 4 - 2
pkg/routes/register.go

@@ -1,13 +1,15 @@
 package routes
 
 import (
+	"io/fs"
+
 	"git.aetherial.dev/aeth/keiji/pkg/controller"
 	"git.aetherial.dev/aeth/keiji/pkg/helpers"
 	"github.com/gin-gonic/gin"
 )
 
-func Register(e *gin.Engine, domain string, redisPort string, redisAddr string, database helpers.DocumentIO) {
-	c := controller.NewController(domain, redisPort, redisAddr, database)
+func Register(e *gin.Engine, domain string, redisPort string, redisAddr string, database helpers.DocumentIO, files fs.FS) {
+	c := controller.NewController(domain, redisPort, redisAddr, database, files)
 	web := e.Group("")
 	web.GET("/", c.ServeHome)
 	web.GET("/blog", c.ServeBlog)

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 5 - 0
pkg/webpages/cdn/bootstrap.bundle.min.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
pkg/webpages/cdn/bootstrap.bundle.min.js.map


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 5 - 0
pkg/webpages/cdn/bootstrap.min.css


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
pkg/webpages/cdn/bootstrap.min.css.map


+ 62 - 0
pkg/webpages/cdn/custom.css

@@ -0,0 +1,62 @@
+ /* The side navigation menu */
+ .sidenav {
+    height: 100%; /* 100% Full-height */
+    width: 0; /* 0 width - change this with JavaScript */
+    position: fixed; /* Stay in place */
+    z-index: 1; /* Stay on top */
+    top: 0; /* Stay at the top */
+    left: 0;
+    background-color: #111; /* Black*/
+    overflow-x: hidden; /* Disable horizontal scroll */
+    padding-top: 60px; /* Place content 60px from the top */
+    transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
+  }
+
+  /* The navigation menu links */
+  .sidenav a {
+    padding: 8px 8px 8px 32px;
+    text-decoration: none;
+    font-size: 25px;
+    color: #818181;
+    display: block;
+    transition: 0.3s;
+
+  }
+
+/*This is modifying the btn-primary colors but you could create your own .btn-something class as well*/
+.btn-primary {
+  color: #fff;
+  background-color: rgba(38, 41, 50, 0.9);
+  border-color: #9dadbb; /*set the color you want here*/
+}
+.btn-primary:hover, .btn-primary:focus, .btn-primary:active, .btn-primary.active, .open>.dropdown-toggle.btn-primary {
+  color: #8a3b3b;
+  background-color: rgba(58, 20, 20, 0.9);
+  border-color: rgb(142, 40, 40); /*set the color you want here*/
+}
+
+
+  /* When you mouse over the navigation links, change their color */
+  .sidenav a:hover {
+    color: #f1f1f1;
+  }
+
+  /* Position and style the close button (top right corner) */
+  .sidenav .closebtn {
+    position: absolute;
+    top: 0;
+    right: 25px;
+    font-size: 36px;
+    margin-left: 50px;
+  }
+
+  /* Style page content - use this if you want to push the page content to the right when you open the side navigation */
+  #main {
+    transition: margin-left .5s;
+  }
+
+  /* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
+  @media screen and (max-height: 450px) {
+    .sidenav {padding-top: 15px;}
+    .sidenav a {font-size: 18px;}
+  }

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
pkg/webpages/cdn/htmx.min.js


+ 12 - 0
pkg/webpages/cdn/json-enc.js

@@ -0,0 +1,12 @@
+htmx.defineExtension('json-enc', {
+    onEvent: function (name, evt) {
+        if (name === "htmx:configRequest") {
+            evt.detail.headers['Content-Type'] = "application/json";
+        }
+    },
+    
+    encodeParameters : function(xhr, parameters, elt) {
+        xhr.overrideMimeType('text/json');
+        return (JSON.stringify(parameters));
+    }
+});

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 18 - 0
pkg/webpages/cdn/mdb.min.css


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
pkg/webpages/cdn/mdb.min.css.map


+ 13 - 0
pkg/webpages/cdn/slide.js

@@ -0,0 +1,13 @@
+/* Set the width of the side navigation to 250px and the left margin of the page content to 250px */
+function toggleNav() {
+  if (document.getElementById("mySidenav").style.width === "400px"){
+      document.getElementById("mySidenav").style.width = "0px";
+      document.getElementById("main").style.marginLeft = "0px";
+      return;
+  } else {
+      document.getElementById("mySidenav").style.width = "400px";
+      document.getElementById("main").style.marginLeft = "400px";
+      return;
+  }
+}
+

+ 12 - 4
pkg/webpages/pages.go

@@ -11,7 +11,7 @@ import (
 	"path"
 )
 
-//go:embed html
+//go:embed html cdn
 var content embed.FS
 
 type ServiceOption string
@@ -26,10 +26,12 @@ Creates the new filesystem implementer for serving the webpages to the API
 */
 func NewContentLayer(opt ServiceOption) fs.FS {
 	if opt == EMBED {
+		fmt.Println("Using embed files to pull html templates")
 		return content
 	}
 	if opt == FILESYSTEM {
-		fmt.Println(os.Getenv("WEB_ROOT"))
+		fmt.Println("Using filesystem to pull html templates")
+
 		return FilesystemWebpages{Webroot: path.Base(os.Getenv("WEB_ROOT"))}
 	}
 	log.Fatal("Unknown option was passed: ", opt)
@@ -49,8 +51,14 @@ type FilesystemWebpages struct {
 Implementing the io.FS interface for interoperability
 */
 func (f FilesystemWebpages) Open(file string) (fs.File, error) {
-	fmt.Println(path.Join(f.Webroot, file))
-	return os.Open(path.Join(f.Webroot, file))
+	filePath := path.Join(os.Getenv("WEB_ROOT"), file)
+	fmt.Println(filePath)
+	fh, err := os.Open(filePath)
+	if err != nil {
+		fmt.Printf("Error opening the file: %s because %s", filePath, err)
+		return nil, err
+	}
+	return fh, nil
 }
 
 /*

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác