forked from shijuvar/go-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
55 lines (48 loc) · 1.07 KB
/
task.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
package task
import (
"fmt"
"html/template"
"net/http"
)
type Task struct {
Name string
Description string
}
const taskForm = `
<html>
<body>
<form action="/task" method="post">
<p>Task Name: <input type="text" name="taskname" ></p>
<p> Description: <input type="text" name="description" ></p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
`
const taskTemplateHTML = `
<html>
<body>
<p>New Task has been created:</p>
<div>Task: {{.Name}}</div>
<div>Description: {{.Description}}</div>
</body>
</html>
`
var taskTemplate = template.Must(template.New("task").Parse(taskTemplateHTML))
func init() {
http.HandleFunc("/", index)
http.HandleFunc("/task", task)
}
func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, taskForm)
}
func task(w http.ResponseWriter, r *http.Request) {
task := Task{
Name: r.FormValue("taskname"),
Description: r.FormValue("description"),
}
err := taskTemplate.Execute(w, task)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}