Documentation
¶
Overview ¶
Copyright 2019 Metaleaf.io
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Example ¶
Examples that describe how to implement route handlers that accept query parameters and path variables.
// Sample search handler.
search := func(writer http.ResponseWriter, request *Request) {
fmt.Printf("Search for: \"%s\"\n", request.Params["s"])
writer.WriteHeader(200)
}
// Sample get resource handler.
getBookByIsbn := func(writer http.ResponseWriter, request *Request) {
fmt.Printf("Get book with ISBN = %s\n", request.Params["isbn"])
writer.WriteHeader(200)
}
// build the router.
router := NewRouter().
AddRoute("GET", "/search", search).
AddRoute("GET", "/book/{isbn}", getBookByIsbn)
// Start the server.
server := httptest.NewServer(router)
defer server.Close()
http.Get(server.URL + "/search?s=stuff+to+search+for")
http.Get(server.URL + "/book/978-0316371247")
Output: Search for: "stuff to search for" Get book with ISBN = 978-0316371247
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Request ¶
type Request struct {
// The original request structure.
*http.Request
// Parameters are a hash of key/value strings. These are extracted from
// the URL path and the query that appears after a question mark "?" in
// the path.
Params map[string]string
}
Enhances the regular http.Request structure by adding the parameters