Split into service and main, refactor some code and add better tests
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Tyler 2019-10-03 19:59:20 -04:00
parent 03e58957f1
commit 75989c9337
8 changed files with 102 additions and 49 deletions

9
.drone.yml Normal file
View File

@ -0,0 +1,9 @@
kind: pipeline
type: docker
name: default
steps:
- name: test
image: golang
commands:
- go test

View File

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

View File

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

46
linkinfo.go Normal file
View File

@ -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
View File

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

15
service/main.go Normal file
View File

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

View File

@ -1,4 +1,4 @@
package main
package linkinfo
import "fmt"

5
youtube.go Normal file
View File

@ -0,0 +1,5 @@
package linkinfo
func youtubeLinkHandler(link string) (*LinkInfo, error) {
return nil, nil
}