added error handling, map transformation functions for conviniese
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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), "<fnr>", 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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user