-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.go
86 lines (73 loc) · 1.85 KB
/
site.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
MI "github.com/fern4lvarez/go-metainspector/metainspector"
"html/template"
"net/http"
"net/url"
"os"
)
const lenMI = len("/mi/")
var templates = template.Must(template.ParseFiles("tmpl/index.html"))
type MetaInspector struct {
Title string
}
func getPort() string {
if p := os.Getenv("PORT"); p != "" {
return p
}
return "8080"
}
func getUrl(w http.ResponseWriter, r *http.Request) string {
return r.URL.Path[lenMI:]
}
func renderEmptyTemplate(w http.ResponseWriter, tmpl string) {
err := templates.ExecuteTemplate(w, tmpl+".html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func renderTemplate(w http.ResponseWriter, tmpl string, mi *MI.MetaInspector) {
err := templates.ExecuteTemplate(w, tmpl+".html", mi)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
renderEmptyTemplate(w, "index")
}
func inspectHandler(w http.ResponseWriter, r *http.Request) {
var ust string
u, err := url.Parse(r.FormValue("url"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if u.Scheme != "" {
u.Scheme = ""
ust = u.String()[2:]
} else {
ust = u.String()
}
http.Redirect(w, r, "/mi/"+ust, http.StatusFound)
}
func miHandler(w http.ResponseWriter, r *http.Request) {
uri := getUrl(w, r)
fmt.Printf("Inspecting %s...", uri)
mi, err := MI.New(uri)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println(" Done: ", "http://"+r.Host+r.RequestURI)
renderTemplate(w, "index", mi)
}
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/inspect", inspectHandler)
http.HandleFunc("/mi/", miHandler)
port := getPort()
fmt.Println("Listening to port", port)
http.ListenAndServe(":"+port, nil)
}