8 changed files with 102 additions and 49 deletions
@ -0,0 +1,9 @@
|
||||
kind: pipeline |
||||
type: docker |
||||
name: default |
||||
|
||||
steps: |
||||
- name: test |
||||
image: golang |
||||
commands: |
||||
- go test |
@ -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) |
||||
} |
@ -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) { |
||||
|
||||
} |
Loading…
Reference in new issue