-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (82 loc) · 2.13 KB
/
main.go
File metadata and controls
100 lines (82 loc) · 2.13 KB
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
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"text/template"
)
//Schedule for struct
type Schedule struct {
Input [][]int
Result [][]int
Avwt float64
Avtat float64
}
// main program
func main() {
runtime.GOMAXPROCS(2)
http.HandleFunc("/", routeIndexGet)
http.Handle("/static/",
http.StripPrefix("/static/",
http.FileServer(http.Dir("assets"))))
fmt.Println("server started at localhost:9000")
http.ListenAndServe(":9000", nil)
}
func routeIndexGet(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
var tmpl = template.Must(template.New("form").ParseFiles("index.html"))
if err := r.ParseMultipartForm(1024); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
uploadedFile, handler, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer uploadedFile.Close()
dir, err := os.Getwd()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
filename := handler.Filename
fileLocation := filepath.Join(dir, "assets/files", filename)
targetFile, err := os.OpenFile(fileLocation, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer targetFile.Close()
if _, err := io.Copy(targetFile, uploadedFile); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
filelocation := "assets/files/" + filename
in, initial := newTaskFromFile(filelocation)
quicksort(in)
result, avwt, avtat := schedulingProcess(in)
data := Schedule{
Input: initial,
Result: result,
Avwt: avwt,
Avtat: avtat,
}
err = tmpl.ExecuteTemplate(w, "form", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
case "GET":
var tmpl = template.Must(template.New("form").ParseFiles("index.html"))
var err = tmpl.ExecuteTemplate(w, "form", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
default:
http.Error(w, "", http.StatusBadRequest)
}
}