Backend: Added basic endpoint with parameter gathering. Added sendResponse and GetFunctionName
This commit is contained in:
parent
e47db359db
commit
2f896195d5
3 changed files with 26 additions and 3 deletions
|
@ -5,6 +5,12 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handlePictureRequest(w http.ResponseWriter, r *http.Request) {
|
func getphoto(w http.ResponseWriter, r *http.Request) {
|
||||||
util.Logformat(util.INFO, "Calling 'handlePictureRequest' with '%s'\n", r.Method)
|
idStr := r.URL.Query().Get("id")
|
||||||
|
if idStr == "" {
|
||||||
|
util.Logformat(util.ERROR, "%s: Missing 'id' parameter.", util.GetFunctionName())
|
||||||
|
http.Error(w, "Missing or invalid parameters : id", http.StatusBadRequest)
|
||||||
|
} else {
|
||||||
|
sendResponse(w, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,14 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"backend/util"
|
"backend/util"
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start(port int64) {
|
func Start(port int64) {
|
||||||
// Creating endpoints
|
// Creating endpoints
|
||||||
http.HandleFunc("/hello/", wrapHeader(handlePictureRequest))
|
http.HandleFunc("/getphoto", wrapHeader(getphoto))
|
||||||
|
|
||||||
util.Logformat(util.INFO, "Starting server on port %d...\n", port)
|
util.Logformat(util.INFO, "Starting server on port %d...\n", port)
|
||||||
if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {
|
if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {
|
||||||
|
@ -17,6 +18,15 @@ func Start(port int64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendResponse(w http.ResponseWriter, r any) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if err := json.NewEncoder(w).Encode(r); err != nil {
|
||||||
|
util.Logformat(util.ERROR, "%s\n", err.Error())
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Not sure about that, might change it later... (added by Lemonochrme on 'yoboujon/automatic-management')
|
// Not sure about that, might change it later... (added by Lemonochrme on 'yoboujon/automatic-management')
|
||||||
func wrapHeader(next http.HandlerFunc) http.HandlerFunc {
|
func wrapHeader(next http.HandlerFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -2,9 +2,16 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetFunctionName() string {
|
||||||
|
pc, _, _, _ := runtime.Caller(1)
|
||||||
|
f := runtime.FuncForPC(pc)
|
||||||
|
return strings.Split(strings.Split(f.Name(), "/")[1], ".")[1]
|
||||||
|
}
|
||||||
|
|
||||||
func HasSubURI(r *http.Request) (bool, string) {
|
func HasSubURI(r *http.Request) (bool, string) {
|
||||||
url := strings.Split(r.URL.Path, "/")
|
url := strings.Split(r.URL.Path, "/")
|
||||||
return (len(url[2]) > 0), url[2]
|
return (len(url[2]) > 0), url[2]
|
||||||
|
|
Loading…
Add table
Reference in a new issue