hidrocor

commit a0e64939cff883a57134b765d773cb3000ff3af1

Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>

all: flesh out serving wikis

 cmd/hidrocor/main.go | 11 ----
 go.mod | 1 
 go.sum | 2 
 main.go | 121 ++++++++++++++++++++++++++++++++++++++++++++++


diff --git a/cmd/hidrocor/main.go b/cmd/hidrocor/main.go
deleted file mode 100644
index 600b70488b1dbd749eb74e997fa950e64c627142..0000000000000000000000000000000000000000
--- a/cmd/hidrocor/main.go
+++ /dev/null
@@ -1,11 +0,0 @@
-package main
-
-import (
-	"log"
-	"github.com/yuin/goldmark"
-	"gopkg.in/yaml.v2"
-)
-
-func main() {
-
-}




diff --git a/go.mod b/go.mod
index e60c1483ac2c2477f6cf69dc7a1911e3ae95b186..9f8b79e8b486d3f16dda3524ba615c331f784e3e 100644
--- a/go.mod
+++ b/go.mod
@@ -3,6 +3,7 @@
 go 1.16
 
 require (
+	github.com/go-chi/chi/v5 v5.0.4 // indirect
 	github.com/yuin/goldmark v1.4.0 // indirect
 	gopkg.in/yaml.v2 v2.4.0 // indirect
 )




diff --git a/go.sum b/go.sum
index 97deb17d7d3f672a737a2286812ddc2f7c853d50..2c8ebe6ff5ab5124d20daf60711a361759070027 100644
--- a/go.sum
+++ b/go.sum
@@ -1,3 +1,5 @@
+github.com/go-chi/chi/v5 v5.0.4 h1:5e494iHzsYBiyXQAHHuI4tyJS9M3V84OuX3ufIIGHFo=
+github.com/go-chi/chi/v5 v5.0.4/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
 github.com/yuin/goldmark v1.4.0 h1:OtISOGfH6sOWa1/qXqqAiOIAO6Z5J3AEAE18WAq6BiQ=
 github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=




diff --git a/main.go b/main.go
new file mode 100644
index 0000000000000000000000000000000000000000..46e22384fb6a89092378eac6dc5d6c3ea833c4df
--- /dev/null
+++ b/main.go
@@ -0,0 +1,121 @@
+package main
+
+import (
+	"bytes"
+	"log"
+	"net/http"
+	"os"
+	"path"
+
+	"github.com/go-chi/chi/v5"
+	"github.com/yuin/goldmark"
+	"github.com/yuin/goldmark/extension"
+	"github.com/yuin/goldmark/parser"
+	"github.com/yuin/goldmark/renderer/html"
+)
+
+func requestError(msg string, w http.ResponseWriter) {
+	log.Printf("500 on " + msg)
+	w.WriteHeader(500)
+	w.Write([]byte("500 Internal Server Error \n"))
+}
+
+func main() {
+	wikiPath := "/home/eletrotupi/sources/cirandas.net-docs"
+
+	md := goldmark.New(
+		goldmark.WithExtensions(
+			extension.GFM,
+			extension.Linkify,
+			extension.Table,
+			extension.TaskList,
+			extension.DefinitionList,
+			extension.Strikethrough,
+			extension.Footnote,
+		),
+		goldmark.WithParserOptions(
+			parser.WithAutoHeadingID(),
+		),
+		goldmark.WithRendererOptions(
+			html.WithHardWraps(),
+		),
+	)
+
+	router := chi.NewRouter()
+	router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
+		var (
+			buf    bytes.Buffer
+			source []byte
+		)
+
+		route := chi.URLParam(r, "*")
+
+		// TODO: Move this into a context and pass this along with the main
+		// template
+		if route == "" {
+			route = "README.md"
+		}
+
+		routePath := path.Join(wikiPath, route)
+		fileInfo, err := os.Stat(routePath)
+
+		if err != nil {
+			requestError("On Stating File", w)
+
+			return
+		}
+
+		if fileInfo.IsDir() {
+			files, err := os.ReadDir(routePath)
+			if err != nil {
+				requestError("On Reading Dir", w)
+
+				return
+			}
+
+			for _, file := range files {
+				log.Printf("fileName: ", file.Name())
+
+				switch file.Name() {
+				case
+					"index.md",
+					"INDEX.md",
+					"readme.md",
+					"README.md":
+					source, err = os.ReadFile(path.Join(routePath, file.Name()))
+
+					if err != nil {
+						requestError("On Opening file inside folder", w)
+
+						return
+					}
+				}
+			}
+		} else {
+			log.Printf("Not dir")
+			source, err = os.ReadFile(routePath)
+
+			if err != nil {
+				requestError("On Opening file", w)
+
+				return
+			}
+		}
+
+		if err := md.Convert(source, &buf); err != nil {
+			log.Printf("500 on Converting Markdown")
+			w.WriteHeader(500)
+			w.Write([]byte("500 Internal Server Error \n"))
+
+			return
+		}
+
+		// XXX: How to deal with images? Also move the charset to the HTML
+		// template
+		w.Header().Add("Content-Type", "text/html; charset=utf-8")
+		w.WriteHeader(200)
+		w.Write(buf.Bytes())
+	})
+
+	http.ListenAndServe(":3000", router)
+}