-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
111 lines (89 loc) · 2.28 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strconv"
"time"
"github.com/asterjdm/Cosmos-Search/engines"
)
func main() {
fmt.Println("Launched on : http://localhost:5000")
http.HandleFunc("/", home)
http.HandleFunc("/search/", search)
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
log.Fatal(http.ListenAndServe(":5000", nil))
}
func home(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("templates/home/index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func search(w http.ResponseWriter, r *http.Request) {
now := time.Now()
startTime := now.UnixMilli()
queryParams := r.URL.Query()
query := queryParams.Get("q")
pageString := queryParams.Get("page")
var page int
var err error
if pageString != "" {
page, err = strconv.Atoi(pageString)
if err != nil {
page = 0
}
}
var next_page_url string = fmt.Sprintf("/search?q=%s&page=%d", query, page+1)
results, err := engines.Search(query, &page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.ParseFiles("templates/search/index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
wikiInfo, err := engines.GetWiki(query)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
FoundInfo, ok := wikiInfo["Found"].(bool)
if !ok {
http.Error(w, "Erreur lors de la conversion du champ Found en booléen", http.StatusInternalServerError)
return
}
now = time.Now()
endTime := now.UnixMilli()
totalSearchTimeSec := (float64(endTime) - float64(startTime))/1000
data := struct {
Results []map[string]string
Query string
NextPageUrl string
Info map[string]interface{}
FoundInfo bool
SearchTime float64
}{
Results: results,
Query: query,
NextPageUrl: next_page_url,
Info: wikiInfo,
FoundInfo: FoundInfo,
SearchTime: totalSearchTimeSec,
}
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}