added error handling, map transformation functions for conviniese

This commit is contained in:
2025-10-12 13:59:51 +00:00
parent c955bde580
commit 68c9aad3a1
6 changed files with 90 additions and 21 deletions

View File

@ -4,8 +4,8 @@ package api
// saving, loading, handling the api requests
import (
"api_manager/lib"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
@ -21,11 +21,10 @@ func Apis() map[string]interface{} {
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
}
lib.Handle_err(err, lib.Map_args())
w.Header().Set("Content-Type", "application/json")
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}]
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")
lib.Handle_err(err, lib.Map_args())
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)