code refacturing, added render package comments
This commit is contained in:
10
lib/lib.go
10
lib/lib.go
@ -55,9 +55,9 @@ func Map_args(array ...string) map[string]string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// func Map_of_string_to_josn(array map[string]string) string {
|
func Map_of_string_to_josn(array map[string]string) string {
|
||||||
// jData, err := json.Marshal(array)
|
jData, err := json.Marshal(array)
|
||||||
// lib.Handle_err(err, lib.Map_args())
|
Handle_err(err, Map_args())
|
||||||
|
|
||||||
// return string(jData)
|
return string(jData)
|
||||||
// }
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"main":{"body":"message"},
|
"main":{"body":"message"},
|
||||||
"sec":"/api/sec",
|
"sec":"second",
|
||||||
"thi":"/api/thi"
|
"thi":"third"
|
||||||
}
|
}
|
||||||
@ -15,33 +15,24 @@ func Add_api(w http.ResponseWriter, req *http.Request) {
|
|||||||
fmt.Fprintf(w, "good")
|
fmt.Fprintf(w, "good")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Show_apis(w http.ResponseWriter, req *http.Request) {
|
|
||||||
apis := api.Apis()
|
|
||||||
eval := "<div>"
|
|
||||||
for tag, varr := range apis {
|
|
||||||
eval_group := ""
|
|
||||||
eval_group += fmt.Sprintf("%v", varr)
|
|
||||||
eval_group += "-"
|
|
||||||
eval_group += tag
|
|
||||||
eval_group += "<br>"
|
|
||||||
eval += render.Template_of_tag(eval_group, "text")
|
|
||||||
}
|
|
||||||
eval += "</div>"
|
|
||||||
|
|
||||||
fmt.Fprint(w, render.Template_with_string(eval))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Render_apis(w http.ResponseWriter, req *http.Request) {
|
func Render_apis(w http.ResponseWriter, req *http.Request) {
|
||||||
apis := api.Apis()
|
apis := api.Apis()
|
||||||
eval := "<div>"
|
eval := ""
|
||||||
|
i := 0
|
||||||
for tag, varr := range apis {
|
for tag, varr := range apis {
|
||||||
eval_group := ""
|
eval_group := ""
|
||||||
eval_group += tag
|
eval_group += tag
|
||||||
eval_group += "-"
|
eval_group += "<br>-"
|
||||||
eval_group += fmt.Sprintf("%v", varr)
|
eval_group += fmt.Sprintf("%v", varr)
|
||||||
eval_group += "<br>"
|
eval_group += "<br>"
|
||||||
eval += eval_group
|
|
||||||
|
eval += render.Put_vars_to_template(
|
||||||
|
render.Template_of_tag("li"),
|
||||||
|
lib.Map_args("li_id", fmt.Sprint(i), "content", eval_group))
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
eval += "</div>"
|
fmt.Fprint(w,
|
||||||
fmt.Fprint(w, render.Put_vars_to_template(render.Template_with_part("json_out"), lib.Map_args("json_code", eval)))
|
render.Put_vars_to_template(
|
||||||
|
render.Template_of_part("json_out_as_list"),
|
||||||
|
lib.Map_args("li_from_json_code", eval)))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,87 +1,77 @@
|
|||||||
package render
|
package render
|
||||||
|
|
||||||
|
// light renderer package that used for generating html templates
|
||||||
|
// loading the template files from views folder
|
||||||
|
// using part folder as template parts
|
||||||
|
// using tag folder as templates of html tags
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"api_manager/lib"
|
"api_manager/lib"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// loading default head and foot template and adding custom string in the middle
|
||||||
func Template_with_string(str string) string {
|
func Template_with_string(str string) string {
|
||||||
|
// loading head.html and foot.html
|
||||||
head, err := os.ReadFile("web/views/head.html")
|
head, err := os.ReadFile("web/views/head.html")
|
||||||
foot, err := os.ReadFile("web/views/foot.html")
|
foot, err := os.ReadFile("web/views/foot.html")
|
||||||
|
// handling error by lib function and exiting if it occures
|
||||||
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||||
|
|
||||||
|
// combining html parts
|
||||||
template := "" + string(head) + str + string(foot)
|
template := "" + string(head) + str + string(foot)
|
||||||
fmt.Println(err)
|
|
||||||
|
|
||||||
return template
|
return template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loading default head and foot template and adding html template from file in the middle
|
||||||
func Template_with_page(str string) string {
|
func Template_with_page(str string) string {
|
||||||
|
// loading head.html and foot.html
|
||||||
head, err := os.ReadFile("web/views/head.html")
|
head, err := os.ReadFile("web/views/head.html")
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
||||||
foot, err := os.ReadFile("web/views/foot.html")
|
foot, err := os.ReadFile("web/views/foot.html")
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
// loading custom html template file
|
||||||
page, err := os.ReadFile("web/views/" + str + ".html")
|
page, err := os.ReadFile("web/views/" + str + ".html")
|
||||||
|
// handling error by lib function and exiting if it occures
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||||
|
|
||||||
template := "" + string(head) + string(page) + string(foot)
|
// combining html parts
|
||||||
fmt.Println(err)
|
|
||||||
|
|
||||||
return template
|
|
||||||
}
|
|
||||||
|
|
||||||
func Template_with_page_vars(str string, tags_and_variables map[string]string) string {
|
|
||||||
head, err := os.ReadFile("web/views/head.html")
|
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
||||||
foot, err := os.ReadFile("web/views/foot.html")
|
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
||||||
|
|
||||||
page := ""
|
|
||||||
for tag, varr := range tags_and_variables {
|
|
||||||
page = templating(str, tag, varr)
|
|
||||||
}
|
|
||||||
template := "" + string(head) + string(page) + string(foot)
|
template := "" + string(head) + string(page) + string(foot)
|
||||||
|
|
||||||
return template
|
return template
|
||||||
}
|
}
|
||||||
|
|
||||||
func Template_of_tag(str string, html_tag string) string {
|
// loading default head and foot template and adding html template from tag file in the middle
|
||||||
|
func Template_of_tag(html_tag string) string {
|
||||||
|
// loading head.html and foot.html
|
||||||
tag, err := os.ReadFile("web/views/tag/" + html_tag + ".html")
|
tag, err := os.ReadFile("web/views/tag/" + html_tag + ".html")
|
||||||
result := strings.Replace(string(tag), "{{fnr}}", str, 1)
|
// handling error by lib function and exiting if it occures
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||||
|
|
||||||
return result
|
return string(tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Template_with_part(part_name string) string {
|
// loading default head and foot template and adding html template from part file in the middle
|
||||||
|
func Template_of_part(part_name string) string {
|
||||||
|
// loading head.html and foot.html
|
||||||
head, err := os.ReadFile("web/views/head.html")
|
head, err := os.ReadFile("web/views/head.html")
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
||||||
page, err := os.ReadFile("web/views/part/" + part_name + ".html")
|
page, err := os.ReadFile("web/views/part/" + part_name + ".html")
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
||||||
foot, err := os.ReadFile("web/views/foot.html")
|
foot, err := os.ReadFile("web/views/foot.html")
|
||||||
|
// handling error by lib function and exiting if it occures
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||||
|
|
||||||
|
// combining html parts
|
||||||
template := "" + string(head) + string(page) + string(foot)
|
template := "" + string(head) + string(page) + string(foot)
|
||||||
fmt.Println(err)
|
|
||||||
|
|
||||||
return template
|
return template
|
||||||
}
|
}
|
||||||
|
|
||||||
func templating(file string, tag string, variable string) string {
|
// getting template string and replacing templating tags with variables
|
||||||
content, err := os.ReadFile("web/views/" + file + ".html")
|
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
||||||
|
|
||||||
find_tag := `{{` + tag + `}}`
|
|
||||||
|
|
||||||
template_result := strings.Replace(string(content), find_tag, variable, 1)
|
|
||||||
|
|
||||||
return template_result
|
|
||||||
}
|
|
||||||
|
|
||||||
func Put_vars_to_template(template string, vars map[string]string) string {
|
func Put_vars_to_template(template string, vars map[string]string) string {
|
||||||
for key, value := range vars {
|
// run through vars array gettin key as tag to find in template and value as value to replace it with
|
||||||
fmt.Print(key, value)
|
for tag, value := range vars {
|
||||||
template = strings.Replace(template, "{{"+key+"}}", value, 1)
|
// find the tag and replace it with value
|
||||||
|
template = strings.Replace(template, "{{"+tag+"}}", value, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return template
|
return template
|
||||||
|
|||||||
@ -1,3 +0,0 @@
|
|||||||
<pre>
|
|
||||||
{{json_code}}
|
|
||||||
</pre>
|
|
||||||
3
web/views/part/json_out_as_list.html
Normal file
3
web/views/part/json_out_as_list.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<ul>
|
||||||
|
{{li_from_json_code}}
|
||||||
|
</ul>
|
||||||
1
web/views/tag/li.html
Normal file
1
web/views/tag/li.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<li class="list-item{{li_id}}">{{content}}</li>
|
||||||
@ -21,5 +21,5 @@ func Server() {
|
|||||||
// wellcoming main web page
|
// wellcoming main web page
|
||||||
func main_page(w http.ResponseWriter, req *http.Request) {
|
func main_page(w http.ResponseWriter, req *http.Request) {
|
||||||
templ := map[string]string{"welcome_message": "hello world! this is api server with easy web menagment"}
|
templ := map[string]string{"welcome_message": "hello world! this is api server with easy web menagment"}
|
||||||
fmt.Fprint(w, render.Template_with_page_vars("main", templ))
|
fmt.Fprint(w, render.Put_vars_to_template(render.Template_of_part("main"), templ))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user