Cody Joyce 2 months ago
parent
commit
b5bab5a268
16 changed files with 75 additions and 46 deletions
  1. 4 1
      .gitignore
  2. 9 2
      gen/layout/main.go
  3. 2 2
      gen/main.go
  4. 9 2
      gen/partial/partial.go
  5. 0 0
      sec/base64/base64url.go
  6. 1 1
      sec/jwt/jwt.go
  7. 10 2
      sec/middleware.go
  8. 0 0
      sec/sec.go
  9. 16 10
      sec/sess.go
  10. 0 7
      sess/config.go
  11. 0 11
      sess/middleware.go
  12. 14 1
      srv/req.go
  13. 0 0
      srv/res.go
  14. 4 4
      srv/rtr.go
  15. 4 1
      srv/srv.go
  16. 2 2
      tpl/tpl.go

+ 4 - 1
.gitignore

@@ -1 +1,4 @@
-tmp
+tmp
+app
+main.go
+.air.toml

+ 9 - 2
gen/layout/main.go

@@ -3,12 +3,14 @@ package layout
 import (
 	"fmt"
 	"os"
+	"path"
 	"strings"
 
 	"git.clearsky.net.au/cody/gex.git/gen/domquery"
 )
 
-func ProcessHTML(htmlStr string) (string, error) {
+func ProcessHTML(htmlStr string, fp string) (string, error) {
+
 	dom := domquery.LoadHTML(htmlStr)
 	layoutTag := dom.QuerySelector("layout")
 
@@ -18,7 +20,12 @@ func ProcessHTML(htmlStr string) (string, error) {
 
 	attr := layoutTag.GetAttribute("src")
 
+	// Set path
 	attr = strings.TrimPrefix(attr, "/")
+	if !strings.Contains(attr, "app/") {
+		dirPath := path.Dir(fp)
+		attr = dirPath + "/" + attr
+	}
 
 	attr = strings.Replace(attr, ".tpl", ".gen.tpl", 1)
 
@@ -32,7 +39,7 @@ func ProcessHTML(htmlStr string) (string, error) {
 	blocks := layoutTag.QuerySelectorAll("block")
 
 	layoutFileHtml := string(layoutFileByt)
-	layoutFileHtml, err = ProcessHTML(layoutFileHtml)
+	layoutFileHtml, err = ProcessHTML(layoutFileHtml, fp)
 	if err != nil {
 		fmt.Printf("ERROR: %s\n", err)
 	}

+ 2 - 2
gen/main.go

@@ -128,7 +128,7 @@ func processPartials(dir string) error {
 		}
 		htmlStr := string(data)
 
-		htmlStr, err = partial.ProcessHTML(htmlStr)
+		htmlStr, err = partial.ProcessHTML(htmlStr, fp)
 		if err != nil {
 			fmt.Println("\n" + fp)
 			return err
@@ -168,7 +168,7 @@ func processLayout(dir string) error {
 		}
 		htmlStr := string(data)
 
-		htmlStr, err = layout.ProcessHTML(htmlStr)
+		htmlStr, err = layout.ProcessHTML(htmlStr, fp)
 		if err != nil {
 			fmt.Println("\n" + fp)
 			return err

+ 9 - 2
gen/partial/partial.go

@@ -3,12 +3,13 @@ package partial
 import (
 	"fmt"
 	"os"
+	"path"
 	"strings"
 
 	"git.clearsky.net.au/cody/gex.git/gen/domquery"
 )
 
-func ProcessHTML(htmlStr string) (string, error) {
+func ProcessHTML(htmlStr string, fp string) (string, error) {
 	dom := domquery.LoadHTML(htmlStr)
 
 	partTag := dom.QuerySelector("part")
@@ -18,7 +19,13 @@ func ProcessHTML(htmlStr string) (string, error) {
 	}
 
 	attr := partTag.GetAttribute("src")
+
+	// Set path
 	attr = strings.TrimPrefix(attr, "/")
+	if !strings.Contains(attr, "app/") {
+		dirPath := path.Dir(fp)
+		attr = dirPath + "/" + attr
+	}
 	attr = strings.Replace(attr, ".tpl", ".gen.tpl", 1)
 
 	partFileByt, err := os.ReadFile(attr)
@@ -35,7 +42,7 @@ func ProcessHTML(htmlStr string) (string, error) {
 	}
 	partTag.SetOuterHTML(partFileDom.InnerHTML())
 
-	htmlStr, err = ProcessHTML(dom.InnerHTML())
+	htmlStr, err = ProcessHTML(dom.InnerHTML(), fp)
 	if err != nil {
 		fmt.Printf("ERROR: %s", err)
 		return "", err

+ 0 - 0
sess/base64/base64url.go → sec/base64/base64url.go


+ 1 - 1
sess/jwt/jwt.go → sec/jwt/jwt.go

@@ -8,7 +8,7 @@ import (
 	"fmt"
 	"strings"
 
-	"git.clearsky.net.au/cody/gex.git/sess/base64"
+	"git.clearsky.net.au/cody/gex.git/sec/base64"
 )
 
 // encode json bytes to a jwt token string

+ 10 - 2
sec/middleware.go

@@ -1,20 +1,28 @@
 package sec
 
 import (
-	"git.clearsky.net.au/cody/gex.git/sess"
 	"git.clearsky.net.au/cody/gex.git/srv"
 )
 
 func Middleware(req *srv.Req, res srv.Res) bool {
+	//Session
+
+	var sess Sess
+	sess.Construct(req, res)
+	sess.Save()
+	req.Ctx["Sess"] = sess
+
 	pattern := req.Pattern
 
+	// Route Access Check
+
 	// Cancel the security check as there are no permissions for this route
 	if len(permissions[pattern]) == 0 {
 		return true
 	}
 
 	handlerRoles := permissions[pattern]
-	sess := req.Ctx["Sess"].(sess.Sess)
+	sess = req.Ctx["Sess"].(Sess)
 
 	for _, role := range handlerRoles {
 		if sess.HasRole(role) {

+ 0 - 0
sec/secure.go → sec/sec.go


+ 16 - 10
sess/session.go → sec/sess.go

@@ -1,10 +1,10 @@
-package sess
+package sec
 
 import (
 	"encoding/json"
 	"fmt"
 
-	"git.clearsky.net.au/cody/gex.git/sess/jwt"
+	"git.clearsky.net.au/cody/gex.git/sec/jwt"
 	"git.clearsky.net.au/cody/gex.git/srv"
 
 	"time"
@@ -17,16 +17,12 @@ type Sess struct {
 	User_name string
 	Roles     []string
 	Expires   time.Time
+	Props     map[string]any
 }
 
-func (sess *Sess) HasRole(roleName string) bool {
-	for _, role := range sess.Roles {
-		if role == roleName {
-			return true
-		}
-	}
-	return false
-}
+var TokenName string = "GexToken"
+var Expires time.Time = time.Now().Add(24 * time.Hour)
+var Secret string = "secret"
 
 func (sess *Sess) setDefaults() {
 	sess.User_id = 0
@@ -38,6 +34,7 @@ func (sess *Sess) setDefaults() {
 func (sess *Sess) Construct(req *srv.Req, res srv.Res) {
 	sess.req = req
 	sess.res = res
+	sess.Props = make(map[string]any)
 	sess.setDefaults()
 
 	// check cookie is valid (not expired too)
@@ -86,6 +83,15 @@ func (sess *Sess) Token() (string, error) {
 	return token, nil
 }
 
+func (sess *Sess) HasRole(roleName string) bool {
+	for _, role := range sess.Roles {
+		if role == roleName {
+			return true
+		}
+	}
+	return false
+}
+
 // Saves token to cookie
 func (sess *Sess) Save() {
 	// get existing session or create new one

+ 0 - 7
sess/config.go

@@ -1,7 +0,0 @@
-package sess
-
-import "time"
-
-var TokenName string = "GexToken"
-var Expires time.Time = time.Now().Add(24 * time.Hour)
-var Secret string = "secret"

+ 0 - 11
sess/middleware.go

@@ -1,11 +0,0 @@
-package sess
-
-import "git.clearsky.net.au/cody/gex.git/srv"
-
-func Middleware(req *srv.Req, res srv.Res) bool {
-	var sess Sess
-	sess.Construct(req, res)
-	sess.Save()
-	req.Ctx["Sess"] = sess
-	return true
-}

+ 14 - 1
srv/request.go → srv/req.go

@@ -15,6 +15,7 @@ type Req struct {
 }
 
 func (req *Req) Construct(r *http.Request) {
+	r.ParseForm()
 	req.r = r
 	req.AppURL = "/" + r.Host
 	req.Path = r.RequestURI
@@ -23,9 +24,21 @@ func (req *Req) Construct(r *http.Request) {
 	req.Ctx = make(map[string]any)
 }
 
+func (req *Req) Body(key string) string {
+	return req.r.FormValue(key)
+}
+
+func (req *Req) Query(key string) string {
+	return req.r.URL.Query().Get(key)
+}
+
+func (req *Req) Param(key string) string {
+	return req.r.PathValue(key)
+}
+
 // The route pattern eg: /user
 func (req *Req) getPattern() string {
-	_, pattern := router.Handler(req.r)
+	_, pattern := rtr.Handler(req.r)
 	return pattern
 }
 

+ 0 - 0
srv/response.go → srv/res.go


+ 4 - 4
srv/router.go → srv/rtr.go

@@ -4,7 +4,7 @@ import (
 	"net/http"
 )
 
-var router *http.ServeMux
+var rtr *http.ServeMux
 
 /*
 Our Special HandleFunc
@@ -31,13 +31,13 @@ func Route(pattern string, handler func(req *Req, res Res)) {
 
 	}
 
-	router.HandleFunc(pattern, realHandler)
+	rtr.HandleFunc(pattern, realHandler)
 }
 
 func InitRouter() {
-	router = http.NewServeMux()
+	rtr = http.NewServeMux()
 
 	// Public Files
 	fs := http.FileServer(http.Dir("app/pub"))
-	router.Handle("GET /app/pub/", http.StripPrefix("/app/pub/", fs))
+	rtr.Handle("GET /app/pub/", http.StripPrefix("/app/pub/", fs))
 }

+ 4 - 1
srv/server.go → srv/srv.go

@@ -7,12 +7,15 @@ import (
 	"os"
 )
 
+var Config map[string]string
+
 func StartServer() {
 	var PORT string = os.Args[1]
 
+	Config = make(map[string]string)
 	server := http.Server{
 		Addr:    fmt.Sprintf(":%v", PORT),
-		Handler: router,
+		Handler: rtr,
 	}
 
 	fmt.Println("Server Running")

+ 2 - 2
render/render.go → tpl/tpl.go

@@ -1,4 +1,4 @@
-package render
+package tpl
 
 import (
 	"bytes"
@@ -17,7 +17,7 @@ func include(req *srv.Req, props Props, funcs Funcs) {}
 
 var Include = include
 
-func Template(fp string, req *srv.Req, propFunc ...interface{}) string {
+func Render(fp string, req *srv.Req, propFunc ...interface{}) string {
 	if !strings.Contains(fp, "app/") {
 		fp = utils.Cwd(2) + "/" + fp
 	}