Leider geht das mit dem interface nicht

master
kreativmonkey 7 years ago
parent aac46dea60
commit 99e57b64c8

@ -37,7 +37,7 @@ func StreamToByte(stream io.Reader) []byte {
return buf.Bytes() return buf.Bytes()
} }
func (s *Storage) Add(URL string) (string, error) { func (s *Storage) Add(URL string, value interface{}) (string, error) {
// if the URL a valid URL? // if the URL a valid URL?
/*if !govalidator.IsURL(URL) { /*if !govalidator.IsURL(URL) {
return "", errors.New("invalid url") return "", errors.New("invalid url")
@ -57,6 +57,7 @@ func (s *Storage) Add(URL string) (string, error) {
token := hash[:hashShortestLen] token := hash[:hashShortestLen]
s.Token[token] = hash s.Token[token] = hash
s.Url[hash] = shrt{URL: URL, Token: token} s.Url[hash] = shrt{URL: URL, Token: token}
value = s.Url[hash]
return token, nil return token, nil
} }
} }
@ -69,9 +70,12 @@ func (s *Storage) Remove(URL string) error {
} }
// Get returns the URL for the given token // Get returns the URL for the given token
func (s *Storage) Get(token string) (string, error) { func (s *Storage) Get(token string, value interface{}) bool {
if val, ok := s.Token[token]; ok { if hash, ok := s.Token[token]; ok {
return s.Url[val].URL, nil fmt.Printf("Url gefunden %s mit Hash: %v \n", s.Url[hash].URL, hash)
value = s.Url[hash].URL
fmt.Printf("Value: %V \n", value)
return true
} }
return "", ErrNotFound return false
} }

@ -9,9 +9,8 @@ import (
) )
type Page struct { type Page struct {
Url string Title string
Token string Body string
Count int
} }
var ( var (
@ -27,21 +26,32 @@ func init() {
} }
func index(w http.ResponseWriter, r *http.Request) { func index(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //get request method fmt.Println("method: ", r.Method) //get request method
fmt.Println("Path: ", r.URL.Path[1:])
switch r.Method { switch r.Method {
case "GET": case "GET":
t, _ := template.ParseFiles("template/index.gohtml") var redirect string
t.Execute(w, nil) if ok := short.Get(r.URL.Path[1:], &redirect); ok{
P := Page{Title: ToString(r.URL.Path[1:]), Body: redirect}
fmt.Printf("Redirect: %s \n", redirect)
t, _ := template.ParseFiles("template/index.gohtml")
t.Execute(w, P)
fmt.Println("Url by Token:", P)
} else {
t, _ := template.ParseFiles("template/index.gohtml")
t.Execute(w, nil)
}
case "POST": case "POST":
r.ParseForm() r.ParseForm()
token, err := short.Add(ToString(r.Form["url"])) var test string
token, err := short.Add(ToString(r.Form["url"]), &test)
if err != nil { if err != nil {
panic(err) panic(err)
} }
P := Page{Url: ToString(r.Form["url"]), Token: token} P := Page{Title: ToString(r.Form["url"]), Body: token}
t, _ := template.ParseFiles("template/index.gohtml") t, _ := template.ParseFiles("template/index.gohtml")
t.Execute(w, P) t.Execute(w, P)
fmt.Println("url:", P) fmt.Println("Token by Url:", P)
} }
} }

Loading…
Cancel
Save