Files
api_web_manager/web/api/api.go

50 lines
1.0 KiB
Go

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
}