46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"backend/util"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func Start(port int64) {
|
|
// Creating endpoints
|
|
http.HandleFunc("/photo_list", wrapHeader(getphotolist))
|
|
http.HandleFunc("/photo/", wrapHeader(getphoto))
|
|
http.HandleFunc("/photo_compressed/", wrapHeader(getphoto_compressed))
|
|
|
|
util.Logformat(util.INFO, "Starting server on port %d...\n", port)
|
|
if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {
|
|
util.Logformat(util.ERROR, "Could not start server (%s)\n", err.Error())
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
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')
|
|
func wrapHeader(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
|
|
|
if r.Method == http.MethodOptions {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
next(w, r)
|
|
}
|
|
}
|