settings file, send interface, page fields getters, setters, tests, readEnv in main

This commit is contained in:
Ruidy Nemausat 2020-03-15 23:51:12 +01:00
parent 25afd5916c
commit 4a1a6139ee
11 changed files with 104 additions and 33 deletions

1
.envTest Normal file
View file

@ -0,0 +1 @@
PORT=6543

View file

@ -0,0 +1,22 @@
package controller
import (
"io"
"net/http"
"testing"
"github.com/rjNemo/go-wiki/service"
)
func TestParseContactForm(t *testing.T) {
var i io.Reader
r, err := http.NewRequest(http.MethodPost, "/contact/post/", i)
if err != nil {
t.Errorf("%s", err)
}
ans := parseContactForm(r)
if ans != service.NewMail("", "") {
t.Errorf("parseContactForm(r) = %v", ans)
}
}

View file

@ -8,6 +8,7 @@ import (
"github.com/rjNemo/go-wiki/model" "github.com/rjNemo/go-wiki/model"
"github.com/rjNemo/go-wiki/service" "github.com/rjNemo/go-wiki/service"
"github.com/rjNemo/go-wiki/settings"
) )
// func ParseTemplates() *template.Template { // func ParseTemplates() *template.Template {
@ -21,7 +22,7 @@ func Router() {
http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler)) http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/contact/", contactHandler) http.HandleFunc("/contact/", contactHandler)
http.HandleFunc("/contact/post", postContactHandler) http.HandleFunc("/contact/post/", postContactHandler)
http.HandleFunc("/", homeHandler) http.HandleFunc("/", homeHandler)
} }
@ -68,7 +69,7 @@ func postContactHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm() err := r.ParseForm()
checkError(err, w) // bad error handling checkError(err, w) // bad error handling
mail := parseContactForm(r) mail := parseContactForm(r)
service.MailClient(mail) mail.Send()
fmt.Println(mail) fmt.Println(mail)
renderTemplate(w, "contact_sent", nil) renderTemplate(w, "contact_sent", nil)
} }
@ -88,7 +89,7 @@ func renderTemplate(w http.ResponseWriter, tmpl string, p *model.Page) {
} }
func getTmplName(tmpl string) string { func getTmplName(tmpl string) string {
return tmplDir + tmpl + ".html" return settings.TmplDir + tmpl + ".html"
} }
func checkError(err error, w http.ResponseWriter) { func checkError(err error, w http.ResponseWriter) {

View file

@ -1,24 +0,0 @@
package controller
import (
"log"
"os"
"github.com/joho/godotenv"
)
// Port value
var Port string = readEnv(".env")
var tmplDir string = "templates/"
func readEnv(f string) string {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
port := os.Getenv("PORT")
if port == "" {
log.Fatal("PORT must be set")
}
return port
}

BIN
go-wiki Executable file

Binary file not shown.

23
main.go
View file

@ -3,19 +3,34 @@ package main
import ( import (
"log" "log"
"net/http" "net/http"
"os"
"github.com/joho/godotenv"
"github.com/rjNemo/go-wiki/controller" "github.com/rjNemo/go-wiki/controller"
"github.com/rjNemo/go-wiki/service" "github.com/rjNemo/go-wiki/settings"
) )
var port string = readEnv(settings.EnvFile)
func main() { func main() {
service.PaymentIntent(1000, "ruidy.nemausat@gmail.com") startServer(port, controller.Router)
startServer(controller.Port, controller.Router)
} }
func startServer(p string, r func()) { func startServer(p string, r func()) {
log.Printf("Start Go-wiki server on http://localhost:%s\n", controller.Port) log.Printf("Start Go-wiki server on http://localhost:%s\n", port)
port := ":" + p port := ":" + p
r() r()
log.Fatal(http.ListenAndServe(port, nil)) log.Fatal(http.ListenAndServe(port, nil))
} }
func readEnv(f string) string {
err := godotenv.Load(f)
if err != nil {
log.Fatal("Error loading .env file")
}
port := os.Getenv("PORT")
if port == "" {
log.Fatal("PORT must be set. Consider verify your EnvFile.")
}
return port
}

20
main_test.go Normal file
View file

@ -0,0 +1,20 @@
package main
import (
"os"
"testing"
)
func TestReadEnv(t *testing.T) {
e, err := os.Create(".envTest")
if err != nil {
t.Errorf("Error creating file %s", err)
}
e.Write([]byte("PORT=6543"))
defer e.Close()
ans := readEnv(".envTest")
if ans != "6543" {
t.Errorf("readEnv('.envTest') = %s ; want 6543", ans)
}
}

View file

@ -10,6 +10,26 @@ type Page struct {
body []byte body []byte
} }
// Title exposes Page's title field
func (p Page) Title() string {
return p.title
}
// SetTitle edits Page's title field
func (p *Page) SetTitle(s string) {
p.title = s
}
// Body exposes Page's body field
func (p Page) Body() []byte {
return p.body
}
// SetBody edits Page's body field
func (p *Page) SetBody(b []byte) {
p.body = b
}
// BlankPage constructor returns a pointer to a blank Page. // BlankPage constructor returns a pointer to a 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")}

View file

@ -14,6 +14,13 @@ type Mail struct {
message string message string
} }
// Sender objects implements Send() method
type Sender interface {
Send()
}
var _ Sender = &Mail{} // verifies if Mail implements Sender
var hostMail string = "ruidy.nemausat@gmail.com" var hostMail string = "ruidy.nemausat@gmail.com"
var smtpServer string = "smtp.gmail.com:587" var smtpServer string = "smtp.gmail.com:587"
@ -22,8 +29,8 @@ func NewMail(from, body string) Mail {
return Mail{from, body} return Mail{from, body}
} }
// MailClient runs a mail client and send mail // Send runs a mail client and send mail
func MailClient(m Mail) { func (m *Mail) Send() {
c, err := smtp.Dial(smtpServer) c, err := smtp.Dial(smtpServer)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View file

@ -8,6 +8,8 @@ import (
// secret API key // secret API key
var secretKey string = "sk_test_dyp7eLEq2KnxpWE0qIMihTYZ" var secretKey string = "sk_test_dyp7eLEq2KnxpWE0qIMihTYZ"
// service.PaymentIntent(1000, "ruidy.nemausat@gmail.com")
// PaymentIntent creates a payment intent // PaymentIntent creates a payment intent
func PaymentIntent(a int64, m string) (*stripe.PaymentIntent, error) { func PaymentIntent(a int64, m string) (*stripe.PaymentIntent, error) {
stripe.Key = secretKey stripe.Key = secretKey

7
settings/settings.go Normal file
View file

@ -0,0 +1,7 @@
package settings
// EnvFile must be set to the address of the main .env file.
const EnvFile string = ".env"
// TmplDir must be set to the address of the templates folder.
const TmplDir string = "templates/"