diff --git a/controller/routes.go b/controller/router.go similarity index 97% rename from controller/routes.go rename to controller/router.go index 909de23..6a18464 100644 --- a/controller/routes.go +++ b/controller/router.go @@ -14,7 +14,8 @@ import ( // return template.Must(template.ParseFiles("templates/edit.html", "templates/view.html")) // add slice of fileNAmes // } -func RegisteredRoutes() { +// Router dispatch the request to the corresponding route handlers. +func Router() { // http.HandleFunc("/", loveHandler) http.HandleFunc("/view/", makeHandler(viewHandler)) http.HandleFunc("/edit/", makeHandler(editHandler)) diff --git a/controller/settings.go b/controller/settings.go index 4174e6f..a8fa7fe 100644 --- a/controller/settings.go +++ b/controller/settings.go @@ -7,6 +7,7 @@ import ( "github.com/joho/godotenv" ) +// Port value var Port string = readEnv(".env") var tmplDir string = "templates/" diff --git a/go.mod b/go.mod index f070856..20cf4a8 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/rjNemo/go-wiki go 1.14 -require github.com/joho/godotenv v1.3.0 +require ( + github.com/joho/godotenv v1.3.0 + github.com/lib/pq v1.3.0 +) diff --git a/go.sum b/go.sum index ead7071..9d15241 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU= +github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= diff --git a/main.go b/main.go index c1d98cd..bb53824 100644 --- a/main.go +++ b/main.go @@ -8,12 +8,12 @@ import ( ) func main() { - startServer(controller.Port) + startServer(controller.Port, controller.Router) } -func startServer(p string) { +func startServer(p string, r func()) { log.Printf("Start Go-wiki server on http://localhost:%s\n", controller.Port) port := ":" + p - controller.RegisteredRoutes() + r() log.Fatal(http.ListenAndServe(port, nil)) } diff --git a/model/page.go b/model/page.go index c059a9b..7602bd4 100644 --- a/model/page.go +++ b/model/page.go @@ -4,29 +4,34 @@ import ( "io/ioutil" ) +// A Page own a wiki page data and has a title and a body. type Page struct { - Title string - Body []byte + title string + body []byte } +// BlankPage constructor returns a pointer to a blank Page. func BlankPage() *Page { - return &Page{Title: "Empty page", Body: []byte("Write some content")} + return &Page{title: "Empty page", body: []byte("Write some content")} } +// NewPage constructor returns a pointer to a sample Page. func NewPage(title string, body []byte) *Page { - return &Page{Title: title, Body: body} + return &Page{title: title, body: body} } +// Save a page to the 'data/' folder in txt format. func (p *Page) Save() error { - fileName := "data/" + p.Title + ".txt" - return ioutil.WriteFile(fileName, p.Body, 0600) + fileName := "data/" + p.title + ".txt" + return ioutil.WriteFile(fileName, p.body, 0600) } +// LoadPage reads a saved page data and returns a pointer to the Page. 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 + return &Page{title: title, body: body}, nil } diff --git a/model/page_test.go b/model/page_test.go new file mode 100644 index 0000000..11faeb4 --- /dev/null +++ b/model/page_test.go @@ -0,0 +1,17 @@ +package model + +import "testing" + +func TestBlankPage(t *testing.T) { + ans := BlankPage() + if ans.title != "Empty page" && string(ans.body) != "Write some content" { + t.Errorf("BlankPage() = %v; want &Page{title: 'Empty page', body: []byte('Write some content')}", ans) + } +} + +func TestNewPage(t *testing.T) { + ans := NewPage("Test Page", []byte("This is a sample page")) + if ans.title != "Test Page" && string(ans.body) != "This is a sample page" { + t.Errorf("NewPage() = %v; want &Page{title: 'Test Page', body: []byte('This is a sample page')}", ans) + } +} diff --git a/service/mail.go b/service/mail.go index 1f20de1..3455952 100644 --- a/service/mail.go +++ b/service/mail.go @@ -6,6 +6,7 @@ import ( "net/smtp" ) +// Mail holds contact form information type Mail struct { // Contact mail address from string @@ -16,10 +17,12 @@ type Mail struct { var hostMail string = "ruidy.nemausat@gmail.com" var smtpServer string = "smtp.gmail.com:587" +// NewMail creates a Mail with from and body fields func NewMail(from, body string) Mail { return Mail{from, body} } +// MailClient runs a mail client and send mail func MailClient(m Mail) { c, err := smtp.Dial(smtpServer) if err != nil {