...

Source file src/github.com/go-chi/chi/_examples/todos-resource/users.go

Documentation: github.com/go-chi/chi/_examples/todos-resource

     1  package main
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/go-chi/chi"
     7  )
     8  
     9  type usersResource struct{}
    10  
    11  // Routes creates a REST router for the todos resource
    12  func (rs usersResource) Routes() chi.Router {
    13  	r := chi.NewRouter()
    14  	// r.Use() // some middleware..
    15  
    16  	r.Get("/", rs.List)    // GET /todos - read a list of todos
    17  	r.Post("/", rs.Create) // POST /todos - create a new todo and persist it
    18  	r.Put("/", rs.Delete)
    19  
    20  	r.Route("/{id}", func(r chi.Router) {
    21  		// r.Use(rs.TodoCtx) // lets have a todos map, and lets actually load/manipulate
    22  		r.Get("/", rs.Get)       // GET /todos/{id} - read a single todo by :id
    23  		r.Put("/", rs.Update)    // PUT /todos/{id} - update a single todo by :id
    24  		r.Delete("/", rs.Delete) // DELETE /todos/{id} - delete a single todo by :id
    25  	})
    26  
    27  	return r
    28  }
    29  
    30  func (rs usersResource) List(w http.ResponseWriter, r *http.Request) {
    31  	w.Write([]byte("aaa list of stuff.."))
    32  }
    33  
    34  func (rs usersResource) Create(w http.ResponseWriter, r *http.Request) {
    35  	w.Write([]byte("aaa create"))
    36  }
    37  
    38  func (rs usersResource) Get(w http.ResponseWriter, r *http.Request) {
    39  	w.Write([]byte("aaa get"))
    40  }
    41  
    42  func (rs usersResource) Update(w http.ResponseWriter, r *http.Request) {
    43  	w.Write([]byte("aaa update"))
    44  }
    45  
    46  func (rs usersResource) Delete(w http.ResponseWriter, r *http.Request) {
    47  	w.Write([]byte("aaa delete"))
    48  }
    49  

View as plain text