From 682ec9cd1498f58151bef765e1247fed690ab252 Mon Sep 17 00:00:00 2001 From: Physcik Date: Fri, 26 Dec 2025 16:29:44 +0500 Subject: Render start --- src/Render/Render.go | 36 ++++++++++++++++++++++++++++-------- src/Render/Templates.go | 33 +++++++++++++++++++++++++++++++++ src/Settings.json | 3 ++- src/Settings/Settings.go | 2 ++ 4 files changed, 65 insertions(+), 9 deletions(-) (limited to 'src') 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", } -- cgit v1.3