-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
162 lines (135 loc) · 4.22 KB
/
test.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"net/http"
"runtime"
"io/ioutil"
"html/template"
"regexp"
"time"
)
var templates = template.Must(template.ParseFiles("edit.html", "view.html"))
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
type Hello struct{}
type Page struct {
Title string
Body []byte
}
func (p *Page) save() error {
filename := p.Title + ".txt"
time.Sleep(5000 * time.Millisecond)
return ioutil.WriteFile(filename, p.Body, 0600)
}
func loadPage(title string) (*Page, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body}, nil
}
func (h Hello) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, "Hello!")
}
type String string
type Struct struct {
Greeting string
Punct string
Who string
}
func(s String) ServeHTTP(
w http.ResponseWriter,
r *http.Request){
//fmt.Println("OUT!")
numCPU := runtime.NumCPU()
fmt.Fprint(w,"Hello string! Number of CPUs: " + fmt.Sprintf("%v",numCPU))
}
func(s Struct) ServeHTTP(
w http.ResponseWriter,
r *http.Request){
fmt.Fprint(w,"Hello STRUCT")
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
return
}
renderTemplate(w, "view", p)
}
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
//err := p.save()
go p.save()
/*if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}*/
//http.Redirect(w, r, "/view/"+title, http.StatusFound)
renderTemplate(w, "view", p)
}
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
fn(w, r, m[2])
}
}
func main() {
http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
//http.Handle("/", &Hello{})
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/",func (w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w,"<html><body>")
fmt.Fprintf(w, "Hi there, I love <b>%s</b>!<br>", r.URL.Path[1:])
vs,_ := ioutil.ReadDir(".")
for _,v := range vs {
name := v.Name()
name_without_ext := name[:len(name)-4]
name_ext := name[len(name)-3:]
if name_ext == "txt" {
fmt.Fprintf(w,"[<a href=\"/view/%s\">%s</a>]<br>",name_without_ext,name_without_ext)
}
}
fmt.Fprintf(w,"</html></body>")
})
i := 1
go func(){
for{
time.Sleep(5000 * time.Millisecond)
filename := fmt.Sprintf("autofile%v.txt",i)
ioutil.WriteFile(filename, []byte("auto"), 0600)
i++
}
}()
http.ListenAndServe("localhost:4000", nil)
/*p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")}
p1.save()
p2, _ := loadPage("TestPage")
fmt.Println(string(p2.Body))*/
/*m := image.NewRGBA(image.Rect(0, 0, 100, 100))
fmt.Println(m.Bounds())
fmt.Println(m.At(0, 0).RGBA())*/
}