first commit. working templating, path routing, api responses, web menagment path
This commit is contained in:
49
web/api/api.go
Normal file
49
web/api/api.go
Normal file
@ -0,0 +1,49 @@
|
||||
package api
|
||||
|
||||
// package for reading and working with the storage file of the api's
|
||||
// saving, loading, handling the api requests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// api manipulation function
|
||||
func Apis() map[string]interface{} {
|
||||
return get_apis()
|
||||
}
|
||||
|
||||
// return the value of api by name
|
||||
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
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(jData)
|
||||
}
|
||||
|
||||
// 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")
|
||||
defer jsonFile.Close()
|
||||
|
||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||
|
||||
var result map[string]interface{}
|
||||
json.Unmarshal([]byte(byteValue), &result)
|
||||
|
||||
return result
|
||||
|
||||
}
|
||||
5
web/api/apis
Normal file
5
web/api/apis
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"main":{"asd":"asd"},
|
||||
"sec":"/api/sec",
|
||||
"thi":"/api/thi"
|
||||
}
|
||||
31
web/manager/manager.go
Normal file
31
web/manager/manager.go
Normal file
@ -0,0 +1,31 @@
|
||||
package manager
|
||||
|
||||
// package for the web menagment of the api paths
|
||||
|
||||
import (
|
||||
"api_manager/web/api"
|
||||
"api_manager/web/render"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Add_api(w http.ResponseWriter, req *http.Request) {
|
||||
// api.Apis["second"] = "/api/sec"
|
||||
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))
|
||||
}
|
||||
61
web/render/render.go
Normal file
61
web/render/render.go
Normal file
@ -0,0 +1,61 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Template_with_string(str string) string {
|
||||
head, err := os.ReadFile("web/views/head.html")
|
||||
foot, err := os.ReadFile("web/views/foot.html")
|
||||
template := "" + string(head) + str + string(foot)
|
||||
fmt.Println(err)
|
||||
|
||||
return template
|
||||
}
|
||||
|
||||
func Template_with_page(str string) string {
|
||||
head, err := os.ReadFile("web/views/head.html")
|
||||
foot, err := os.ReadFile("web/views/foot.html")
|
||||
page, err := os.ReadFile("web/views/" + str + ".html")
|
||||
|
||||
template := "" + string(head) + string(page) + string(foot)
|
||||
fmt.Println(err)
|
||||
|
||||
return template
|
||||
}
|
||||
|
||||
func Template_with_page_vars(str string, tags_and_variables map[string]string) string {
|
||||
head, err := os.ReadFile("web/views/head.html")
|
||||
foot, err := os.ReadFile("web/views/foot.html")
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 + `}`
|
||||
|
||||
template_result := strings.Replace(string(content), find_tag, variable, 1)
|
||||
|
||||
return template_result
|
||||
}
|
||||
3
web/views/foot.html
Normal file
3
web/views/foot.html
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
</body>
|
||||
</html>
|
||||
8
web/views/head.html
Normal file
8
web/views/head.html
Normal file
@ -0,0 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
1
web/views/main.html
Normal file
1
web/views/main.html
Normal file
@ -0,0 +1 @@
|
||||
<welcome_message>
|
||||
1
web/views/part/key-val.html
Normal file
1
web/views/part/key-val.html
Normal file
@ -0,0 +1 @@
|
||||
<key>:<val>
|
||||
3
web/views/tag/text.html
Normal file
3
web/views/tag/text.html
Normal file
@ -0,0 +1,3 @@
|
||||
<div>
|
||||
<p><fnr></p>
|
||||
</div>
|
||||
25
web/web.go
Normal file
25
web/web.go
Normal file
@ -0,0 +1,25 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"api_manager/web/api"
|
||||
"api_manager/web/manager"
|
||||
"api_manager/web/render"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Server() {
|
||||
http.HandleFunc("/", main_page)
|
||||
|
||||
http.HandleFunc("/mgr/add", manager.Add_api)
|
||||
http.HandleFunc("/mgr/show", manager.Show_apis)
|
||||
http.HandleFunc("/api/", api.Get_api)
|
||||
|
||||
http.ListenAndServe(":8090", nil)
|
||||
}
|
||||
|
||||
// wellcoming main web page
|
||||
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"}
|
||||
fmt.Fprint(w, render.Template_with_page_vars("main", templ))
|
||||
}
|
||||
Reference in New Issue
Block a user