Repository navigation
Expand file tree
/
Copy pathapi_subscriptions.go
More file actions
158 lines (133 loc) · 4.84 KB
/
Copy pathapi_subscriptions.go
File metadata and controls
158 lines (133 loc) · 4.84 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"net/http"
"strconv"
"strings"
"ublproxy/internal/store"
)
func (a *apiHandler) routeSubscriptions(w http.ResponseWriter, r *http.Request, path string, sess *store.Session) {
switch {
case path == "/subscriptions" && r.Method == http.MethodGet:
a.handleListSubscriptions(w, r, sess)
case path == "/subscriptions" && r.Method == http.MethodPost:
a.handleCreateSubscription(w, r, sess)
case path == "/subscriptions/refresh" && r.Method == http.MethodPost:
a.handleRefreshSubscriptions(w, r, sess)
case strings.HasPrefix(path, "/subscriptions/") && r.Method == http.MethodDelete:
a.handleDeleteSubscription(w, r, path, sess)
case strings.HasPrefix(path, "/subscriptions/") && r.Method == http.MethodPatch:
a.handlePatchSubscription(w, r, path, sess)
default:
http.NotFound(w, r)
}
}
type subscriptionResponse struct {
ID int64 `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
CreatedAt string `json:"createdAt"`
}
func toSubscriptionResponse(s store.Subscription) subscriptionResponse {
return subscriptionResponse{
ID: s.ID,
URL: s.URL,
Name: s.Name,
Enabled: s.Enabled,
CreatedAt: s.CreatedAt.Format("2006-01-02T15:04:05Z"),
}
}
func (a *apiHandler) handleListSubscriptions(w http.ResponseWriter, r *http.Request, sess *store.Session) {
subs, err := a.store.ListSubscriptions(sess.CredentialID)
if err != nil {
writeJSON(w, http.StatusInternalServerError, errorResponse{"failed to list subscriptions"})
return
}
resp := make([]subscriptionResponse, len(subs))
for i, s := range subs {
resp[i] = toSubscriptionResponse(s)
}
writeJSON(w, http.StatusOK, resp)
}
type createSubscriptionRequest struct {
URL string `json:"url"`
Name string `json:"name"`
}
func (a *apiHandler) handleCreateSubscription(w http.ResponseWriter, r *http.Request, sess *store.Session) {
var req createSubscriptionRequest
if err := readJSON(w, r, &req); err != nil {
writeJSON(w, http.StatusBadRequest, errorResponse{"invalid request body"})
return
}
if req.URL == "" {
writeJSON(w, http.StatusBadRequest, errorResponse{"url is required"})
return
}
if !strings.HasPrefix(req.URL, "http://") && !strings.HasPrefix(req.URL, "https://") {
writeJSON(w, http.StatusBadRequest, errorResponse{"url must start with http:// or https://"})
return
}
sub, err := a.store.CreateSubscription(sess.CredentialID, req.URL, req.Name)
if err != nil {
// UNIQUE constraint violation means duplicate
if strings.Contains(err.Error(), "UNIQUE") {
writeJSON(w, http.StatusConflict, errorResponse{"subscription already exists"})
return
}
writeJSON(w, http.StatusInternalServerError, errorResponse{"failed to create subscription"})
return
}
a.triggerReload(sess.CredentialID)
writeJSON(w, http.StatusCreated, toSubscriptionResponse(*sub))
}
func (a *apiHandler) handleDeleteSubscription(w http.ResponseWriter, r *http.Request, path string, sess *store.Session) {
id, err := parseSubscriptionID(path)
if err != nil {
writeJSON(w, http.StatusBadRequest, errorResponse{"invalid subscription ID"})
return
}
if err := a.store.DeleteSubscription(id, sess.CredentialID); err != nil {
writeJSON(w, http.StatusNotFound, errorResponse{"subscription not found"})
return
}
a.triggerReload(sess.CredentialID)
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
type patchSubscriptionRequest struct {
Enabled *bool `json:"enabled"`
}
func (a *apiHandler) handlePatchSubscription(w http.ResponseWriter, r *http.Request, path string, sess *store.Session) {
id, err := parseSubscriptionID(path)
if err != nil {
writeJSON(w, http.StatusBadRequest, errorResponse{"invalid subscription ID"})
return
}
var req patchSubscriptionRequest
if err := readJSON(w, r, &req); err != nil {
writeJSON(w, http.StatusBadRequest, errorResponse{"invalid request body"})
return
}
if req.Enabled == nil {
writeJSON(w, http.StatusBadRequest, errorResponse{"enabled field is required"})
return
}
if err := a.store.SetSubscriptionEnabled(id, sess.CredentialID, *req.Enabled); err != nil {
writeJSON(w, http.StatusNotFound, errorResponse{"subscription not found"})
return
}
a.triggerReload(sess.CredentialID)
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
// handleRefreshSubscriptions forces re-download of all cached blocklists.
func (a *apiHandler) handleRefreshSubscriptions(w http.ResponseWriter, r *http.Request, sess *store.Session) {
if err := a.store.ClearBlocklistCache(); err != nil {
writeJSON(w, http.StatusInternalServerError, errorResponse{"failed to clear cache"})
return
}
a.triggerReload(sess.CredentialID)
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
func parseSubscriptionID(path string) (int64, error) {
idStr := strings.TrimPrefix(path, "/subscriptions/")
return strconv.ParseInt(idStr, 10, 64)
}