work on /add - start fetching and trying to parse ?url=

This commit is contained in:
Dan Ballard 2015-05-03 09:31:36 -07:00
parent fe41cc2405
commit 5f5e69a298
2 changed files with 24 additions and 9 deletions

View File

@ -7,6 +7,8 @@ import (
"github.com/dballard/transmet/user"
"fmt"
"time"
"io/ioutil"
"regexp"
)
func GetFlashes(session *sessions.Session) map[string]interface{} {
@ -53,8 +55,8 @@ func getPostHandler(getFn, postFn func(http.ResponseWriter, *http.Request)) func
}
}
func ShowTemplate(template string, w http.ResponseWriter, r *http.Request, user *user.User) {
err := templates[template].Execute(w, map[string]interface{}{"user": user})
func ShowTemplate(template string, w http.ResponseWriter, data map[string]interface{}) {
err := templates[template].Execute(w, data)
if err != nil {
fmt.Println("Exec err: ", err)
}
@ -66,10 +68,8 @@ func LoginFormHandler(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "c_user")
flashes := GetFlashes(session)
session.Save(r, w)
err := templates["login"].Execute(w, map[string]interface{}{"flashes": flashes})
if err != nil {
fmt.Println("Exec err: ", err)
}
ShowTemplate("login", w, map[string]interface{}{"flashes": flashes})
}
// handler for login POST
@ -101,7 +101,22 @@ func LoginPostHandler(w http.ResponseWriter, r *http.Request) {
// ?url=
func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
ShowTemplate("add", w, r, user)
var url = r.URL.Query().Get("url")
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error looking up link", url, ":", err)
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading link", url, ":", err)
} else {
re := regexp.MustCompile("< *[Tt][Ii][Tt][Ll][Ee] *>(.*)< *[Tt][Ii][Tt][Ll][Ee] *>")
title := re.FindStringSubmatch(string(body))
fmt.Println(title)
}
}
ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url})
}
func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {

View File

@ -3,7 +3,7 @@
{{template "flashes" .}}
<form class="form-add" action="/add" method="post" role="form" class="container col-form">
<div class="row">
<div class="col-xs-2">Link:</div><div class="col-xs-10"><input type="text" class="form-control" name="link" placeholder="Link"/></div>
<div class="col-xs-2">Link:</div><div class="col-xs-10"><input type="text" class="form-control" name="link" placeholder="Link" value="{{.link}}"/></div>
<div class="col-xs-2">Title:</div><div class="col-xs-10"><input type="text" class="form-control" name="title" placeholder="Title"/></div>
<div class="col-xs-2">Path:</div><div class="col-xs-10"><input type="text" class="form-control" name="path" placeholder="Path"/></div>
<div class="col-xs-2">Description:</div><div class="col-xs-10"><textarea class="form-control" name="description" placeholder="Description" rows="3" cols="80">{{.task.Description}}</textarea></div>
@ -11,4 +11,4 @@
</div>
</form>
{{end}}
{{end}}