-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
78 lines (69 loc) · 1.77 KB
/
router.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-chi/chi"
)
/*pagina resultados extensos*/
func paginate(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
func header(w http.ResponseWriter){
//w.Header().Add("Date ","Fri, 02 Aug 2019 18:36:12 GMT")
w.Header().Add("Server ","Application/go (Ubuntu)")
w.Header().Add("Vary ","Accept-Encoding")
w.Header().Add("Content-Encoding ","gzip")
//w.Header().Add("Content-Length","464")
w.Header().Add("Keep-Alive", "timeout=5, max=99")
w.Header().Add("Connection", "Keep-Alive")
//w.Header().Add("Content-Type "text/html;charset=UTF-8"
w.Header().Add("Content-Type", "application/json; charset=utf-8")
}
func home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("API Truora-Whois"))
}
/*Lista los dominios previamente consultados*/
func List(w http.ResponseWriter, r *http.Request) {
list, _ := GetAll()
type items struct {
Items []string
}
out_json, err := json.Marshal(items{Items: list})
if err != nil {
fmt.Println(err)
}
header(w)
w.Write([]byte(fmt.Sprintf("%v", string(out_json))))
//fmt.Fprint(w,"API List\n")
}
/*Analice un dominio*/
func Analyce(w http.ResponseWriter, r *http.Request) {
url := chi.URLParam(r, "url")
out := ApiAnalyce(url)
old, err0 := Get(url)
if !err0 {
out = Compare(out, old)
}
tmp, err := json.Marshal(&out)
if err != nil {
return
}
data := string(tmp)
Insert(url, data)
header(w)
fmt.Fprint(w, data)
//fmt.Fprint(w,"API Analice\n")
//fmt.Fprint(w, "Url ", url)
}
func Rutes() {
r := chi.NewRouter()
r.Route("/api", func(r chi.Router) {
r.Get("/", home)
r.With(paginate).Get("/list", List)
r.Get("/analyce/{url}", Analyce)
})
http.ListenAndServe(":8090", r)
}