Compare commits
2 Commits
c955bde580
...
1a1ef9b55f
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a1ef9b55f | |||
| 68c9aad3a1 |
14
.vscode/launch.json
vendored
Normal file
14
.vscode/launch.json
vendored
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -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
|
||||||
63
lib/lib.go
Normal file
63
lib/lib.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// func Map_of_string_to_josn(array map[string]string) string {
|
||||||
|
// jData, err := json.Marshal(array)
|
||||||
|
// lib.Handle_err(err, lib.Map_args())
|
||||||
|
|
||||||
|
// return string(jData)
|
||||||
|
// }
|
||||||
6
main.go
6
main.go
@ -1,13 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "api_manager/web"
|
||||||
|
|
||||||
// main package of the programm
|
// main package of the programm
|
||||||
// launching the main web package
|
// launching the main web package
|
||||||
// for future: loging
|
// for future: loging
|
||||||
|
|
||||||
import (
|
|
||||||
"api_manager/web"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
web.Server()
|
web.Server()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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, lib.Map_args())
|
||||||
// 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, lib.Map_args())
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
fmt.Println("Successfully Opened users.json")
|
|
||||||
defer jsonFile.Close()
|
defer jsonFile.Close()
|
||||||
|
|
||||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"main":{"asd":"asd"},
|
"main":{"body":"message"},
|
||||||
"sec":"/api/sec",
|
"sec":"/api/sec",
|
||||||
"thi":"/api/thi"
|
"thi":"/api/thi"
|
||||||
}
|
}
|
||||||
@ -3,6 +3,7 @@ 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"
|
||||||
@ -29,3 +30,18 @@ func Show_apis(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
fmt.Fprint(w, render.Template_with_string(eval))
|
fmt.Fprint(w, render.Template_with_string(eval))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Render_apis(w http.ResponseWriter, req *http.Request) {
|
||||||
|
apis := api.Apis()
|
||||||
|
eval := "<div>"
|
||||||
|
for tag, varr := range apis {
|
||||||
|
eval_group := ""
|
||||||
|
eval_group += tag
|
||||||
|
eval_group += "-"
|
||||||
|
eval_group += fmt.Sprintf("%v", varr)
|
||||||
|
eval_group += "<br>"
|
||||||
|
eval += eval_group
|
||||||
|
}
|
||||||
|
eval += "</div>"
|
||||||
|
fmt.Fprint(w, render.Put_vars_to_template(render.Template_with_part("json_out"), lib.Map_args("json_code", eval)))
|
||||||
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package render
|
package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"api_manager/lib"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -17,8 +18,11 @@ func Template_with_string(str string) string {
|
|||||||
|
|
||||||
func Template_with_page(str string) string {
|
func Template_with_page(str string) string {
|
||||||
head, err := os.ReadFile("web/views/head.html")
|
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")
|
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")
|
page, err := os.ReadFile("web/views/" + str + ".html")
|
||||||
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||||
|
|
||||||
template := "" + string(head) + string(page) + string(foot)
|
template := "" + string(head) + string(page) + string(foot)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@ -28,34 +32,57 @@ func Template_with_page(str string) string {
|
|||||||
|
|
||||||
func Template_with_page_vars(str string, tags_and_variables map[string]string) string {
|
func Template_with_page_vars(str string, tags_and_variables map[string]string) string {
|
||||||
head, err := os.ReadFile("web/views/head.html")
|
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")
|
foot, err := os.ReadFile("web/views/foot.html")
|
||||||
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||||
|
|
||||||
page := ""
|
page := ""
|
||||||
for tag, varr := range tags_and_variables {
|
for tag, varr := range tags_and_variables {
|
||||||
page = templating(str, tag, varr)
|
page = templating(str, tag, varr)
|
||||||
}
|
}
|
||||||
template := "" + string(head) + string(page) + string(foot)
|
template := "" + string(head) + string(page) + string(foot)
|
||||||
fmt.Println(err)
|
|
||||||
|
|
||||||
return template
|
return template
|
||||||
}
|
}
|
||||||
|
|
||||||
func Template_of_tag(str string, html_tag string) string {
|
func Template_of_tag(str string, html_tag string) string {
|
||||||
tag, err := os.ReadFile("web/views/tag/" + html_tag + ".html")
|
tag, err := os.ReadFile("web/views/tag/" + html_tag + ".html")
|
||||||
result := strings.Replace(string(tag), "<fnr>", str, 1)
|
result := strings.Replace(string(tag), "{{fnr}}", str, 1)
|
||||||
fmt.Println(err)
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||||
|
|
||||||
return result
|
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 {
|
func templating(file string, tag string, variable string) string {
|
||||||
content, err := os.ReadFile("web/views/" + file + ".html")
|
content, err := os.ReadFile("web/views/" + file + ".html")
|
||||||
if err != nil {
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
find_tag := `{{` + tag + `}}`
|
||||||
}
|
|
||||||
find_tag := `{` + tag + `}`
|
|
||||||
|
|
||||||
template_result := strings.Replace(string(content), find_tag, variable, 1)
|
template_result := strings.Replace(string(content), find_tag, variable, 1)
|
||||||
|
|
||||||
return template_result
|
return template_result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Put_vars_to_template(template string, vars map[string]string) string {
|
||||||
|
for key, value := range vars {
|
||||||
|
fmt.Print(key, value)
|
||||||
|
template = strings.Replace(template, "{{"+key+"}}", value, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return template
|
||||||
|
}
|
||||||
|
|||||||
@ -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>
|
||||||
@ -1 +1 @@
|
|||||||
<welcome_message>
|
{{welcome_message}}
|
||||||
3
web/views/part/json_out.html
Normal file
3
web/views/part/json_out.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<pre>
|
||||||
|
{{json_code}}
|
||||||
|
</pre>
|
||||||
@ -1 +1 @@
|
|||||||
<key>:<val>
|
{{key}}:{{val}}
|
||||||
@ -1,3 +1,3 @@
|
|||||||
<div>
|
<div>
|
||||||
<p><fnr></p>
|
<p>{{fnr}}</p>
|
||||||
</div>
|
</div>
|
||||||
@ -12,7 +12,7 @@ func Server() {
|
|||||||
http.HandleFunc("/", main_page)
|
http.HandleFunc("/", main_page)
|
||||||
|
|
||||||
http.HandleFunc("/mgr/add", manager.Add_api)
|
http.HandleFunc("/mgr/add", manager.Add_api)
|
||||||
http.HandleFunc("/mgr/show", manager.Show_apis)
|
http.HandleFunc("/mgr/show", manager.Render_apis)
|
||||||
http.HandleFunc("/api/", api.Get_api)
|
http.HandleFunc("/api/", api.Get_api)
|
||||||
|
|
||||||
http.ListenAndServe(":8090", nil)
|
http.ListenAndServe(":8090", nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user