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"
|
||||
}
|
||||
Reference in New Issue
Block a user