mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-06 02:36:40 +00:00
Base project
This commit is contained in:
commit
a958cdeb99
11 changed files with 160 additions and 0 deletions
90
controllers/routes.go
Normal file
90
controllers/routes.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/rjNemo/go-wiki/models"
|
||||
)
|
||||
|
||||
// func ParseTemplates() *template.Template {
|
||||
// return template.Must(template.ParseFiles("templates/edit.html", "templates/view.html")) // add slice of fileNAmes
|
||||
// }
|
||||
|
||||
func RegisteredRoutes() {
|
||||
// http.HandleFunc("/", loveHandler)
|
||||
http.HandleFunc("/view/", makeHandler(viewHandler))
|
||||
http.HandleFunc("/edit/", makeHandler(editHandler))
|
||||
http.HandleFunc("/save/", makeHandler(saveHandler))
|
||||
http.HandleFunc("/", homeHandler)
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
// func loveHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// title := r.URL.Path[1:]
|
||||
// p := models.NewPage(title, nil) // already a pointer
|
||||
// renderTemplate(w, "love", p)
|
||||
// }
|
||||
|
||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// viewHandler(w, r, "FrontPage")
|
||||
p := models.BlankPage()
|
||||
renderTemplate(w, "home", p)
|
||||
}
|
||||
|
||||
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||
p, err := models.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 := models.LoadPage(title)
|
||||
if err != nil {
|
||||
p = models.NewPage(title, nil)
|
||||
}
|
||||
renderTemplate(w, "edit", p)
|
||||
}
|
||||
|
||||
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||
body := r.FormValue("body")
|
||||
p := models.NewPage(title, []byte(body))
|
||||
err := p.Save()
|
||||
checkError(err, w)
|
||||
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p)
|
||||
t, err := template.ParseFiles("templates/" + tmpl + ".html")
|
||||
checkError(err, w)
|
||||
err = t.Execute(w, p)
|
||||
checkError(err, w)
|
||||
}
|
||||
|
||||
func checkError(err error, w http.ResponseWriter) {
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
|
||||
|
||||
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])
|
||||
}
|
||||
}
|
||||
1
data/TestPage.txt
Normal file
1
data/TestPage.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
This is a sample page
|
||||
1
data/chocolate.txt
Normal file
1
data/chocolate.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
I like it very much
|
||||
1
data/test.txt
Normal file
1
data/test.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
Hello
|
||||
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module github.com/rjNemo/go-wiki
|
||||
|
||||
go 1.14
|
||||
13
main.go
Normal file
13
main.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/rjNemo/go-wiki/controllers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Start Go-wiki server at %s\n", time.Now())
|
||||
controllers.RegisteredRoutes()
|
||||
}
|
||||
32
models/page.go
Normal file
32
models/page.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package models
|
||||
|
||||
import "io/ioutil"
|
||||
|
||||
type Page struct {
|
||||
Title string
|
||||
Body []byte
|
||||
}
|
||||
|
||||
// write constructor blank page
|
||||
|
||||
func BlankPage() *Page {
|
||||
return &Page{Title: "Empty page", Body: []byte("Write some content")}
|
||||
}
|
||||
|
||||
func NewPage(title string, body []byte) *Page {
|
||||
return &Page{Title: title, Body: body}
|
||||
}
|
||||
|
||||
func (p *Page) Save() error {
|
||||
fileName := "data/" + p.Title + ".txt"
|
||||
return ioutil.WriteFile(fileName, p.Body, 0600)
|
||||
}
|
||||
|
||||
func LoadPage(title string) (*Page, error) {
|
||||
fileName := "data/" + title + ".txt"
|
||||
body, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Page{Title: title, Body: body}, nil
|
||||
}
|
||||
8
templates/edit.html
Normal file
8
templates/edit.html
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<h1>Editing {{.Title}}</h1>
|
||||
|
||||
<form action="/save/{{.Title}}" method="POST">
|
||||
<div>
|
||||
<textarea name="body" rows="20" cols="80">{{printf "%s" .Body}}</textarea>
|
||||
</div>
|
||||
<div><input type="submit" value="Save" /></div>
|
||||
</form>
|
||||
5
templates/home.html
Normal file
5
templates/home.html
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<h1>Hi there, Welcome to Go-Wiki</h1>
|
||||
|
||||
<div>
|
||||
<h2>Useful links</h2>
|
||||
</div>
|
||||
1
templates/love.html
Normal file
1
templates/love.html
Normal file
|
|
@ -0,0 +1 @@
|
|||
<h1>Hi there, I love {{.Title}}</h1>
|
||||
5
templates/view.html
Normal file
5
templates/view.html
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<h1>{{.Title}}</h1>
|
||||
|
||||
<p>[<a href="/edit/{{.Title}}">edit</a>]</p>
|
||||
|
||||
<div>{{printf "%s" .Body}}</div>
|
||||
Loading…
Reference in a new issue