From e47db359db8c36538bbcad486bc77200aa390ede Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Thu, 1 May 2025 12:11:55 +0200 Subject: [PATCH] Added basic server from ['yoboujon/automatic-management'](https://github.com/yoboujon/automatic-management) --- backend/.gitignore | 1 + backend/main.go | 13 +++++++-- backend/server/pictures.go | 10 +++++++ backend/server/server.go | 34 ++++++++++++++++++++++ backend/util/logger.go | 59 ++++++++++++++++++++++++++++++++++++++ backend/util/util.go | 11 +++++++ 6 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 backend/.gitignore create mode 100644 backend/server/pictures.go create mode 100644 backend/server/server.go create mode 100644 backend/util/logger.go create mode 100644 backend/util/util.go diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/backend/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/backend/main.go b/backend/main.go index d9e8b4e..f851159 100644 --- a/backend/main.go +++ b/backend/main.go @@ -1,7 +1,14 @@ package main -import "fmt" +import ( + "backend/server" + "backend/util" +) func main() { - fmt.Println("Hello, World!") -} \ No newline at end of file + // Checking arguments + util.SetLevel(util.NOLOG) + + // Rest API start on port + server.Start(8085) +} diff --git a/backend/server/pictures.go b/backend/server/pictures.go new file mode 100644 index 0000000..6eff88a --- /dev/null +++ b/backend/server/pictures.go @@ -0,0 +1,10 @@ +package server + +import ( + "backend/util" + "net/http" +) + +func handlePictureRequest(w http.ResponseWriter, r *http.Request) { + util.Logformat(util.INFO, "Calling 'handlePictureRequest' with '%s'\n", r.Method) +} diff --git a/backend/server/server.go b/backend/server/server.go new file mode 100644 index 0000000..c8c1f1c --- /dev/null +++ b/backend/server/server.go @@ -0,0 +1,34 @@ +package server + +import ( + "backend/util" + "net/http" + "strconv" +) + +func Start(port int64) { + // Creating endpoints + http.HandleFunc("/hello/", wrapHeader(handlePictureRequest)) + + 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) + } +} + +// 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) + } +} diff --git a/backend/util/logger.go b/backend/util/logger.go new file mode 100644 index 0000000..0cb096c --- /dev/null +++ b/backend/util/logger.go @@ -0,0 +1,59 @@ +package util + +import ( + "fmt" + "time" +) + +type colorFormat string +type LogLevel int + +const ( + BLUE colorFormat = "\033[34m" + GREEN colorFormat = "\033[32m" + YELLOW colorFormat = "\033[33m" + RED colorFormat = "\033[31m" + LIGHT_RED colorFormat = "\033[91m" + WHITE colorFormat = "\033[97m" + RESET colorFormat = "\033[0m" +) + +const ( + NOLOG LogLevel = iota + INFO + CORRECT + WARNING + ERROR +) + +var colorPrint = map[LogLevel]colorFormat{ + NOLOG: "", + INFO: BLUE + "[INFO]" + RESET, + CORRECT: GREEN, + WARNING: YELLOW + "[WARN]", + ERROR: RED + "[ERR.]" + LIGHT_RED, +} + +var minimumlog = NOLOG + +func Logformat(level LogLevel, format string, args ...interface{}) { + if level < minimumlog { + return + } + + header := colorPrint[level] + message := fmt.Sprintf(format, args...) + currentTime := time.Now() + time := fmt.Sprintf("[%02d/%02d/%d %02d:%02d:%02d]", currentTime.Day(), currentTime.Month(), currentTime.Year(), currentTime.Hour(), currentTime.Minute(), currentTime.Second()) + if level == NOLOG { + fmt.Printf("%s\t\t%s", time, message) + } else if level != INFO { + fmt.Printf("%s%s\t%s%s", header, time, message, RESET) + } else { + fmt.Printf("%s%s\t%s", header, time, message) + } +} + +func SetLevel(level LogLevel) { + minimumlog = level +} diff --git a/backend/util/util.go b/backend/util/util.go new file mode 100644 index 0000000..0d98a0f --- /dev/null +++ b/backend/util/util.go @@ -0,0 +1,11 @@ +package util + +import ( + "net/http" + "strings" +) + +func HasSubURI(r *http.Request) (bool, string) { + url := strings.Split(r.URL.Path, "/") + return (len(url[2]) > 0), url[2] +}