diff --git a/config/local.json b/config/local.json index 9458efe..e592340 100644 --- a/config/local.json +++ b/config/local.json @@ -5,5 +5,6 @@ "Username": "transmet", "Password": "asdfasdf" }, - "Port": "8001" + "Port": "8001", + "Url": "http://localhost:8001/" } diff --git a/main.go b/main.go index 029a26c..7a11f9e 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ type Config struct { Password string } Port string + Url string } const ( diff --git a/route_handlers.go b/route_handlers.go index 16848c4..2a51c1d 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -114,6 +114,25 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request, user *user.User) { http.Redirect(w, r, "/", http.StatusFound) } +func getUrlTitle(url string) string { + 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] *>(.*)") + title := re.FindStringSubmatch(string(body)) + if title != nil { + return strings.TrimSpace(title[1]) + } + } + } + return "" +} + // ?url= func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { categories.LoadCategories(db) @@ -136,28 +155,17 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { } var url = r.URL.Query().Get("url") - reHttp := regexp.MustCompile("^http://") + reHttp := regexp.MustCompile("^https?://") if url != "" && ! reHttp.Match([]byte(url)) { url = "http://" + 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] *>(.*)") - title := re.FindStringSubmatch(string(body)) - if title != nil { - ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": strings.TrimSpace(title[1])}) - return - } - } + + title = r.URL.Query().Get("title") + if title == "" && url != "" { + title = getUrlTitle(url) } - ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "categories": categories.CategoriesTree, "flashes": flashes}) + ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title}) } func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { @@ -208,7 +216,7 @@ func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User fmt.Println("Exec err: ", err) } - ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf}) + ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf, "url": config.Url}) } func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { diff --git a/templates/layout.html b/templates/layout.html index 7d8087e..a4a5488 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -30,6 +30,7 @@