chi

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2024 License: MIT Imports: 2 Imported by: 0

README

chi router extension

Install

go get -u github.com/slipros/roamer/pkg/chi@latest

Example

package main

import (
	"encoding/json"
	"net/http"

	"github.com/slipros/roamer"
	"github.com/slipros/roamer/parser"
	rchi "github.com/slipros/roamer/pkg/chi"
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
)

type Body struct {
	UserID string `path:"user_id"`
}

func main() {
	router := chi.NewRouter()

	r := roamer.NewRoamer(
		roamer.WithParsers(
			parser.NewPath(rchi.NewPath(router)),
		),
	)

	router.Use(middleware.Logger, roamer.Middleware[Body](r))
	router.Post("/user/{user_id}", func(w http.ResponseWriter, r *http.Request) {
		var body Body
		if err := roamer.ParsedDataFromContext(r.Context(), &body); err != nil {
			w.Write([]byte(err.Error()))
			return
		}

		if err := json.NewEncoder(w).Encode(&body); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	})
	http.ListenAndServe(":3000", router)
}

Documentation

Overview

Package chi chi router extensions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPath

func NewPath(mux *chi.Mux) func(r *http.Request, name string) (string, bool)

NewPath returns new path parser for chi router.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL