mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-06 02:36:40 +00:00
refactor
This commit is contained in:
parent
a958cdeb99
commit
f35cc2d2fd
5 changed files with 27 additions and 18 deletions
|
|
@ -1,12 +1,13 @@
|
||||||
package controllers
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/rjNemo/go-wiki/models"
|
"github.com/rjNemo/go-wiki/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func ParseTemplates() *template.Template {
|
// func ParseTemplates() *template.Template {
|
||||||
|
|
@ -19,23 +20,28 @@ func RegisteredRoutes() {
|
||||||
http.HandleFunc("/edit/", makeHandler(editHandler))
|
http.HandleFunc("/edit/", makeHandler(editHandler))
|
||||||
http.HandleFunc("/save/", makeHandler(saveHandler))
|
http.HandleFunc("/save/", makeHandler(saveHandler))
|
||||||
http.HandleFunc("/", homeHandler)
|
http.HandleFunc("/", homeHandler)
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Fatal(server(Port))
|
||||||
|
}
|
||||||
|
|
||||||
|
func server(p int) error {
|
||||||
|
port := ":" + strconv.Itoa(p)
|
||||||
|
return http.ListenAndServe(port, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func loveHandler(w http.ResponseWriter, r *http.Request) {
|
// func loveHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// title := r.URL.Path[1:]
|
// title := r.URL.Path[1:]
|
||||||
// p := models.NewPage(title, nil) // already a pointer
|
// p := model.NewPage(title, nil) // already a pointer
|
||||||
// renderTemplate(w, "love", p)
|
// renderTemplate(w, "love", p)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// viewHandler(w, r, "FrontPage")
|
// viewHandler(w, r, "FrontPage")
|
||||||
p := models.BlankPage()
|
p := model.BlankPage()
|
||||||
renderTemplate(w, "home", p)
|
renderTemplate(w, "home", p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
p, err := models.LoadPage(title)
|
p, err := model.LoadPage(title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
||||||
return
|
return
|
||||||
|
|
@ -44,16 +50,16 @@ func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
|
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
p, err := models.LoadPage(title)
|
p, err := model.LoadPage(title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p = models.NewPage(title, nil)
|
p = model.NewPage(title, nil)
|
||||||
}
|
}
|
||||||
renderTemplate(w, "edit", p)
|
renderTemplate(w, "edit", p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
body := r.FormValue("body")
|
body := r.FormValue("body")
|
||||||
p := models.NewPage(title, []byte(body))
|
p := model.NewPage(title, []byte(body))
|
||||||
err := p.Save()
|
err := p.Save()
|
||||||
checkError(err, w)
|
checkError(err, w)
|
||||||
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
||||||
|
|
@ -61,7 +67,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
|
|
||||||
// var templates = template.Must(template.ParseFiles("templates/edit.html", "templates/view.html")) // add slice of fileNAmes
|
// var templates = template.Must(template.ParseFiles("templates/edit.html", "templates/view.html")) // add slice of fileNAmes
|
||||||
|
|
||||||
func renderTemplate(w http.ResponseWriter, tmpl string, p *models.Page) {
|
func renderTemplate(w http.ResponseWriter, tmpl string, p *model.Page) {
|
||||||
// err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p)
|
// err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p)
|
||||||
t, err := template.ParseFiles("templates/" + tmpl + ".html")
|
t, err := template.ParseFiles("templates/" + tmpl + ".html")
|
||||||
checkError(err, w)
|
checkError(err, w)
|
||||||
3
controller/settings.go
Normal file
3
controller/settings.go
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
package controller
|
||||||
|
|
||||||
|
var Port int = 8080
|
||||||
|
|
@ -1 +1 @@
|
||||||
Hello
|
Hello There
|
||||||
6
main.go
6
main.go
|
|
@ -4,10 +4,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rjNemo/go-wiki/controllers"
|
"github.com/rjNemo/go-wiki/controller"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Printf("Start Go-wiki server at %s\n", time.Now())
|
fmt.Printf("Start Go-wiki server on http://localhost:%d at %s\n", controller.Port, time.Now())
|
||||||
controllers.RegisteredRoutes()
|
controller.RegisteredRoutes()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
package models
|
package model
|
||||||
|
|
||||||
import "io/ioutil"
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
type Page struct {
|
type Page struct {
|
||||||
Title string
|
Title string
|
||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// write constructor blank page
|
|
||||||
|
|
||||||
func BlankPage() *Page {
|
func BlankPage() *Page {
|
||||||
return &Page{Title: "Empty page", Body: []byte("Write some content")}
|
return &Page{Title: "Empty page", Body: []byte("Write some content")}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue