Ejemplo n.º 1
0
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	getTodoListHandler := GetTodoList{
		TodoListRepository: h.TodoListRepository,
	}
	getTodoListIDHandler := GetTodoListID{
		TodoListRepository: h.TodoListRepository,
	}
	createTodoHandler := CreateTodo{
		TodoListRepository: h.TodoListRepository,
	}
	updateTodoHandler := UpdateTodo{
		TodoListRepository: h.TodoListRepository,
	}
	deleteTodoHandler := DeleteTodo{
		TodoListRepository: h.TodoListRepository,
	}

	router := mux.NewRouter(nil)

	fs := http.FileServer(http.Dir("public"))
	router.Get("/", fs)

	router.Group("/api", func(router mux.Router) {
		router.Get("/todolist", getTodoListHandler)
		router.Get("/todolist/{id}", getTodoListIDHandler)
		router.Put("/todolist/{tlid}/todo", createTodoHandler)
		router.Patch("/todolist/{tlid}/todo/{tid}", updateTodoHandler)
		router.Delete("/todolist/{tlid}/todo/{tid}", deleteTodoHandler)
	})

	router.ServeHTTP(w, r)
}
Ejemplo n.º 2
0
func main() {
	router := mux.NewRouter(nil)
	router.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "adljasldk")
	}))

	router.Get("/x", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "yyyy")
	}))

	router.Middlewares(HowLong, MiddlewareGenerator("one"), MiddlewareGenerator("two")).Group("/api", func(r mux.Router) {
		r.Get("/", HandlerGenerator("api:get"))
		r.Get("/user", HandlerGenerator("user:get"))
		r.Post("/user", HandlerGenerator("user:post"))
		r.Get("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			id := mux.Vars(r)["id"]

			fmt.Fprintf(w, "Hello, %s!", id)
		}))
	})

	http.ListenAndServe(":8080", router)
}