-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
108 lines (96 loc) · 2.07 KB
/
utils.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package tore
import (
"html/template"
"log"
"net/http"
"net/url"
"runtime/debug"
)
var tpl string = `
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<style type="text/css">
.container {padding-top: 30px;}
</style>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h2>输入URL, 提取正文</h2>
<form method="post" action="/">
<div class="input-group">
<input class="form-control" name="q" autofocus="autofocus" value="" placeholder="url" type="text">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go!</button>
</span>
</div>
</form>
</div>
<div class="content">
<h3>{{ .Title }}</h3>
{{ .Text }}
</div>
</div>
</body>
</html>`
type Page struct {
Title string
Text template.HTML
}
func getFormUrl(u string) (string, error) {
r, err := url.Parse(u)
if err != nil {
return "", err
}
if r.Scheme == "" {
r.Scheme = "http"
}
return r.String(), nil
}
// handler http request
func NewHttpHandler() http.Handler {
h := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rev := recover(); rev != nil {
w.WriteHeader(500)
log.Println(rev)
debug.PrintStack()
}
}()
if r.URL.Path != "/" {
w.WriteHeader(403)
return
}
var err error
tmpl, _ := template.New("index").Parse(tpl)
if r.Method == "GET" {
err = tmpl.Execute(w, nil)
if err != nil {
panic(err)
}
} else if r.Method == "POST" {
u := r.FormValue("q")
log.Println(u)
u, err = getFormUrl(u)
if err != nil {
err = tmpl.Execute(w, "Invalid URL")
if err != nil {
panic(err)
}
return
}
title, text := GetText(u)
data := &Page{title, template.HTML(text)}
err = tmpl.Execute(w, data)
if err != nil {
panic(err)
}
} else {
w.WriteHeader(403)
}
}
return http.HandlerFunc(h)
}