Split into service and main, refactor some code and add better tests
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
03e58957f1
commit
75989c9337
|
@ -0,0 +1,9 @@
|
|||
kind: pipeline
|
||||
type: docker
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang
|
||||
commands:
|
||||
- go test
|
|
@ -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
|
||||
|
@ -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
|
||||
|
|
|
@ -1,16 +1,27 @@
|
|||
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
|
||||
Title string
|
||||
|
|
|
@ -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)
|
||||
}
|
33
main.go
33
main.go
|
@ -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) {
|
||||
|
||||
}
|
|
@ -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) {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package linkinfo
|
||||
|
||||
func youtubeLinkHandler(link string) (*LinkInfo, error) {
|
||||
return nil, nil
|
||||
}
|
Loading…
Reference in New Issue