adaptor

command
v0.0.0-...-231bdf8 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

Using Adaptor

This example demonstrates how to use get standard http.ResponseWriter and http.Request from app.RequestContext . This is useful if you want to integrate packages build for net/http with Hertz, especially these use Handler to register handler. Be mindful that this compatibility comes at the cost performance loss.

package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/adaptor"
)

func handler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(200)

	_, err := w.Write([]byte("Hello World"))
	if err != nil {
		fmt.Println(err)
		return
	}
}

func main() {
	h := server.Default()

	h.GET("/hello", func(ctx context.Context, c *app.RequestContext) {
		req, err := adaptor.GetCompatRequest(&c.Request)
		if err != nil {
			fmt.Println(err)
			return
		}
		// You may build more logic on req
		fmt.Println(req.URL.String())

		// caution: don't pass in c.GetResponse() as it return a copy of response
		rw := adaptor.GetCompatResponseWriter(&c.Response)

		handler(rw, req)
	})

	h.Spin()
}

How to run

  1. run go run hello/main.go. This will spin up hertz listening on 8080.
  2. run curl --location --request GET '127.0.0.1:8080/hello'

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Code generated by "jade.go"; DO NOT EDIT.
Code generated by "jade.go"; DO NOT EDIT.

Jump to

Keyboard shortcuts

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