...

Source file src/github.com/go-chi/chi/_examples/todos-resource/todos.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 todosResource struct{}
    10  
    11  // Routes creates a REST router for the todos resource
    12  func (rs todosResource) 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  		r.Get("/sync", rs.Sync)
    26  	})
    27  
    28  	return r
    29  }
    30  
    31  func (rs todosResource) List(w http.ResponseWriter, r *http.Request) {
    32  	w.Write([]byte("todos list of stuff.."))
    33  }
    34  
    35  func (rs todosResource) Create(w http.ResponseWriter, r *http.Request) {
    36  	w.Write([]byte("todos create"))
    37  }
    38  
    39  func (rs todosResource) Get(w http.ResponseWriter, r *http.Request) {
    40  	w.Write([]byte("todo get"))
    41  }
    42  
    43  func (rs todosResource) Update(w http.ResponseWriter, r *http.Request) {
    44  	w.Write([]byte("todo update"))
    45  }
    46  
    47  func (rs todosResource) Delete(w http.ResponseWriter, r *http.Request) {
    48  	w.Write([]byte("todo delete"))
    49  }
    50  
    51  func (rs todosResource) Sync(w http.ResponseWriter, r *http.Request) {
    52  	w.Write([]byte("todo sync"))
    53  }
    54  

View as plain text