Files
httpRouter/main.go
2025-12-28 14:12:31 +01:00

56 lines
1.3 KiB
Go

package main
import (
"fmt"
"httpRouter/src/router"
"net"
"time"
)
func main() {
//Création du socket d'écoute
server, err := net.Listen("tcp", "localhost:4000")
if err != nil {
fmt.Printf("Erreur : %s", err.Error())
}
defer server.Close()
//Initialisation du routeur
router := router.NewRouter()
router.Register("GET", "/hello", sendHello)
router.Register("GET", "/time", sendTime)
router.Register("GET", "/time/{time}", hello)
router.Register("GET", "/time/{day}/{hour}/{minute}", hello)
//Écoute et gestion des réception
for {
//Accepter une nouvelle connexion
conn, err := server.Accept()
//Gestion des erreurs s'il y en a (stop total)
if err != nil {
fmt.Printf("Erreur : %s\n", err.Error())
break
}
fmt.Printf("Nouvelle connection acceptée !\n")
go router.HandleClient(conn)
}
}
func sendHello(method, path string, args map[string]string, queries map[string]string) (int, string) {
return 200, "Hello World !"
}
func sendTime(method, path string, args map[string]string, queries map[string]string) (int, string) {
return 200, fmt.Sprintf("%q", time.Now())
}
func hello(method, path string, args map[string]string, queries map[string]string) (int, string) {
fmt.Printf("%w\n", queries)
return 200, fmt.Sprintf("%w", args)
}