Split into service and main, refactor some code and add better tests
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		
							
								
								
									
										9
									
								
								.drone.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								.drone.yml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					kind: pipeline
 | 
				
			||||||
 | 
					type: docker
 | 
				
			||||||
 | 
					name: default
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					steps:
 | 
				
			||||||
 | 
					  - name: test
 | 
				
			||||||
 | 
					    image: golang
 | 
				
			||||||
 | 
					    commands:
 | 
				
			||||||
 | 
					      - go test
 | 
				
			||||||
							
								
								
									
										10
									
								
								default.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								default.go
									
									
									
									
									
								
							@ -1,4 +1,4 @@
 | 
				
			|||||||
package main
 | 
					package linkinfo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
@ -29,7 +29,7 @@ func defaultLinkHandler(link string) (*LinkInfo, error) {
 | 
				
			|||||||
	var res *http.Response
 | 
						var res *http.Response
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for i := 0; i < 10; i++ {
 | 
						for i := 0; i < 10; i++ {
 | 
				
			||||||
		res, err = client.Head(link)
 | 
							res, err = Client.Head(link)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
@ -64,7 +64,7 @@ func defaultLinkHandler(link string) (*LinkInfo, error) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ret := &LinkInfo{
 | 
						ret := &LinkInfo{
 | 
				
			||||||
		ContentType: contentType,
 | 
							ContentType:   contentType,
 | 
				
			||||||
		ContentLength: contentLength,
 | 
							ContentLength: contentLength,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -87,7 +87,7 @@ func detectContentType(link, defaultType string) string {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	req.Header.Set("Range", "bytes=0-512")
 | 
						req.Header.Set("Range", "bytes=0-512")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res, err := client.Do(req)
 | 
						res, err := Client.Do(req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return defaultType
 | 
							return defaultType
 | 
				
			||||||
@ -115,7 +115,7 @@ var (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func retrieveHtmlLinkTitle(i *LinkInfo, link string) error {
 | 
					func retrieveHtmlLinkTitle(i *LinkInfo, link string) error {
 | 
				
			||||||
	res, err := client.Get(link)
 | 
						res, err := Client.Get(link)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
 | 
				
			|||||||
@ -1,20 +1,31 @@
 | 
				
			|||||||
package main
 | 
					package linkinfo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"net/http"
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Test_DefaultLinkHandler(t *testing.T) {
 | 
					func Test_detectContentType(t *testing.T) {
 | 
				
			||||||
	client = &http.Client{
 | 
						contentType := detectContentType("http://example.com", "")
 | 
				
			||||||
		Timeout: 10 * time.Second,
 | 
					
 | 
				
			||||||
 | 
						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 {
 | 
						type expectedData struct {
 | 
				
			||||||
		Link string
 | 
							Link  string
 | 
				
			||||||
		Title string
 | 
							Title string
 | 
				
			||||||
		Type string
 | 
							Type  string
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	testLinks := []expectedData{
 | 
						testLinks := []expectedData{
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										46
									
								
								linkinfo.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								linkinfo.go
									
									
									
									
									
										Normal 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
									
									
									
									
									
								
							
							
						
						
									
										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
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								service/main.go
									
									
									
									
									
										Normal 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) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										5
									
								
								youtube.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								youtube.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					package linkinfo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func youtubeLinkHandler(link string) (*LinkInfo, error) {
 | 
				
			||||||
 | 
						return nil, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user