changed lib removed map_args, project_dir debug; changed render added get_template; small changes to api,manager due to function delition; big change to web split listen and serve for server log and routing through switch-case

This commit is contained in:
2025-10-13 20:38:21 +00:00
parent 3820bc399b
commit 960d9e2ff8
6 changed files with 86 additions and 43 deletions

View File

@ -7,6 +7,8 @@ import (
"strconv"
)
var Dev = true
func Handle_err(err error, args map[string]string) {
if err != nil {
if args["msg"] != "" {
@ -41,23 +43,26 @@ func Map_interface(array string) map[string]interface{} {
return result
}
func Map_args(array ...string) map[string]string {
if len(array) < 1 {
return map[string]string{}
}
result := make(map[string]string)
for i := 0; i < len(array); i += 2 {
result[array[i]] = array[i+1]
}
return result
}
func Map_of_string_to_josn(array map[string]string) string {
jData, err := json.Marshal(array)
Handle_err(err, Map_args())
Handle_err(err, nil)
return string(jData)
}
func Project_dir() string{
path, err := os.Getwd()
Handle_err(err, nil)
return path
}
func Debug(message string,function string){
if Dev == true{
fmt.Println("\n")
fmt.Println("---------debug-message----------")
fmt.Println("---------"+function+"-------------")
fmt.Println(message)
fmt.Println("--------------end---------------")
fmt.Println("\n")
}
}

View File

@ -1,6 +1,8 @@
package main
import "api_manager/web"
import (
"api_manager/web"
)
// main package of the programm
// launching the main web package
@ -8,4 +10,4 @@ import "api_manager/web"
func main() {
web.Server()
}
}

View File

@ -23,7 +23,7 @@ func Get_api(w http.ResponseWriter, req *http.Request) {
str := strings.Replace(string(req.RequestURI), "/api/", "", 1)
jData, err := json.Marshal(apis[str])
lib.Handle_err(err, lib.Map_args())
lib.Handle_err(err, map[string]string{})
w.Header().Set("Content-Type", "application/json")
w.Write(jData)
@ -32,7 +32,7 @@ func Get_api(w http.ResponseWriter, req *http.Request) {
// reding the api storage file. returning map value like map[main:{sub:sub}]
func get_apis() map[string]interface{} {
jsonFile, err := os.Open("web/api/apis")
lib.Handle_err(err, lib.Map_args())
lib.Handle_err(err, map[string]string{})
defer jsonFile.Close()

View File

@ -3,7 +3,6 @@ package manager
// package for the web menagment of the api paths
import (
"api_manager/lib"
"api_manager/web/api"
"api_manager/web/render"
"fmt"
@ -28,11 +27,17 @@ func Render_apis(w http.ResponseWriter, req *http.Request) {
eval += render.Put_vars_to_template(
render.Template_of_tag("li"),
lib.Map_args("li_id", fmt.Sprint(i), "content", eval_group))
map[string]string{
"li_id":fmt.Sprint(i),
"content":eval_group,
})
i++
}
fmt.Fprint(w,
render.Put_vars_to_template(
render.Template_of_part("json_out_as_list"),
lib.Map_args("li_from_json_code", eval)))
map[string]string{
"li_from_json_code":eval,
"template_title":"Api paths",
}))
}

View File

@ -14,54 +14,55 @@ import (
// loading default head and foot template and adding custom string in the middle
func Template_with_string(str string) string {
// loading head.html and foot.html
head, err := os.ReadFile("web/views/head.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"))
head, err := os.ReadFile(Get_template("head","")); lib.Handle_err(err, map[string]string{"die":"1"})
foot, err := os.ReadFile(Get_template("foot","")); lib.Handle_err(err, map[string]string{"die":"1"})
// combining html parts
template := "" + string(head) + str + string(foot)
lib.Debug(template,"Template_with_string") // debuging the 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 {
// loading head.html and foot.html
head, err := os.ReadFile("web/views/head.html")
foot, err := os.ReadFile("web/views/foot.html")
// loading custom html template file
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"))
head, err := os.ReadFile(Get_template("head","")); lib.Handle_err(err, map[string]string{"die":"1"})
foot, err := os.ReadFile(Get_template("foot","")); lib.Handle_err(err, map[string]string{"die":"1"})
// loading custom html template file
page, err := os.ReadFile(Get_template(str,"part")); lib.Handle_err(err, map[string]string{"die":"1"})
// combining html parts
template := "" + string(head) + string(page) + string(foot)
lib.Debug(template,"Template_with_page") // debuging the template output
return template
}
// 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")
// handling error by lib function and exiting if it occures
lib.Handle_err(err, lib.Map_args("die", "1"))
tag, err := os.ReadFile(Get_template(html_tag,"tag")); lib.Handle_err(err, map[string]string{"die":"1"})
lib.Debug(string(tag),"Template_of_tag") // debuging the template output
return string(tag)
}
// 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")
page, err := os.ReadFile("web/views/part/" + part_name + ".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"))
head, err := os.ReadFile(Get_template("head","")); lib.Handle_err(err, map[string]string{"die":"1"})
foot, err := os.ReadFile(Get_template("foot","")); lib.Handle_err(err, map[string]string{"die":"1"})
page, err := os.ReadFile(Get_template(part_name,"part")); lib.Handle_err(err, map[string]string{"die":"1"})
// combining html parts
template := "" + string(head) + string(page) + string(foot)
lib.Debug(template,"Template_of_part") // debuging the template output
return template
}
@ -74,5 +75,21 @@ func Put_vars_to_template(template string, vars map[string]string) string {
template = strings.Replace(template, "{{"+tag+"}}", value, 1)
}
lib.Debug(template,"Put_vars_to_template") // debuging the template output
return template
}
// searchin default folder and retriving the template files in web views folder
// type of template searching for sub folder in views
func Get_template(path_to_template string, type_of_template string) string{
path := ""
if type_of_template == ""{
path = lib.Project_dir()+"/web/views/"+path_to_template+".html";
}else {
path = lib.Project_dir()+"/web/views/"+type_of_template+"/"+path_to_template+".html";
}
lib.Debug(path,"Get_template") // debuging the template
return path;
}

View File

@ -5,21 +5,35 @@ import (
"api_manager/web/manager"
"api_manager/web/render"
"fmt"
"net"
"net/http"
)
func Server() {
http.HandleFunc("/", main_page)
http.HandleFunc("/mgr/add", manager.Add_api)
http.HandleFunc("/mgr/show", manager.Render_apis)
http.HandleFunc("/api/", api.Get_api)
http.ListenAndServe(":8090", nil)
l, err := net.Listen("tcp", ":8090")
if err == nil {
fmt.Println("Listening on port 8090")
}
http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
main_page(w,r);
case "/mgr/add":
manager.Add_api(w,r)
case "/mgr/show":
manager.Render_apis(w,r)
case "/api/":
api.Get_api(w,r)
default:
fmt.Fprint(w,"Not Found")
return
}
}))
}
// wellcoming main web page
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","template_title":"main page"}
fmt.Fprint(w, render.Put_vars_to_template(render.Template_of_part("main"), templ))
}