-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (113 loc) · 4.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"Dictionnaire/dictionary"
"encoding/json"
"fmt"
"net/http"
)
const filename = "dictionary/dictionary.txt"
// main function
func main() {
// Création d'un dictionnaire
dic := dictionary.New()
// Chargement du dictionnaire depuis le fichier "dictionary.txt"
err := dic.LoadFromFile(filename)
if err != nil {
fmt.Println("Erreur lors du chargement du fichier :", err)
return
}
// Gestion des routes
// Création des gestionnaires pour différentes routes HTTP
http.HandleFunc("/add", func(w http.ResponseWriter, r *http.Request) {
handleAdd(dic, w, r)
})
http.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
handleGet(dic, w, r)
})
http.HandleFunc("/remove", func(w http.ResponseWriter, r *http.Request) {
handleRemove(dic, w, r)
})
http.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) {
handleList(dic, w, r)
})
// Démarrage du serveur HTTP
http.ListenAndServe(":8080", nil)
}
// handleAdd gère la route /add
func handleAdd(dic *dictionary.Dictionary, w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { //Vérifier si la méthode HHTP de la requête est POST
http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed)
return
}
//Attente d'un corps de requête JSON contenant un mot (Word) et une définition (Definition)
var entry struct {
Word string `json:"word"`
Definition string `json:"definition"`
}
decoder := json.NewDecoder(r.Body) // Décodage du JSON de la requête dans la structure entry
err := decoder.Decode(&entry)
if err != nil {
http.Error(w, "Erreur de décodage JSON", http.StatusBadRequest)
return
}
//Création du canal pour synchroniser l'ajout asynchrone au dictionnaire
channelAdd := make(chan struct{})
go func() { //Lancement du goroutine
dic.Add(entry.Word, entry.Definition, channelAdd, filename)
}()
<-channelAdd //Envoi d'un signal sur le canal par le goroutine
w.WriteHeader(http.StatusCreated) //Réponse HTTP avec le code de statut 201 (StatusCreated) pour indiquer que la ressource a été créée avec succès
}
// handleGet gère la route /get
func handleGet(dic *dictionary.Dictionary, w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet { //Vérifier si la méthode HHTP de la requête est GET
http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed)
return
}
word := r.URL.Query().Get("word")
definition, found := dic.Get(word)
if !found {
http.Error(w, "Mot non trouvé dans le dictionnaire", http.StatusNotFound)
return
}
response := struct {
Word string `json:"word"`
Definition string `json:"definition"`
}{Word: word, Definition: definition}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// handleRemove gère la route /remove
func handleRemove(dic *dictionary.Dictionary, w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete { //Vérifier si la méthode HHTP de la requête est DELETE
http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed)
return
}
word := r.URL.Query().Get("word")
channelRemove := make(chan struct{})
go func() {
err := dic.Remove(word, channelRemove, channelRemove, filename)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
}()
<-channelRemove
}
// handleList gère la route /list
func handleList(dic *dictionary.Dictionary, w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet { //Vérifier si la méthode HHTP de la requête est GET
http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed)
return
}
wordList := dic.List()
response := struct {
Entries []string `json:"entries"`
}{Entries: wordList}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// GET : http://localhost:8080/get?word=WIFI (GET)
// LIST : http://localhost:8080/list (GET)
// DELETE : http://localhost:8080/remove?word=NouveauMot (DELETE)
// http://localhost:8080/add (POST) {"word":"Ordinateur","definition":"Un ordinateur est une machine électronique capable de recevoir, de traiter et de stocker des données"}