-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
58 lines (54 loc) · 1.32 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
package main
import (
"log"
"net/http"
"io"
"os"
"sort"
"github.com/igorgubernat/quicksort"
"time"
"strconv"
"fmt"
"strings"
)
func main () {
port := os.Getenv("PORT")
http.HandleFunc("/", form)
http.HandleFunc("/result", result)
http.ListenAndServe(":" + port, nil)
}
func form (w http.ResponseWriter, r *http.Request) {
f, err := os.Open("templates/form.html")
if err != nil {
log.Fatal(err)
}
io.Copy(w, f)
}
func result (w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var errmsg string
var numbers []int
goroutines, err := strconv.Atoi(r.PostForm["goroutines"][0])
if err != nil {
errmsg = r.PostForm["goroutines"][0] + " is not a number.\n"
}
s := strings.Split(r.PostForm["numbers"][0], " ")
for _, val := range s {
num, err := strconv.Atoi(val)
if err != nil {
errmsg += val + " is not a number.\n"
} else {
numbers = append(numbers, num)
}
}
if errmsg != "" {
http.Error(w, errmsg, http.StatusBadRequest)
return
}
data := sort.IntSlice(numbers)
start := time.Now()
quicksort.QuickSort(data, goroutines)
end := time.Now()
fmt.Fprint(w, "Result: ", data, "\n")
fmt.Fprint(w, "Time : ", end.Sub(start))
}