02package15 main 02import15 (15  15 05"time" 15 05"fmt" 15 05"html/template" 15 05"net/http" 15 05"database/sql" 15 _05"github.com/go-sql-driver/mysql" ) 02type15 Page15 02struct15 { 15 Title15 10string 15 Body15 []10byte 15 Timestamp15 time.Time } 02func15 (p15 *Page)15 save()15 10error15 { 15 _,15 err15 :=15 stmtIns.Exec(p.Title,15 p.Body) 15 02return15 err15 15// ioutil.WriteFile(filename, p.Body, 0600) } 02func15 loadPage(ident15 10string)15 (*Page,15 10error)15 { 15 s15 :=15 Page{} 15 err15 :=15 stmtOut.QueryRow(ident).Scan(&s.Title,15 &s.Body,15 &s.Timestamp) 15 02if15 err15 !=15 02nil15 { 15 02return15 02nil,15 err 15 } 15 02return15 &s,15 02nil } 02func15 viewHandler(w15 http.ResponseWriter,15 r15 *http.Request)15 { 15 title15 :=15 r.URL.Path[10len(05"/view/"):] 15 02if15 10len(title)15 <15 02115 { 15 http.Redirect(w,15 r,15 05"/",15 http.StatusFound) 15 02return 15 } 15 p,15 err15 :=15 loadPage(title) 15 02if15 err15 !=15 02nil15 { 15 http.NotFoundHandler() 15 }15 02else15 { 15 renderTemplate(w,15 05"view",15 p) 15 } } 02func15 editHandler(w15 http.ResponseWriter,15 r15 *http.Request)15 { 15 title15 :=15 r.URL.Path[10len(05"/edit/"):] 15 p,15 err15 :=15 loadPage(title) 15 02if15 err15 !=15 02nil15 { 15 p15 =15 &Page{Title:15 title} 15 } 15 renderTemplate(w,15 05"edit",15 p) } 02func15 saveHandler(w15 http.ResponseWriter,15 r15 *http.Request)15 { 15 15//title := r.URL.Path[len("/save/"):] 15 15//if len(title) < 1 { 15 15// http.Redirect(w, r, "/", http.StatusFound) 15 15// return 15 15//} 15 title:=15 r.FormValue(05"appname") 15 body15 :=15 r.FormValue(05"body") 15 p15 :=15 &Page{Title:15 title,15 Body:15 []10byte(body)} 15 p.save() 15 http.Redirect(w,15 r,15 05"/view/"+title,15 http.StatusFound) } 02func15 errorHandler(w15 http.ResponseWriter,15 r15 *http.Request,15 status15 10int)15 { 15 w.WriteHeader(status) 15 02if15 status15 ==15 http.StatusNotFound15 { 15 fmt.Fprint(w,15 05"file was not found on server") 15 } } 02var15 templates15 =15 template.Must(template.ParseFiles(05"edit.html",15 05"view.html")) 02func15 renderTemplate(w15 http.ResponseWriter,15 tmpl15 10string,15 p15 *Page)15 { 15 err15 :=15 templates.ExecuteTemplate(w,15 tmpl+05".html",15 p) 15 02if15 err15 !=15 02nil15 { 15 http.Error(w,15 err.Error(),15 http.StatusInternalServerError) 15 } } 15// var db *sql.DB 02var15 stmtIns,15 stmtOut15 *sql.Stmt 02func15 main()15 { 15 db,15 err15 :=15 sql.Open(05"mysql",15 05"root:feelssemibadman@/golang") 15 02if15 err15 !=15 02nil15 { 15 fmt.Printf(05"failed to open mysql handle\n") 15 } 15 02defer15 db.Close() 15 err15 =15 db.Ping() 15 02if15 err15 !=15 02nil15 { 15 fmt.Printf(05"opened handle but failed to communicate with mysql\n") 15 } 15 15// Prepare statement for inserting data 15 stmtIns,15 err15 :=15 db.Prepare(05"INSERT Applications SET AppName=?, Description=?") 15 02if15 err15 !=15 02nil15 { 15 fmt.Printf(err.Error()) 15 } 15 02defer15 stmtIns.Close()15 15// Close the statement when we leave main() / the program terminates 15  15 15// Prepare statement for reading data 15 stmtOut,15 err15 :=15 db.Query(05"SELECT AppName, Description, CreatedOn FROM Applications WHERE AppID=?") 15 02if15 err15 !=15 02nil15 { 15 fmt.Printf(err.Error()) 15 } 15 02defer15 stmtOut.Close() 15 http.HandleFunc(05"/view/",15 viewHandler) 15 http.HandleFunc(05"/edit/",15 editHandler) 15 http.HandleFunc(05"/save/",15 saveHandler) 15 http.ListenAndServe(05":8080",15 02nil) }