mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-06 02:36:40 +00:00
db
This commit is contained in:
parent
9425dc16f6
commit
17387f25a8
8 changed files with 44 additions and 12 deletions
|
|
@ -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))
|
||||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// Port value
|
||||
var Port string = readEnv(".env")
|
||||
var tmplDir string = "templates/"
|
||||
|
||||
|
|
|
|||
5
go.mod
5
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
|
||||
)
|
||||
|
|
|
|||
2
go.sum
2
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=
|
||||
|
|
|
|||
6
main.go
6
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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
17
model/page_test.go
Normal file
17
model/page_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue