mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-06 02:36:40 +00:00
db
This commit is contained in:
parent
a2a3022b2a
commit
5313ef834f
10 changed files with 144 additions and 14 deletions
|
|
@ -1,12 +1,13 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/rjNemo/go-wiki/model"
|
||||
"github.com/rjNemo/go-wiki/service"
|
||||
)
|
||||
|
||||
// func ParseTemplates() *template.Template {
|
||||
|
|
@ -18,13 +19,9 @@ func RegisteredRoutes() {
|
|||
http.HandleFunc("/view/", makeHandler(viewHandler))
|
||||
http.HandleFunc("/edit/", makeHandler(editHandler))
|
||||
http.HandleFunc("/save/", makeHandler(saveHandler))
|
||||
http.HandleFunc("/contact/", contactHandler)
|
||||
http.HandleFunc("/contact/post", postContactHandler)
|
||||
http.HandleFunc("/", homeHandler)
|
||||
log.Fatal(server(Port))
|
||||
}
|
||||
|
||||
func server(p string) error {
|
||||
port := ":" + p
|
||||
return http.ListenAndServe(port, nil)
|
||||
}
|
||||
|
||||
// func loveHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -34,9 +31,7 @@ func server(p string) error {
|
|||
// }
|
||||
|
||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// viewHandler(w, r, "FrontPage")
|
||||
p := model.BlankPage()
|
||||
renderTemplate(w, "home", p)
|
||||
renderTemplate(w, "home", nil)
|
||||
}
|
||||
|
||||
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||
|
|
@ -64,6 +59,23 @@ func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
|||
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
||||
}
|
||||
|
||||
func contactHandler(w http.ResponseWriter, r *http.Request) {
|
||||
renderTemplate(w, "contact", nil)
|
||||
}
|
||||
|
||||
func postContactHandler(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
checkError(err, w) // bad error handling
|
||||
mail := parseContactForm(r)
|
||||
service.MailClient(mail)
|
||||
fmt.Println(mail)
|
||||
renderTemplate(w, "contact_sent", nil)
|
||||
}
|
||||
|
||||
func parseContactForm(r *http.Request) service.Mail {
|
||||
return service.NewMail(r.PostFormValue("email"), r.PostFormValue("message"))
|
||||
}
|
||||
|
||||
// var templates = template.Must(template.ParseFiles("templates/edit.html", "templates/view.html")) // add slice of fileNAmes
|
||||
|
||||
func renderTemplate(w http.ResponseWriter, tmpl string, p *model.Page) {
|
||||
|
|
@ -77,6 +89,7 @@ func renderTemplate(w http.ResponseWriter, tmpl string, p *model.Page) {
|
|||
func getTmplName(tmpl string) string {
|
||||
return tmplDir + tmpl + ".html"
|
||||
}
|
||||
|
||||
func checkError(err error, w http.ResponseWriter) {
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
|
|
|||
17
data/database.go
Normal file
17
data/database.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
connStr := "user=nemausat dbname=godb sslmode=verify-full"
|
||||
db, err := sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
}
|
||||
|
|
@ -12,7 +12,7 @@ import (
|
|||
var exJson string = "https://jsonplaceholder.typicode.com/todos/1"
|
||||
|
||||
func Main() {
|
||||
b := fetchApi(exJson)
|
||||
b := FetchApi(exJson)
|
||||
fmt.Println(b)
|
||||
}
|
||||
|
||||
|
|
|
|||
10
main.go
10
main.go
|
|
@ -2,6 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/rjNemo/go-wiki/controller"
|
||||
|
|
@ -9,5 +11,11 @@ import (
|
|||
|
||||
func main() {
|
||||
fmt.Printf("Start Go-wiki server on http://localhost:%s at %s\n", controller.Port, time.Now())
|
||||
controller.RegisteredRoutes()
|
||||
startServer(controller.Port)
|
||||
}
|
||||
|
||||
func startServer(p string) {
|
||||
port := ":" + p
|
||||
controller.RegisteredRoutes()
|
||||
log.Fatal(http.ListenAndServe(port, nil))
|
||||
}
|
||||
|
|
|
|||
56
service/mail.go
Normal file
56
service/mail.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
type Mail struct {
|
||||
// Contact mail address
|
||||
from string
|
||||
// Mail body
|
||||
message string
|
||||
}
|
||||
|
||||
var hostMail string = "ruidy.nemausat@gmail.com"
|
||||
var smtpServer string = "smtp.gmail.com:587"
|
||||
|
||||
func NewMail(from, body string) Mail {
|
||||
return Mail{from, body}
|
||||
}
|
||||
|
||||
func MailClient(m Mail) {
|
||||
c, err := smtp.Dial(smtpServer)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// c.StartTLS()
|
||||
|
||||
if err := c.Mail(hostMail); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := c.Rcpt(hostMail); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
wc, err := c.Data()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprintf(wc, m.message); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := wc.Close(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = c.Quit()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -29,6 +29,15 @@
|
|||
>
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item active">
|
||||
<a class="nav-link" href="/contact"
|
||||
>Contact <span class="sr-only">(current)</span></a
|
||||
>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<form class="form-inline my-2 my-lg-0">
|
||||
|
|
|
|||
17
templates/contact.html
Normal file
17
templates/contact.html
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
{{define "title"}} Contact Us | Go-Wiki {{end}} {{define "content"}}
|
||||
<h1>Contact us</h1>
|
||||
|
||||
<form action="/contact/post" method="POST">
|
||||
<div class="form-group">
|
||||
<label for="email">Email</label>
|
||||
<input type="email" class="form-control" name="email" />
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="message">Message</label>
|
||||
<textarea class="form-control" name="message" rows="10"> </textarea>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input type="submit" value="Save" class="btn btn-primary" />
|
||||
</div>
|
||||
</form>
|
||||
{{end}}
|
||||
8
templates/contact_sent.html
Normal file
8
templates/contact_sent.html
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{{define "title"}} Contact Us | Go-Wiki {{end}} {{define "content"}}
|
||||
<h1>Thanks for your mail</h1>
|
||||
|
||||
<div class="section">
|
||||
<a class="btn btn-primary" href="/">Return to Home</a>
|
||||
</div>
|
||||
|
||||
{{end}}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
<form action="/save/{{.Title}}" method="POST">
|
||||
<div class="form-group">
|
||||
<textarea class="form-control" name="body" rows="20" cols="80">
|
||||
<textarea class="form-control" name="body" rows="10">
|
||||
{{printf "%s" .Body}}
|
||||
</textarea>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
{{define "title"}} {{.Title}} | Go-Wiki {{end}} {{define "content"}}
|
||||
<h1>{{.Title}}</h1>
|
||||
|
||||
<p>[<a href="/edit/{{.Title}}">edit</a>]</p>
|
||||
<p>
|
||||
<a class="badge-pill badge-primary" href="/edit/{{.Title}}">Edit </a>
|
||||
</p>
|
||||
|
||||
<div>{{printf "%s" .Body}}</div>
|
||||
{{end}}
|
||||
|
|
|
|||
Loading…
Reference in a new issue