...

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

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

     1  //
     2  // Todos Resource
     3  // ==============
     4  // This example demonstrates a project structure that defines a subrouter and its
     5  // handlers on a struct, and mounting them as subrouters to a parent router.
     6  // See also _examples/rest for an in-depth example of a REST service, and apply
     7  // those same patterns to this structure.
     8  //
     9  package main
    10  
    11  import (
    12  	"net/http"
    13  
    14  	"github.com/go-chi/chi"
    15  	"github.com/go-chi/chi/middleware"
    16  )
    17  
    18  func main() {
    19  	r := chi.NewRouter()
    20  
    21  	r.Use(middleware.RequestID)
    22  	r.Use(middleware.RealIP)
    23  	r.Use(middleware.Logger)
    24  	r.Use(middleware.Recoverer)
    25  
    26  	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
    27  		w.Write([]byte("."))
    28  	})
    29  
    30  	r.Mount("/users", usersResource{}.Routes())
    31  	r.Mount("/todos", todosResource{}.Routes())
    32  
    33  	http.ListenAndServe(":3333", r)
    34  }
    35  

View as plain text