diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..ab65c02 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,9 @@ +kind: pipeline +type: docker +name: default + +steps: + - name: test + image: golang + commands: + - go test \ No newline at end of file diff --git a/default.go b/default.go index 87d65db..78fd53f 100644 --- a/default.go +++ b/default.go @@ -1,4 +1,4 @@ -package main +package linkinfo import ( "errors" @@ -29,7 +29,7 @@ func defaultLinkHandler(link string) (*LinkInfo, error) { var res *http.Response for i := 0; i < 10; i++ { - res, err = client.Head(link) + res, err = Client.Head(link) if err != nil { return nil, err @@ -64,7 +64,7 @@ func defaultLinkHandler(link string) (*LinkInfo, error) { } ret := &LinkInfo{ - ContentType: contentType, + ContentType: contentType, ContentLength: contentLength, } @@ -87,7 +87,7 @@ func detectContentType(link, defaultType string) string { req.Header.Set("Range", "bytes=0-512") - res, err := client.Do(req) + res, err := Client.Do(req) if err != nil { return defaultType @@ -115,7 +115,7 @@ var ( ) func retrieveHtmlLinkTitle(i *LinkInfo, link string) error { - res, err := client.Get(link) + res, err := Client.Get(link) if err != nil { return err @@ -170,4 +170,4 @@ func retrieveHtmlLinkTitle(i *LinkInfo, link string) error { } return nil -} \ No newline at end of file +} diff --git a/default_test.go b/default_test.go index db2b550..5e09775 100644 --- a/default_test.go +++ b/default_test.go @@ -1,20 +1,31 @@ -package main +package linkinfo import ( - "net/http" + "strings" "testing" - "time" ) -func Test_DefaultLinkHandler(t *testing.T) { - client = &http.Client{ - Timeout: 10 * time.Second, +func Test_detectContentType(t *testing.T) { + contentType := detectContentType("http://example.com", "") + + if contentType == "" { + t.Fatal("Unable to test example.com, returned empty type") } + if idx := strings.Index(contentType, ";"); idx != -1 { + contentType = contentType[:idx] + } + + if contentType != ContentTypeHtml { + t.Fatal("Unexpected content type:", contentType) + } +} + +func Test_defaultLinkHandler(t *testing.T) { type expectedData struct { - Link string + Link string Title string - Type string + Type string } testLinks := []expectedData{ @@ -37,4 +48,4 @@ func Test_DefaultLinkHandler(t *testing.T) { t.Fatal("Unexpected content type, expected:", link.Type, "got:", l.ContentType) } } -} \ No newline at end of file +} diff --git a/linkinfo.go b/linkinfo.go new file mode 100644 index 0000000..ef8f007 --- /dev/null +++ b/linkinfo.go @@ -0,0 +1,46 @@ +package linkinfo + +import ( + "net/http" + "net/url" +) + +type linkHandler struct { + hosts []string + handler func(link string) (*LinkInfo, error) +} + +type LinkInfo struct { + Title string `json:"title"` + Description string `json:"description"` + ContentType string `json:"type"` + ContentLength int64 `json:"contentLength"` + Duration string `json:"duration,omitempty"` + Redirects []string `json:"redirects,omitempty"` +} + +var ( + Client = http.DefaultClient + + linkHandlers = []*linkHandler{ + {hosts: []string{"youtube.com", "youtu.be"}, handler: youtubeLinkHandler}, + } +) + +func Retrieve(link string) (*LinkInfo, error) { + u, err := url.Parse(link) + + if err != nil { + return nil, err + } + + for _, handler := range linkHandlers { + for _, host := range handler.hosts { + if host == u.Hostname() { + return handler.handler(link) + } + } + } + + return defaultLinkHandler(link) +} diff --git a/main.go b/main.go deleted file mode 100644 index 2137919..0000000 --- a/main.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "net/http" - "time" -) - -var ( - client *http.Client -) - -type LinkInfo struct { - Title string `json:"title"` - Description string `json:"description"` - ContentType string `json:"type"` - ContentLength int64 `json:"contentLength"` - Duration string `json:"duration,omitempty"` - Redirects []string `json:"redirects,omitempty"` -} - -func main() { - client = &http.Client{ - Timeout: 10 * time.Second, - } - - mux := http.NewServeMux() - mux.HandleFunc("/info", handleInfoRequest) - http.ListenAndServe(":8080", mux) -} - -func handleInfoRequest(w http.ResponseWriter, r *http.Request) { - -} \ No newline at end of file diff --git a/service/main.go b/service/main.go new file mode 100644 index 0000000..4de38a8 --- /dev/null +++ b/service/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "net/http" +) + +func main() { + mux := http.NewServeMux() + mux.HandleFunc("/info", handleInfoRequest) + http.ListenAndServe(":8080", mux) +} + +func handleInfoRequest(w http.ResponseWriter, r *http.Request) { + +} diff --git a/util.go b/util.go index a8b8324..3aa8a9b 100644 --- a/util.go +++ b/util.go @@ -1,4 +1,4 @@ -package main +package linkinfo import "fmt" diff --git a/youtube.go b/youtube.go new file mode 100644 index 0000000..2dbe336 --- /dev/null +++ b/youtube.go @@ -0,0 +1,5 @@ +package linkinfo + +func youtubeLinkHandler(link string) (*LinkInfo, error) { + return nil, nil +}