-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki.go
192 lines (158 loc) · 4.66 KB
/
wiki.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"fmt"
"io/ioutil"
"net/http"
"html/template"
"regexp"
"errors"
"os"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type Page struct {
Title string
Body []byte
}
type User struct {
UserName string
Email string
Password string
}
var templates = template.Must(template.ParseFiles("public/edit.html", "public/view.html", "public/main.html"))
//var Tmls = template.Must(template.ParseGlob("../public/*"))
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
func getStaticFiles(w http.ResponseWriter, r *http.Request) {
dir, _ := os.Getwd()
//Tmls.ExecuteTemplate(w, "main", nil)
fmt.Fprintf(w, "The current directory: %s", dir)
}
func (p *Page) save() error {
filename := p.Title + ".txt"
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 handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func mPageHandler(w http.ResponseWriter, r *http.Request){
err := templates.ExecuteTemplate(w, "main.html", nil)
if err != nil {
return
} else {
fmt.Println("Sucessfully ")
}
}
func addUser(w http.ResponseWriter, r *http.Request){
if r.Method == http.MethodPost {
email := r.PostFormValue("email")
username := r.PostFormValue("username")
password := r.PostFormValue("password")
aUser := User{username, email, password }
fmt.Fprintf(w, "Email: %s \n Username: %s \n Password: %s \n", aUser.Email, aUser.UserName, aUser.Password)
}
return
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
//title := r.URL.Path[len("/view/"):]
title, err := getTitle(w, r)
if err != nil {
return
}
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 := r.URL.Path[len("/edit/"):]
title, err := getTitle(w, r)
if err != nil {
return
}
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request){
//title := r.URL.Path[len("/save/"):]
title, err := getTitle(w, r)
if err != nil {
return
}
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
err = p.save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
func getTitle(w http.ResponseWriter, r *http.Request) (string, error) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return "", errors.New("invalid page title")
}
return m[2], nil // The title is the second subexpression.
}
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 main() {
p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")}
p1.save()
p2, _ := loadPage("TestPage")
fmt.Println(string(p2.Body))
//user@cloudsql(project-id:regionname:instance-name)/dbname
//db, err := sql.Open("mysql", "sean7218@cloudql(fatigue-creep-me-vpi:us-central1:fatigue-v17501)/mydb")
// Setup database
db, err := sql.Open("mysql", "sean7218:SZ12192017!@cloudsql(fatigue-creep-me-vpi:us-central1:fatigue-v17501)/mydb")
//db, err := sql.Open("mysql", "szhang:password@unix(/tmp/mysql.sock)/user?loc=Local")
//db, err := sql.Open("mysql", "sean7218:SZ12192017!@unix(/cloudsql/fatigue-creep-me-vpi:us-central1:fatigue-v17501)/mydb")
if err != nil {
panic(err.Error())
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err.Error())
}
setupJSON()
//setupCJWT()
//a := AuthHandler{ db }
//l := LoginHandler{ db}
// this is tutorial from the golang.org
http.HandleFunc("/", handler)
http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler)
http.HandleFunc("/save/", saveHandler)
// main page for login and register
http.HandleFunc("/main/", mPageHandler)
// show how to serve static files
http.HandleFunc("/getStaticFile", getStaticFiles)
// pattern difference between register and login
http.Handle("/register", registerHandler(db))
http.Handle("/login", &LoginHandler{db })
// demonstrate how to serve json
http.HandleFunc("/getDrawing/", setupUsers )
// Middleware to handle secure route
http.Handle("/sendProtected", sendProtected())
http.Handle("/adapt", Adapt(sendProtected(), isAuthenticated()))
http.ListenAndServe(":8080", nil)
}