Set Timeout Fix #12

master
kreativmonkey 7 years ago
parent 7de59d325e
commit 1a1ae9ca74

@ -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)

@ -8,7 +8,7 @@ import (
"fmt"
)
const version = "0.05"
const version = "0.06"
var (
short *shrty.Store

@ -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
}

@ -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,

@ -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
}
}

@ -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)
}

Loading…
Cancel
Save