Added basic server from ['yoboujon/automatic-management'](https://github.com/yoboujon/automatic-management)

This commit is contained in:
Yohan Boujon 2025-05-01 12:11:55 +02:00
parent f9e9ad1297
commit e47db359db
6 changed files with 125 additions and 3 deletions

1
backend/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.env

View file

@ -1,7 +1,14 @@
package main package main
import "fmt" import (
"backend/server"
"backend/util"
)
func main() { func main() {
fmt.Println("Hello, World!") // Checking arguments
util.SetLevel(util.NOLOG)
// Rest API start on port
server.Start(8085)
} }

View file

@ -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)
}

34
backend/server/server.go Normal file
View file

@ -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)
}
}

59
backend/util/logger.go Normal file
View file

@ -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
}

11
backend/util/util.go Normal file
View file

@ -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]
}