Browse Source

Split into service and main, refactor some code and add better tests

master
Tyler 3 years ago
parent
commit
75989c9337
  1. 9
      .drone.yml
  2. 12
      default.go
  3. 29
      default_test.go
  4. 46
      linkinfo.go
  5. 33
      main.go
  6. 15
      service/main.go
  7. 2
      util.go
  8. 5
      youtube.go

9
.drone.yml

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

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

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

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

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

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

2
util.go

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

5
youtube.go

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