From 68c9aad3a1574dcc4410982bf07d8ff4b5c03c46 Mon Sep 17 00:00:00 2001 From: n0rdye Date: Sun, 12 Oct 2025 13:59:51 +0000 Subject: [PATCH] added error handling, map transformation functions for conviniese --- .vscode/launch.json | 14 +++++++++++ README.md | 2 +- lib/lib.go | 56 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 6 ++--- web/api/api.go | 15 +++++------- web/render/render.go | 18 ++++++++------ 6 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 lib/lib.go diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6e66f52 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "command": "go run .", + "name": "Run go start", + "request": "launch", + "type": "node-terminal" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index e7e524b..2971d22 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -#project created for easy menagment creating and accessing custom apis +project created for easy menagment creating and accessing custom apis you can easely menage custom apis for app testing or simplifying app development \ No newline at end of file diff --git a/lib/lib.go b/lib/lib.go new file mode 100644 index 0000000..0d63a31 --- /dev/null +++ b/lib/lib.go @@ -0,0 +1,56 @@ +package lib + +import ( + "encoding/json" + "fmt" + "os" + "strconv" +) + +func Handle_err(err error, args map[string]string) { + if err != nil { + if args["msg"] != "" { + fmt.Println(args["msg"], " due to error <", err, ">") + } else { + fmt.Println(err) + } + + if args["die"] != "" { + code, err := strconv.Atoi(args["die"]) + if err != nil { + fmt.Println(err) + } else { + os.Exit(code) + } + + } + } +} + +func Map_string(array string) map[string]string { + var result map[string]string + json.Unmarshal([]byte(array), &result) + + return result +} + +func Map_interface(array string) map[string]interface{} { + var result map[string]interface{} + json.Unmarshal([]byte(array), &result) + + 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 +} diff --git a/main.go b/main.go index df23ce0..3694a1b 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,11 @@ package main +import "api_manager/web" + // main package of the programm // launching the main web package // for future: loging -import ( - "api_manager/web" -) - func main() { web.Server() } diff --git a/web/api/api.go b/web/api/api.go index 99ff70c..f8c5d6a 100644 --- a/web/api/api.go +++ b/web/api/api.go @@ -4,8 +4,8 @@ package api // saving, loading, handling the api requests import ( + "api_manager/lib" "encoding/json" - "fmt" "io/ioutil" "net/http" "os" @@ -21,11 +21,10 @@ func Apis() map[string]interface{} { func Get_api(w http.ResponseWriter, req *http.Request) { apis := Apis() str := strings.Replace(string(req.RequestURI), "/api/", "", 1) - fmt.Print(apis[str]) + jData, err := json.Marshal(apis[str]) - if err != nil { - // handle error - } + lib.Handle_err(err, lib.Map_args()) + w.Header().Set("Content-Type", "application/json") w.Write(jData) } @@ -33,10 +32,8 @@ 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") - if err != nil { - fmt.Println(err) - } - fmt.Println("Successfully Opened users.json") + lib.Handle_err(err, lib.Map_args()) + defer jsonFile.Close() byteValue, _ := ioutil.ReadAll(jsonFile) diff --git a/web/render/render.go b/web/render/render.go index e14b7ea..de49cc0 100644 --- a/web/render/render.go +++ b/web/render/render.go @@ -1,6 +1,7 @@ package render import ( + "api_manager/lib" "fmt" "os" "strings" @@ -17,8 +18,11 @@ func Template_with_string(str string) string { func Template_with_page(str 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, err := os.ReadFile("web/views/" + str + ".html") + lib.Handle_err(err, lib.Map_args("die", "1")) template := "" + string(head) + string(page) + string(foot) fmt.Println(err) @@ -28,13 +32,15 @@ func Template_with_page(str string) string { 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) - fmt.Println(err) return template } @@ -42,18 +48,16 @@ func Template_with_page_vars(str string, tags_and_variables map[string]string) s func Template_of_tag(str string, html_tag string) string { tag, err := os.ReadFile("web/views/tag/" + html_tag + ".html") result := strings.Replace(string(tag), "", str, 1) - fmt.Println(err) + lib.Handle_err(err, lib.Map_args("die", "1")) return result } func templating(file string, tag string, variable string) string { content, err := os.ReadFile("web/views/" + file + ".html") - if err != nil { - fmt.Println(err) - os.Exit(1) - } - find_tag := `{` + tag + `}` + lib.Handle_err(err, lib.Map_args("die", "1")) + + find_tag := `<` + tag + `>` template_result := strings.Replace(string(content), find_tag, variable, 1)