summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPhyscik <mynameisgennadiy@vk.com>2025-12-26 16:29:44 +0500
committerPhyscik <mynameisgennadiy@vk.com>2025-12-26 16:29:44 +0500
commit682ec9cd1498f58151bef765e1247fed690ab252 (patch)
treee1b7a52f59ac18a178475839bb71e6accd1e8b08 /src
parent39f7384dfc295bd80bfe8514335930cfdf6d85a0 (diff)
Render start
Diffstat (limited to 'src')
-rw-r--r--src/Render/Render.go36
-rw-r--r--src/Render/Templates.go33
-rw-r--r--src/Settings.json3
-rw-r--r--src/Settings/Settings.go2
4 files changed, 65 insertions, 9 deletions
diff --git a/src/Render/Render.go b/src/Render/Render.go
index f9b702b..bdaa51e 100644
--- a/src/Render/Render.go
+++ b/src/Render/Render.go
@@ -9,10 +9,19 @@ import (
settings "physick.ru/Settings"
)
-const indexTemplateName string = "index.html"
+type IndexTemplateContents struct {
+ LastArticles []recentArticle
+ Content string
+}
+
+type recentArticle struct {
+ Link string
+ DisplayName string
+}
func RegisterEndpoints() {
// http.Handle("static/", http.FileServer(http.FS(os.DirFS(settings.Current.StaticLocation))))
+ http.HandleFunc("/articles/{name}", test)
http.HandleFunc("/", mux)
}
@@ -26,16 +35,27 @@ func mux(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filePath)
return
}
- render(w, r)
+ common(w, r)
}
-func render(w http.ResponseWriter, r *http.Request) {
- var indexTempl, readErr = getTemplateString(indexTemplateName)
- if readErr != nil {
- slog.Warn("Failed to load template", slog.Any("Error", readErr))
+func common(w http.ResponseWriter, r *http.Request) {
+ var indexArticle, indexErr = getTemplate("index.html")
+ if indexErr != nil {
+ slog.Error("Failed to parse the template", slog.Any("Error", indexErr))
w.WriteHeader(http.StatusInternalServerError)
return
}
- // slog.Info("Requested a template")
- w.Write([]byte(indexTempl))
+ executeIndexTemplate(IndexTemplateContents{
+ LastArticles: []recentArticle {
+ recentArticle {
+ Link: "/",
+ DisplayName: "Index page",
+ },
+ },
+ Content: indexArticle,
+ }, w)
+}
+
+func test(w http.ResponseWriter, r *http.Request) {
+ fmt.Fprint(w, "xdx")
}
diff --git a/src/Render/Templates.go b/src/Render/Templates.go
index 544a2ac..ab24878 100644
--- a/src/Render/Templates.go
+++ b/src/Render/Templates.go
@@ -2,11 +2,16 @@ package render
import (
"fmt"
+ "log/slog"
+ "net/http"
"os"
+ "text/template"
settings "physick.ru/Settings"
)
+const indexTemplateName string = "index.html"
+
func getTemplateString(templName string) (string, error) {
var fullPath = fmt.Sprintf("%s/%s", settings.Current.TemplatesLocation, templName)
var file, fopenerr = os.ReadFile(fullPath)
@@ -15,3 +20,31 @@ func getTemplateString(templName string) (string, error) {
}
return string(file), nil
}
+
+func executeIndexTemplate(content IndexTemplateContents, w http.ResponseWriter) {
+ var indexTempl, readErr = getTemplateString(indexTemplateName)
+ if readErr != nil {
+ slog.Error("Failed to load template", slog.Any("Error", readErr))
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ var templ, templErr = template.New("index").Parse(indexTempl)
+ if templErr != nil {
+ slog.Error("Failed to parse the template", slog.Any("Error", templErr))
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ var err = templ.Execute(w, content)
+ if err != nil {
+ slog.Error("Failed to execute the template", slog.Any("Error", err))
+ }
+}
+
+func getTemplate(templName string) (string, error) {
+ var fullPath = fmt.Sprintf("%s/%s", settings.Current.ArticlesLocation, templName)
+ var file, fopenerr = os.ReadFile(fullPath)
+ if fopenerr != nil {
+ return "", fopenerr
+ }
+ return string(file), nil
+}
diff --git a/src/Settings.json b/src/Settings.json
index c614305..acd7ab3 100644
--- a/src/Settings.json
+++ b/src/Settings.json
@@ -1,5 +1,6 @@
{
"Port": ":7000",
"StaticLocation": "../static",
- "TemplatesLocation": "../templates"
+ "TemplatesLocation": "../templates",
+ "ArticlesLocation": "../articles"
}
diff --git a/src/Settings/Settings.go b/src/Settings/Settings.go
index 58daed3..50df787 100644
--- a/src/Settings/Settings.go
+++ b/src/Settings/Settings.go
@@ -11,6 +11,7 @@ type Settings struct {
Port string
StaticLocation string
TemplatesLocation string
+ ArticlesLocation string
}
const settingsLocation = "Settings.json"
@@ -19,6 +20,7 @@ var defaultSettings = Settings {
Port: ":6969",
StaticLocation: "../static",
TemplatesLocation: "../templates",
+ ArticlesLocation: "../articles",
}