From 58e428f6a9cbbb411db7a15be646fddf6b240d5c Mon Sep 17 00:00:00 2001 From: Maciej Bowszys Date: Fri, 3 Jul 2026 00:19:04 +0200 Subject: [PATCH] 260703_001904 --- moira_db.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/moira_db.go b/moira_db.go index c506c0c..d93b941 100644 --- a/moira_db.go +++ b/moira_db.go @@ -36,7 +36,7 @@ type Film struct{ Year int } -var db_film []Film +var db_film map[string]any func findURL_in_msg(in_msg string) (_ string,err error){ @@ -200,22 +200,22 @@ func fetch_filmInfo(in_url string) (_ Film, err error){ } } -func hdlr_root(template_renders map[string]string) http.HandlerFunc{ +func hdlr_root(template_renders map[string]struct{Template string;Context *map[string]any}) http.HandlerFunc{ fSrv := http.FileServer(http.Dir("./website/static")) return func (resp http.ResponseWriter, req *http.Request){ if fileName, ok := template_renders[req.URL.Path]; ok{ - fmt.Println("[srv] template", req.URL.Path, fileName) + fmt.Println("[srv] template", req.URL.Path, fileName.Template) url_query := req.URL.Query() url_query_id := url_query.Get("id") var context any if(url_query_id != ""){ - context = db_film[int(url_query_id)] + context = (*fileName.Context)[url_query_id] }else{ context = nil } - err := templates.ExecuteTemplate(resp, fileName, context) + err := templates.ExecuteTemplate(resp, fileName.Template, context) if(err != nil){ http.Error(resp, err.Error(), http.StatusInternalServerError) @@ -228,14 +228,14 @@ func hdlr_root(template_renders map[string]string) http.HandlerFunc{ } } -func templates_register(template_renders map[string]string){ +func templates_register(template_renders map[string]struct{Template string;Context *map[string]any}){ for _,i_file := range template_renders{ - text, err := os.ReadFile("./website/templates/" + i_file) + text, err := os.ReadFile("./website/templates/" + i_file.Template) if(err != nil){ panic(err) } - templates.New(i_file).Parse(string(text)) + templates.New(i_file.Template).Parse(string(text)) } } @@ -250,7 +250,7 @@ func hdlr_film_parseMsg(resp http.ResponseWriter, req *http.Request){ fmt.Println(t_url_noRed) t_film, _ := fetch_filmInfo(t_url_noRed) - db_film = append(db_film, t_film) + db_film[string(len(db_film)-1)] = t_film t_json, _ := json.MarshalIndent(t_film, "", " ") fmt.Printf("%+v\n", string(t_json)) @@ -282,14 +282,14 @@ func main(){ //t_json, _ := json.MarshalIndent(t_film, "", " ") //fmt.Printf("%+v\n", string(t_json)) - template_renders := map[string]string { - "/": "index.html", - "/film/view/": "index.html", - "/film/edit/": "index.html", - //"/list/view/": "index.html", - //"/list/edit/": "index.html", - //"/group/view/": "index.html", - //"/group/edit/": "index.html", + template_renders := map[string]struct{Template string;Context *map[string]any} { + "/": {"index.html", nil}, + "/film/view/": {"index.html", &db_film}, + "/film/edit/": {"index.html", nil}, + //"/list/view/": {"index.html", nil}, + //"/list/edit/": {"index.html", nil}, + //"/group/view/": {"index.html", nil}, + //"/group/edit/": {"index.html", nil}, } templates_register(template_renders)