Leider geht das mit dem interface nicht

master
kreativmonkey 8 years ago
parent aac46dea60
commit 99e57b64c8

@ -37,7 +37,7 @@ func StreamToByte(stream io.Reader) []byte {
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 !govalidator.IsURL(URL) {
return "", errors.New("invalid url")
@ -57,6 +57,7 @@ func (s *Storage) Add(URL string) (string, error) {
token := hash[:hashShortestLen]
s.Token[token] = hash
s.Url[hash] = shrt{URL: URL, Token: token}
value = s.Url[hash]
return token, nil
}
}
@ -69,9 +70,12 @@ func (s *Storage) Remove(URL string) error {
}
// Get returns the URL for the given token
func (s *Storage) Get(token string) (string, error) {
if val, ok := s.Token[token]; ok {
return s.Url[val].URL, nil
func (s *Storage) Get(token string, value interface{}) bool {
if hash, ok := s.Token[token]; ok {
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 {
Url string
Token string
Count int
Title string
Body string
}
var (
@ -27,21 +26,32 @@ func init() {
}
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 {
case "GET":
t, _ := template.ParseFiles("template/index.gohtml")
t.Execute(w, nil)
var redirect string
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":
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 {
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.Execute(w, P)
fmt.Println("url:", P)
fmt.Println("Token by Url:", P)
}
}

Loading…
Cancel
Save