Added basic server from ['yoboujon/automatic-management'](https://github.com/yoboujon/automatic-management)
This commit is contained in:
parent
f9e9ad1297
commit
e47db359db
6 changed files with 125 additions and 3 deletions
1
backend/.gitignore
vendored
Normal file
1
backend/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.env
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
10
backend/server/pictures.go
Normal file
10
backend/server/pictures.go
Normal 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
34
backend/server/server.go
Normal 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
59
backend/util/logger.go
Normal 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
11
backend/util/util.go
Normal 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]
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue