Compare commits
5 Commits
1a1ef9b55f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 9650056120 | |||
| c4cce0fbd2 | |||
| 07fe3fe9db | |||
| 960d9e2ff8 | |||
| 3820bc399b |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.vscode
|
||||||
|
*.sublime-*
|
||||||
14
.vscode/launch.json
vendored
14
.vscode/launch.json
vendored
@ -1,14 +0,0 @@
|
|||||||
{
|
|
||||||
// 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"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
37
lib/lib.go
37
lib/lib.go
@ -7,6 +7,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Dev = true
|
||||||
|
|
||||||
func Handle_err(err error, args map[string]string) {
|
func Handle_err(err error, args map[string]string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if args["msg"] != "" {
|
if args["msg"] != "" {
|
||||||
@ -41,23 +43,26 @@ func Map_interface(array string) map[string]interface{} {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func Map_args(array ...string) map[string]string {
|
func Map_of_string_to_josn(array map[string]string) string {
|
||||||
if len(array) < 1 {
|
jData, err := json.Marshal(array)
|
||||||
return map[string]string{}
|
Handle_err(err, nil)
|
||||||
}
|
|
||||||
|
|
||||||
result := make(map[string]string)
|
return string(jData)
|
||||||
|
|
||||||
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 {
|
func Project_dir() string{
|
||||||
// jData, err := json.Marshal(array)
|
path, err := os.Getwd()
|
||||||
// lib.Handle_err(err, lib.Map_args())
|
Handle_err(err, nil)
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
// return string(jData)
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
4
main.go
4
main.go
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "api_manager/web"
|
import (
|
||||||
|
"api_manager/web"
|
||||||
|
)
|
||||||
|
|
||||||
// main package of the programm
|
// main package of the programm
|
||||||
// launching the main web package
|
// launching the main web package
|
||||||
|
|||||||
@ -23,7 +23,7 @@ func Get_api(w http.ResponseWriter, req *http.Request) {
|
|||||||
str := strings.Replace(string(req.RequestURI), "/api/", "", 1)
|
str := strings.Replace(string(req.RequestURI), "/api/", "", 1)
|
||||||
|
|
||||||
jData, err := json.Marshal(apis[str])
|
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.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(jData)
|
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}]
|
// 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")
|
||||||
lib.Handle_err(err, lib.Map_args())
|
lib.Handle_err(err, map[string]string{})
|
||||||
|
|
||||||
defer jsonFile.Close()
|
defer jsonFile.Close()
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"main":{"body":"message"},
|
"main":{"body":"message"},
|
||||||
"sec":"/api/sec",
|
"sec":"second",
|
||||||
"thi":"/api/thi"
|
"thi":"third"
|
||||||
}
|
}
|
||||||
@ -3,7 +3,6 @@ package manager
|
|||||||
// package for the web menagment of the api paths
|
// package for the web menagment of the api paths
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"api_manager/lib"
|
|
||||||
"api_manager/web/api"
|
"api_manager/web/api"
|
||||||
"api_manager/web/render"
|
"api_manager/web/render"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -15,33 +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) {
|
|
||||||
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"),
|
||||||
|
map[string]string{
|
||||||
|
"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"),
|
||||||
|
map[string]string{
|
||||||
|
"li_from_json_code":eval,
|
||||||
|
"template_title":"Api paths",
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,88 +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 (
|
||||||
"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 {
|
||||||
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
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
// handling error by lib function and exiting if it occures
|
||||||
foot, err := os.ReadFile("web/views/foot.html")
|
head, err := os.ReadFile(Get_template("head","")); lib.Handle_err(err, map[string]string{"die":"1"})
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
foot, err := os.ReadFile(Get_template("foot","")); lib.Handle_err(err, map[string]string{"die":"1"})
|
||||||
page, err := os.ReadFile("web/views/" + str + ".html")
|
// loading custom html template file
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
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")
|
|
||||||
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)
|
|
||||||
|
|
||||||
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)
|
|
||||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func Template_with_part(part_name string) string {
|
|
||||||
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")
|
|
||||||
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"))
|
|
||||||
|
|
||||||
template := "" + string(head) + string(page) + string(foot)
|
|
||||||
fmt.Println(err)
|
|
||||||
|
|
||||||
return template
|
|
||||||
}
|
|
||||||
|
|
||||||
func templating(file string, tag string, variable string) string {
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lib.Debug(template,"Put_vars_to_template") // debuging the template output
|
||||||
return template
|
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;
|
||||||
|
}
|
||||||
@ -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>
|
||||||
30
web/web.go
30
web/web.go
@ -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.Render_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))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user