api.go
1 package server 2 3 import ( 4 "fmt" 5 ) 6 7 const ( 8 // GET is HTTP GET request type 9 GET string = "GET" 10 // POST is HTTP POST request type 11 POST string = "POST" 12 // PUT is HTTP PUT request type 13 PUT string = "PUT" 14 // PATCH is HTTP PATCH request type 15 PATCH string = "PATCH" 16 // HEAD is HTTP HEAD request type 17 HEAD string = "HEAD" 18 ) 19 20 type endpointHandler func(*webContext) 21 22 // EndpointArg represents an API endpoint argument. 23 type EndpointArg struct { 24 Name string 25 Type string 26 Required bool 27 Description string 28 } 29 30 // Endpoint represents an API endpoint definition. 31 type Endpoint struct { 32 Name string 33 Path string 34 Method string 35 CSRFRequired bool 36 Handler endpointHandler `json:"-"` 37 Description string 38 Args []*EndpointArg 39 } 40 41 func (e *Endpoint) Pattern() string { 42 return fmt.Sprintf("%s %s", e.Method, e.Path) 43 } 44 45 // Endpoints contains all registered API endpoints. 46 var Endpoints []*Endpoint 47 48 func init() { 49 // TODO add Args 50 Endpoints = []*Endpoint{ 51 &Endpoint{ 52 Name: "Config", 53 Path: "/api/config", 54 Method: GET, 55 CSRFRequired: true, 56 Handler: serveConfig, 57 Description: "Serve config", 58 }, 59 &Endpoint{ 60 Name: "Search", 61 Path: "/search", 62 Method: GET, 63 CSRFRequired: false, 64 Handler: serveSearch, 65 Description: "Search websocket endpoint", 66 }, 67 // tmp added for backward compatibility 68 &Endpoint{ 69 Name: "Add", 70 Path: "/api/add", 71 Method: GET, 72 CSRFRequired: true, 73 Handler: serveAdd, 74 Description: "Add document form", 75 }, 76 &Endpoint{ 77 Name: "Add post", 78 Path: "/api/add", 79 Method: POST, 80 CSRFRequired: true, 81 Handler: serveAdd, 82 Description: "Save added document", 83 }, 84 // alias for /api/add - backward compatibility - use /api/add in the future 85 &Endpoint{ 86 Name: "Add post", 87 Path: "/add", 88 Method: POST, 89 CSRFRequired: true, 90 Handler: serveAdd, 91 Description: "Save added document", 92 }, 93 &Endpoint{ 94 Name: "Get document", 95 Path: "/api/document", 96 Method: GET, 97 CSRFRequired: false, 98 Handler: serveGet, 99 Description: "Get document by URL", 100 Args: []*EndpointArg{ 101 &EndpointArg{ 102 Name: "url", 103 Type: "string", 104 Required: true, 105 Description: "URL of the document", 106 }, 107 }, 108 }, 109 &Endpoint{ 110 Name: "Rules", 111 Path: "/api/rules", 112 Method: GET, 113 CSRFRequired: true, 114 Handler: serveRules, 115 Description: "Rules page", 116 }, 117 &Endpoint{ 118 Name: "Save rules", 119 Path: "/api/rules", 120 Method: POST, 121 CSRFRequired: true, 122 Handler: serveRules, 123 Description: "Save rules", 124 }, 125 &Endpoint{ 126 Name: "History", 127 Path: "/api/history", 128 Method: GET, 129 CSRFRequired: true, 130 Handler: serveHistory, 131 Description: "History page", 132 }, 133 &Endpoint{ 134 Name: "Add history item", 135 Path: "/api/history", 136 Method: POST, 137 CSRFRequired: true, 138 Handler: serveHistory, 139 Description: "Add new history item", 140 }, 141 &Endpoint{ 142 Name: "Delete", 143 Path: "/api/delete", 144 Method: POST, 145 CSRFRequired: true, 146 Handler: serveDeleteDocument, 147 Description: "Delete document endpoint", 148 }, 149 &Endpoint{ 150 Name: "Delete alias", 151 Path: "/api/delete_alias", 152 Method: POST, 153 CSRFRequired: true, 154 Handler: serveDeleteAlias, 155 Description: "Delete alias", 156 }, 157 &Endpoint{ 158 Name: "Add alias", 159 Path: "/api/add_alias", 160 Method: POST, 161 CSRFRequired: true, 162 Handler: serveAddAlias, 163 Description: "Add alias", 164 }, 165 &Endpoint{ 166 Name: "Readable", 167 Path: "/api/readable", 168 Method: GET, 169 CSRFRequired: false, 170 Handler: serveReadable, 171 Description: "Readabilty view", 172 }, 173 &Endpoint{ 174 Name: "Stats", 175 Path: "/api/stats", 176 Method: GET, 177 CSRFRequired: false, 178 Handler: serveStats, 179 Description: "Search engine statistics", 180 }, 181 &Endpoint{ 182 Name: "API", 183 Path: "/api", 184 Method: GET, 185 CSRFRequired: false, 186 Handler: serveAPI, 187 Description: "API documentation", 188 }, 189 } 190 }