From 1a1ae9ca746d48d0b985149318dd1d5e814ac653 Mon Sep 17 00:00:00 2001 From: kreativmonkey Date: Mon, 3 Jul 2017 19:16:35 +0200 Subject: [PATCH] Set Timeout Fix #12 --- config.go | 3 +++ main.go | 2 +- shrty/short.go | 10 +++++----- shrty/short_test.go | 8 ++++---- shrty/urlinfo.go | 18 +++++++++++++----- shrty/urlinfo_test.go | 26 +++++++++++++++++++++++++- 6 files changed, 51 insertions(+), 16 deletions(-) diff --git a/config.go b/config.go index 956325e..5b30cde 100644 --- a/config.go +++ b/config.go @@ -13,6 +13,7 @@ func DefaultConfig() Config { Host: "localhost", Port: "6889", Title: "Sgot", + CheckDomain: false, } } @@ -23,6 +24,7 @@ type Config struct { Port string `env:"SHRT_PORT"` Domain string `env:"SHRT_DOMAIN"` Title string `env:"SHRT_TITLE"` + CheckDomain bool `env:"SHRT_CHECK_DOMAIN"` } func (c Config) HostPort() string { @@ -53,6 +55,7 @@ func readConfig(f *flag.FlagSet, args []string) (*Config, error){ f.StringVar(&config.DB, "db-file", config.DB, "The db file to use") f.StringVar(&config.Domain, "domain", config.Domain, "The domain for redirect links") f.StringVar(&config.Title, "title", config.Title, "The title on the Front") + f.BoolVar(&config.CheckDomain, "check-domain", config.CheckDomain, "Do you want to check Domain? Default:") // Arguments variables err = f.Parse(args) diff --git a/main.go b/main.go index 8cf5439..265a1fc 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "fmt" ) -const version = "0.05" +const version = "0.06" var ( short *shrty.Store diff --git a/shrty/short.go b/shrty/short.go index 233926e..de9df7b 100644 --- a/shrty/short.go +++ b/shrty/short.go @@ -17,7 +17,7 @@ type Data struct { Token string `json:"token"` Meta `json:"meta"` FavIconLink string `json:"favicon_url"` - HTTPStatusCode string `json:"http_code"` + HTTPStatusCode int `json:"http_code"` Category string `json:"category"` Created string `json:"created_at"` Clicks int64 `json:"clicks"` @@ -41,7 +41,7 @@ func (s *Store) Short(URL string, value *string) error { } // Create a sha256 Hash from the URL - d.Hash = fmt.Sprintf("%x", sha256.Sum256([]byte(d.OriginalURL))) + d.Hash = fmt.Sprintf("%x", sha256.Sum256([]byte(d.URLFetched))) // Check if the URL already in the Storage if ok := s.Exist(&d); ok { @@ -79,10 +79,10 @@ func (s *Store) Exist(d *Data) bool { // Returns the URL for the given token func (s *Store) Redirect(token string) (string, bool) { - if shrt, ok := s.Token[token]; ok { - s.Token[token].Clicks += 1 + if data, ok := s.Token[token]; ok { + data.Clicks += 1 s.Save() - return shrt.OriginalURL, true + return data.URLFetched, true } return "", false } diff --git a/shrty/short_test.go b/shrty/short_test.go index 315afcc..4f9be3e 100644 --- a/shrty/short_test.go +++ b/shrty/short_test.go @@ -34,7 +34,7 @@ func TestStorage_Short(t *testing.T) { Description:"", Type:""}, FavIconLink:"", - HTTPStatusCode:"200", + HTTPStatusCode: 200, Category:"", Created:s.Token[token].Created, Clicks:0, @@ -81,7 +81,7 @@ func TestStorage_Get(t *testing.T) { Description:"", Type:""}, FavIconLink:"", - HTTPStatusCode:"200", + HTTPStatusCode: 200, Category:"", Created:s.Token[token].Created, Clicks:0, @@ -110,7 +110,7 @@ func TestStore_Exist(t *testing.T) { Description:"", Type:""}, FavIconLink:"", - HTTPStatusCode:"200", + HTTPStatusCode: 200, Category:"", Created:time.Now().String(), Clicks:0, @@ -157,7 +157,7 @@ func TestStore_Redirect(t *testing.T) { Description:"", Type:""}, FavIconLink:"", - HTTPStatusCode:"200", + HTTPStatusCode: 200, Category:"", Created:time.Now().String(), Clicks:0, diff --git a/shrty/urlinfo.go b/shrty/urlinfo.go index 2509088..3714643 100644 --- a/shrty/urlinfo.go +++ b/shrty/urlinfo.go @@ -3,21 +3,29 @@ package shrty import ( "fmt" "net/http" + "time" ) func CheckURL(d *Data) error { - resp, err := http.Get(d.URL) + // Set a timeout for the Request + var netClient = &http.Client{ + Timeout: time.Second * 10, + } + + resp, err := netClient.Get(d.URL) if err != nil { - fmt.Println(err) + d.URLFetched = d.URL + d.HTTPStatusCode = http.StatusGatewayTimeout + return nil } defer resp.Body.Close() - - d.HTTPStatusCode = fmt.Sprint(resp.StatusCode) + // Set the Informations from the Request + d.HTTPStatusCode = resp.StatusCode d.URLFetched = fmt.Sprint(resp.Request.URL) d.OriginalURL = fmt.Sprint(resp.Request.URL) d.Domain = resp.Request.URL.Host return err -} +} \ No newline at end of file diff --git a/shrty/urlinfo_test.go b/shrty/urlinfo_test.go index 59bbd55..3d4263a 100644 --- a/shrty/urlinfo_test.go +++ b/shrty/urlinfo_test.go @@ -3,6 +3,9 @@ package shrty import ( "testing" "github.com/stretchr/testify/assert" + "net/http/httptest" + "net/http" + "time" ) func TestCheckURL(t *testing.T) { @@ -14,7 +17,7 @@ func TestCheckURL(t *testing.T) { URL: "https://t.co/kA45uWnKkg", URLFetched: "https://www.youtube.com/watch?v=Qg0pO9VG1J8&utm_content=buffera4269&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer", OriginalURL: "https://www.youtube.com/watch?v=Qg0pO9VG1J8&utm_content=buffera4269&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer", - HTTPStatusCode: "200", + HTTPStatusCode: 200, Domain: "www.youtube.com", } @@ -22,3 +25,24 @@ func TestCheckURL(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expected, data) } + +func TestCheckURL2(t *testing.T) { + svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(time.Second * 12) + })) + defer svr.Close() + + data := Data{ + URL: svr.URL, + } + + expected := Data{ + URL: svr.URL, + URLFetched: svr.URL, + HTTPStatusCode: http.StatusGatewayTimeout, + } + + err := CheckURL(&data) + assert.NoError(t, err) + assert.Equal(t, expected, data) +}