-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
118 lines (99 loc) · 2.92 KB
/
service.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
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/json"
"net/http"
"strings"
"time"
"github.com/go-martini/martini"
)
// Autocomplete endpoint; suggests hosts based on prefix
func SuggestHosts(params martini.Params, resp http.ResponseWriter) {
bytes, err := json.Marshal(FindHostsByPrefix(params["prefix"]))
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
} else {
resp.Header().Set("Content-Type", "application/json")
resp.Write(bytes)
}
}
// Save a user's incoming report in the database as a full report
func FileReport(report InboundReport, req *http.Request, resp http.ResponseWriter) {
lchost := strings.ToLower(report.ParsedURL.Host)
uri := report.ParsedURL.RequestURI()
host := db.getOrCreateHost(lchost)
if _, exists := host.URIs[uri]; !exists {
host.URIs[uri] = make(map[string]Report)
}
page := host.URIs[uri]
if _, exists := page[report.OriginalText]; !exists {
page[report.OriginalText] = Report{
ID: UuidStr(),
Original: report.OriginalText,
Status: RPT_NEW,
}
}
rpt := page[report.OriginalText]
rpt.Suggestions = append(rpt.Suggestions, Suggestion{
Suggestion: report.SuggestedText,
Note: report.Note,
Submitter: req.RemoteAddr,
Date: time.Now().UTC(),
})
host.Reported++
page[report.OriginalText] = rpt
host.URIs[uri] = page
db.Hosts[lchost] = host
index.Put(lchost)
db.Counts.Total++
db.Counts.New++
Save()
resp.WriteHeader(http.StatusOK)
}
// Get all reports for a certain host
func GetReports(params martini.Params, resp http.ResponseWriter) {
host, exists := GetHost(strings.ToLower(params["host"]))
if !exists {
resp.WriteHeader(http.StatusNotFound)
return
}
bytes, err := json.Marshal(host.URIs)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
} else {
resp.Header().Set("Content-Type", "application/json")
resp.Write(bytes)
}
}
func Acknowledge(action ActionMeta, params martini.Params, resp http.ResponseWriter) {
if SetFlag(action.Host, action.Page, params["reportID"], RPT_ACK) {
resp.WriteHeader(http.StatusOK)
} else {
resp.WriteHeader(http.StatusNotFound)
}
}
func Fixed(action ActionMeta, params martini.Params, resp http.ResponseWriter) {
if SetFlag(action.Host, action.Page, params["reportID"], RPT_FIX) {
resp.WriteHeader(http.StatusOK)
} else {
resp.WriteHeader(http.StatusNotFound)
}
}
func Reject(action ActionMeta, params martini.Params, resp http.ResponseWriter) {
if SetFlag(action.Host, action.Page, params["reportID"], RPT_REJ) {
resp.WriteHeader(http.StatusOK)
} else {
resp.WriteHeader(http.StatusNotFound)
}
}
func Delete(action ActionMeta, params martini.Params, resp http.ResponseWriter) {
if SetFlag(action.Host, action.Page, params["reportID"], RPT_DEL) {
resp.WriteHeader(http.StatusOK)
} else {
resp.WriteHeader(http.StatusNotFound)
}
}
// Used to assist with finding a particular report in the database
type ActionMeta struct {
Host string `form:"host"`
Page string `form:"uri"`
}