Compare commits

...

7 Commits

16 changed files with 204 additions and 71 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.vscode
*.sublime-*

View File

@ -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 you can easely menage custom apis for app testing or simplifying app development

68
lib/lib.go Normal file
View File

@ -0,0 +1,68 @@
package lib
import (
"encoding/json"
"fmt"
"os"
"strconv"
)
var Dev = true
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_of_string_to_josn(array map[string]string) string {
jData, err := json.Marshal(array)
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,13 +1,13 @@
package main package main
// main package of the programm
// launching the main web package
// for future: loging
import ( import (
"api_manager/web" "api_manager/web"
) )
// main package of the programm
// launching the main web package
// for future: loging
func main() { func main() {
web.Server() web.Server()
} }

View File

@ -4,8 +4,8 @@ package api
// saving, loading, handling the api requests // saving, loading, handling the api requests
import ( import (
"api_manager/lib"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
@ -21,11 +21,10 @@ func Apis() map[string]interface{} {
func Get_api(w http.ResponseWriter, req *http.Request) { func Get_api(w http.ResponseWriter, req *http.Request) {
apis := Apis() apis := Apis()
str := strings.Replace(string(req.RequestURI), "/api/", "", 1) str := strings.Replace(string(req.RequestURI), "/api/", "", 1)
fmt.Print(apis[str])
jData, err := json.Marshal(apis[str]) jData, err := json.Marshal(apis[str])
if err != nil { lib.Handle_err(err, map[string]string{})
// handle error
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.Write(jData) 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}] // reding the api storage file. returning map value like map[main:{sub:sub}]
func get_apis() map[string]interface{} { func get_apis() map[string]interface{} {
jsonFile, err := os.Open("web/api/apis") jsonFile, err := os.Open("web/api/apis")
if err != nil { lib.Handle_err(err, map[string]string{})
fmt.Println(err)
}
fmt.Println("Successfully Opened users.json")
defer jsonFile.Close() defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile) byteValue, _ := ioutil.ReadAll(jsonFile)

View File

@ -1,5 +1,5 @@
{ {
"main":{"asd":"asd"}, "main":{"body":"message"},
"sec":"/api/sec", "sec":"second",
"thi":"/api/thi" "thi":"third"
} }

View File

@ -14,18 +14,30 @@ 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) { 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 += fmt.Sprintf("%v", varr)
eval_group += "-"
eval_group += tag eval_group += tag
eval_group += "<br>-"
eval_group += fmt.Sprintf("%v", varr)
eval_group += "<br>" eval_group += "<br>"
eval += render.Template_of_tag(eval_group, "text")
}
eval += "</div>"
fmt.Fprint(w, render.Template_with_string(eval)) eval += render.Put_vars_to_template(
render.Template_of_tag("li"),
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"),
map[string]string{
"li_from_json_code":eval,
"template_title":"Api paths",
}))
} }

View File

@ -1,61 +1,95 @@
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 (
"fmt" "api_manager/lib"
"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 {
head, err := os.ReadFile("web/views/head.html") // loading head.html and foot.html
foot, err := os.ReadFile("web/views/foot.html") // handling error by lib function and exiting if it occures
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) template := "" + string(head) + str + string(foot)
fmt.Println(err)
lib.Debug(template,"Template_with_string") // debuging the template
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 {
head, err := os.ReadFile("web/views/head.html") // loading head.html and foot.html
foot, err := os.ReadFile("web/views/foot.html") // handling error by lib function and exiting if it occures
page, err := os.ReadFile("web/views/" + str + ".html") 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) template := "" + string(head) + string(page) + string(foot)
fmt.Println(err)
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
// handling error by lib function and exiting if it occures
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
// handling error by lib function and exiting if it occures
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 return template
} }
func Template_with_page_vars(str string, tags_and_variables map[string]string) string { // getting template string and replacing templating tags with variables
head, err := os.ReadFile("web/views/head.html") func Put_vars_to_template(template string, vars map[string]string) string {
foot, err := os.ReadFile("web/views/foot.html") // run through vars array gettin key as tag to find in template and value as value to replace it with
page := "" for tag, value := range vars {
for tag, varr := range tags_and_variables { // find the tag and replace it with value
page = templating(str, tag, varr) template = strings.Replace(template, "{{"+tag+"}}", value, 1)
} }
template := "" + string(head) + string(page) + string(foot)
fmt.Println(err)
lib.Debug(template,"Put_vars_to_template") // debuging the template output
return template return template
} }
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)
return result // 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{
func templating(file string, tag string, variable string) string { path := ""
content, err := os.ReadFile("web/views/" + file + ".html") if type_of_template == ""{
if err != nil { path = lib.Project_dir()+"/web/views/"+path_to_template+".html";
fmt.Println(err) }else {
os.Exit(1) path = lib.Project_dir()+"/web/views/"+type_of_template+"/"+path_to_template+".html";
} }
find_tag := `{` + tag + `}`
template_result := strings.Replace(string(content), find_tag, variable, 1) lib.Debug(path,"Get_template") // debuging the template
return path;
return template_result
} }

View File

@ -3,6 +3,8 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>{{template_title}}</title>
<style></style>
</head> </head>
<body> <body>

View File

@ -1 +0,0 @@
<welcome_message>

View File

@ -0,0 +1,3 @@
<ul>
{{li_from_json_code}}
</ul>

View File

@ -1 +1 @@
<key>:<val> {{key}}:{{val}}

1
web/views/part/main.html Normal file
View File

@ -0,0 +1 @@
{{welcome_message}}

1
web/views/tag/li.html Normal file
View File

@ -0,0 +1 @@
<li class="list-item{{li_id}}">{{content}}</li>

View File

@ -1,3 +1,3 @@
<div> <div>
<p><fnr></p> <p>{{fnr}}</p>
</div> </div>

View File

@ -5,21 +5,35 @@ import (
"api_manager/web/manager" "api_manager/web/manager"
"api_manager/web/render" "api_manager/web/render"
"fmt" "fmt"
"net"
"net/http" "net/http"
) )
func Server() { func Server() {
http.HandleFunc("/", main_page)
http.HandleFunc("/mgr/add", manager.Add_api) l, err := net.Listen("tcp", ":8090")
http.HandleFunc("/mgr/show", manager.Show_apis) if err == nil {
http.HandleFunc("/api/", api.Get_api) fmt.Println("Listening on port 8090")
}
http.ListenAndServe(":8090", nil) 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 // 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","template_title":"main page"}
fmt.Fprint(w, render.Template_with_page_vars("main", templ)) fmt.Fprint(w, render.Put_vars_to_template(render.Template_of_part("main"), templ))
} }