~poldi1405/gomod-recter

v0.2.0 3 years ago

tag v0.2.0
Tagger: Moritz Poldrack <ich@moritz-poldrack.de>
Date:   Thu Nov 25 00:38:50 2021 +0100

tag v0.2.0
Tagger: Moritz Poldrack <ich@moritz-poldrack.de>
Date:   Wed Nov 24 23:27:45 2021 +0100

asset handling, data fetching, and themes

added prepared themes
added automatic updates of packages
added asset handling

commit e28afde441d34fa10d78d91ba9a0b87ef990aac3
Author: Moritz Poldrack <ich@moritz-poldrack.de>
Date:   Wed Nov 24 23:27:21 2021 +0100

    added asset handling

diff --git a/internal/handler/assets.go b/internal/handler/assets.go
new file mode 100644
index 0000000..ff5be2e
--- /dev/null
+++ b/internal/handler/assets.go
@@ -0,0 +1,31 @@
+package handler
+
+import (
+	"io"
+	"mime"
+	"os"
+	"path"
+	"strings"
+
+	"git.sr.ht/~poldi1405/glog"
+	"github.com/spf13/viper"
+	"github.com/valyala/fasthttp"
+)
+
+func assetHandler(ctx *fasthttp.RequestCtx) {
+	filepath := viper.GetString("Directories.AssetDir") + strings.TrimPrefix(string(ctx.Path()), "/assets")
+
+	glog.Debugf("serving asset: %s", filepath)
+	fh, err := os.Open(filepath)
+	if err != nil {
+		ctx.SetStatusCode(fasthttp.StatusNotFound)
+		return
+	}
+	ctx.SetContentType(mime.TypeByExtension(path.Ext(filepath)))
+	_, err = io.Copy(ctx.Response.BodyWriter(), fh)
+	if err != nil {
+		ctx.SetStatusCode(fasthttp.StatusInternalServerError)
+		return
+	}
+	return
+}
diff --git a/internal/handler/handler.go b/internal/handler/handler.go
index be25afa..3a72e8d 100644
--- a/internal/handler/handler.go
+++ b/internal/handler/handler.go
@@ -30,12 +30,15 @@ func FasthttpHandler(ctx *fasthttp.RequestCtx) {
 		return
 	}

-	if name := viper.GetString("Projects." + string(path[1]) + ".Name"); name != "" {
+	name := viper.GetString("Projects." + string(path[1]) + ".Name")
+	switch {
+	case bytes.Compare(path[1], []byte("assets")) == 0:
+		assetHandler(ctx)
+	case name != "":
 		remainingPath := ctx.Path()[len(path[1])+1:]
 		projectHandler(ctx, string(path[1]), remainingPath)
-	} else {
+	default:
 		glog.Warnf("project '%s' not found in config", path[1])
-		ctx.SetStatusCode(fasthttp.StatusNotFound)
 		ctx.Redirect("/", fasthttp.StatusSeeOther)
 	}
 }

commit 132f4ca3600ccdb00fb3faa9b0b50ea7b1c7bf37
Author: Moritz Poldrack <ich@moritz-poldrack.de>
Date:   Wed Nov 24 23:27:21 2021 +0100

    added asset handling

diff --git a/internal/handler/assets.go b/internal/handler/assets.go
new file mode 100644
index 0000000..ff5be2e
--- /dev/null
+++ b/internal/handler/assets.go
@@ -0,0 +1,31 @@
+package handler
+
+import (
+	"io"
+	"mime"
+	"os"
+	"path"
+	"strings"
+
+	"git.sr.ht/~poldi1405/glog"
+	"github.com/spf13/viper"
+	"github.com/valyala/fasthttp"
+)
+
+func assetHandler(ctx *fasthttp.RequestCtx) {
+	filepath := viper.GetString("Directories.AssetDir") + strings.TrimPrefix(string(ctx.Path()), "/assets")
+
+	glog.Debugf("serving asset: %s", filepath)
+	fh, err := os.Open(filepath)
+	if err != nil {
+		ctx.SetStatusCode(fasthttp.StatusNotFound)
+		return
+	}
+	ctx.SetContentType(mime.TypeByExtension(path.Ext(filepath)))
+	_, err = io.Copy(ctx.Response.BodyWriter(), fh)
+	if err != nil {
+		ctx.SetStatusCode(fasthttp.StatusInternalServerError)
+		return
+	}
+	return
+}
diff --git a/internal/handler/handler.go b/internal/handler/handler.go
index be25afa..3a72e8d 100644
--- a/internal/handler/handler.go
+++ b/internal/handler/handler.go
@@ -30,12 +30,15 @@ func FasthttpHandler(ctx *fasthttp.RequestCtx) {
 		return
 	}

-	if name := viper.GetString("Projects." + string(path[1]) + ".Name"); name != "" {
+	name := viper.GetString("Projects." + string(path[1]) + ".Name")
+	switch {
+	case bytes.Compare(path[1], []byte("assets")) == 0:
+		assetHandler(ctx)
+	case name != "":
 		remainingPath := ctx.Path()[len(path[1])+1:]
 		projectHandler(ctx, string(path[1]), remainingPath)
-	} else {
+	default:
 		glog.Warnf("project '%s' not found in config", path[1])
-		ctx.SetStatusCode(fasthttp.StatusNotFound)
 		ctx.Redirect("/", fasthttp.StatusSeeOther)
 	}
 }
Do not follow this link