-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
68 lines (59 loc) · 1.66 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"github.com/oadam/longest-word/dico"
"log"
"net/http"
"os"
"sync"
"time"
)
const maxLetters = 1000002
const dicoFilename = "mots.txt"
const resourcesDirname = "resources"
var dic dico.Dico
var dicWait sync.WaitGroup
func main() {
var startedAt = time.Now()
dicoFile, errDico := os.Open(dicoFilename)
if errDico != nil {
panic(fmt.Sprintf("did not manage to open dico file at specified path \"%s\" (error: %s)", dicoFilename, errDico))
}
resourcesDir, errRes := os.Open(resourcesDirname)
if errRes != nil {
panic(fmt.Sprintf("did not find resources dir at path \"%s\" (error: %s)", resourcesDirname, errRes))
}
resourcesDir.Close()
//init dico
dicWait.Add(1)
go func() {
defer dicoFile.Close()
dic = dico.New(dicoFile)
dicWait.Add(-1)
log.Printf("dictionnary ready (%s ellapsed)\n", time.Since(startedAt))
}()
http.Handle("/query", http.HandlerFunc(query))
http.Handle("/", http.FileServer(http.Dir(resourcesDirname)))
var port = ":" + os.Getenv("PORT")
log.Printf("server started in %s, listening on %s\n", time.Since(startedAt), port)
var err = http.ListenAndServe(port, nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func query(w http.ResponseWriter, req *http.Request) {
var receivedAt = time.Now()
req.ParseForm()
var query = req.Form.Get("q")
if len(query) > maxLetters {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "Too much (more than", maxLetters, ") letters")
query = query[0:maxLetters] + "..."
} else {
dicWait.Wait()
var result = dic.Find(query)
json.NewEncoder(w).Encode(result)
}
log.Println("handled request \"", query, "\" in", time.Since(receivedAt))
}