-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathajax.go
85 lines (75 loc) · 1.89 KB
/
ajax.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
package main
import (
"net/http"
"fmt"
"log"
"path/filepath"
"github.com/hoisie/mustache"
"strings"
"math/rand"
)
func testajax(_a []string) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := filepath.Clean(r.URL.Path)
log.Printf("%s %s", r.Method, path)
_, file := filepath.Split(path)
ext := filepath.Ext(file)
r.ParseForm()
switch ext {
case ".ts", ".html", ".css", ".js", ".mp4", ".rm", ".rmvb", ".avi", ".mkv":
http.ServeFile(w, r, path[1:])
}
if path == "/dumpform" {
fmt.Fprintf(w, "form = ")
jsonWrite(w, r.Form)
return
}
if path == "/seldel" {
fmt.Fprintf(w, `<form>`)
for i := 0; i < 4; i++ {
fmt.Fprintf(w, `<input class="input" type="checkbox" value="%d" name="sel">%d</input>`, i, i)
}
fmt.Fprintf(w, `</form>`)
fmt.Fprintf(w, `<a class="btn" do="ok form">Del</a>`)
}
if path == "/dumpform1" {
fmt.Fprintf(w, "form = ")
jsonWrite(w, r.Form)
fmt.Fprintf(w, `<a class="btn" do="ok">Ok</a>`)
fmt.Fprintf(w, `<a class="btn" do="cancel">Cancel</a>`)
return
}
if path == "/err" {
http.Error(w, "something happens", 404)
return
}
if path == "/mod1" {
fmt.Fprintf(w, `<p>module1</p>`)
fmt.Fprintf(w, `<a class="btn" do="get $t 'p=banana&p=orange&p=apple'">Fruits</a>`)
fmt.Fprintf(w, `<a class="btn" do="get $t 'p=alien&p=human&p=child'">Humans</a>`)
}
if r.Method == "POST" {
switch path {
case "/randstr":
jsonWrite(w, hash{"list":rand.Intn(400)})
}
return
}
switch path {
case "/":
str := mustache.RenderFile("tpl/ajax.html")
fmt.Fprintf(w, "%s", str)
case "/showlist1":
list := r.FormValue("list")
if list != "" {
for _, s := range strings.Split(list, ",") {
fmt.Fprintf(w, `<a class="btn" href="#" listdel="%s">%s</a>`, s, s)
}
}
}
})
err := http.ListenAndServe(":9192", nil)
if err != nil {
log.Printf("%v", err)
}
}