File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ In fact the internal code of **go-json-rest** is itself implemented with Middlew
3535 - [Hello World!](#hello-world)
3636 - [Countries](#countries)
3737 - [Users](#users)
38+ - [Lookup](#lookup)
3839 - [ Applications] ( #applications )
3940 - [API and static files](#api-and-static-files)
4041 - [GORM](#gorm)
@@ -374,6 +375,52 @@ func (u *Users) DeleteUser(w rest.ResponseWriter, r *rest.Request) {
374375
375376```
376377
378+ #### Lookup
379+
380+ Demonstrate how to use the relaxed placeholder (notation #paramName).
381+ This placeholder matches everything until the first ` / ` , including ` . `
382+
383+ The curl demo:
384+ ```
385+ curl -i http://127.0.0.1:8080/lookup/google.com
386+ curl -i http://127.0.0.1:8080/lookup/notadomain
387+ ```
388+
389+ Go code:
390+ ``` go
391+ package main
392+
393+ import (
394+ " github.com/ant0ine/go-json-rest/rest"
395+ " log"
396+ " net"
397+ " net/http"
398+ )
399+
400+ type Message struct {
401+ Body string
402+ }
403+
404+ func main () {
405+ handler := rest.ResourceHandler {}
406+ err := handler.SetRoutes (
407+ &rest.Route {" GET" , " /lookup/#host" , func (w rest.ResponseWriter , req *rest.Request ) {
408+ ip , err := net.LookupIP (req.PathParam (" host" ))
409+ if err != nil {
410+ rest.Error (w, err.Error (), http.StatusInternalServerError )
411+ return
412+ }
413+ w.WriteJson (&ip)
414+ }},
415+ )
416+ if err != nil {
417+ log.Fatal (err)
418+ }
419+ log.Fatal (http.ListenAndServe (" :8080" , &handler))
420+ }
421+
422+ ```
423+
377424
378425### Applications
379426
You can’t perform that action at this time.
0 commit comments