hertz-examples

module
v0.0.0-...-45c0762 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: Apache-2.0

README

Hertz Examples

English | 中文

How to run

Enter an example directory for its specific run instructions. Unless an example says otherwise, run commands from the repository root.

Bizdemo

  • bizdemo/hertz_gorm: Use Thrift IDL, hz, Hertz binding and validation, GORM, and MySQL in a Hertz service.
  • bizdemo/hertz_jwt: Build a Hertz service with JWT authentication, GORM, and MySQL.
  • bizdemo/hertz_session: Use Redis-backed distributed sessions and CSRF protection in a Hertz service.
  • bizdemo/hertz_swagger_gen: Generate Swagger documentation and a Swagger UI service from Thrift IDL with thrift-gen-http-swagger.
  • bizdemo/tiktok_demo: A simple Hertz backend for user, video, social, feed, favorite, comment, and message services.
  • formulago: An enterprise administration framework built with Hertz and Ent.
  • gpress: A Web3 content platform built with Hertz and Go templates, with FTS5 full-text search and Hugo compatibility.

Server

  • hello: A basic Hertz “hello world” server.
  • config: Configure a Hertz server.
  • protocol: Use HTTP/1.1 and TLS with Hertz. It also links to external protocol examples.
    • HTTP1: A Hertz HTTP/1.1 server.
    • TLS: A TLS server plus a Hertz client that sends a TLS request.
    • HTTP2: The external Hertz HTTP/2 examples.
    • HTTP3: The external Hertz HTTP/3 example based on QUIC.
    • WebSocket: The external Hertz WebSocket examples.
    • SSE: A Hertz Server-Sent Events server and client using Hertz’s built-in SSE package.
  • middleware: Server middleware examples, including BasicAuth, CORS, CSRF, custom middleware, pprof, RequestID, Gzip, and load balancing.
    • basicauth: Use BasicAuth middleware.
    • CORS: Use CORS middleware.
    • csrf: Configure CSRF middleware, including custom token extraction and skip rules.
    • custom: Write and register custom middleware.
    • pprof: Configure pprof middleware.
    • requestid: Configure RequestID middleware and custom ID handling.
    • gzip: Configure Gzip compression and route/path exclusions.
    • loadbalance: Configure a non-default load-balancing algorithm.
    • Recovery: Use the built-in Recovery middleware.
    • JWT: Use JWT authentication middleware.
    • i18n: Use internationalization middleware.
    • session: Use session middleware with cookie or Redis backends.
    • KeyAuth: Authenticate requests with a configurable key lookup.
    • Swagger: Serve Swagger documentation with Hertz.
    • access log: Log HTTP access details.
    • Secure: Configure security-related HTTP headers and redirects.
    • Sentry: Report Hertz request errors to Sentry.
    • Casbin: Apply Casbin authorization middleware.
    • ETag: Add ETag response headers and customize ETag behavior.
    • Cache: Cache Hertz responses with memory or Redis backends.
    • Paseto: Protect routes with PASETO tokens.
  • binding: Bind request parameters and validate them.
  • parameters: Read query, form, and cookie parameters.
  • file: Upload and download files, serve static files, and render HTML from files or templates.
  • render: Render JSON, HTML, Protobuf, text, XML, and customized YAML responses.
  • redirect: Redirect requests to internal or external URIs.
  • route: Register static, grouped, parameterized, and middleware-protected routes, and inspect route metadata.
  • streaming: Stream request and response bodies from a Hertz server.
  • graceful_shutdown: Gracefully shut down a Hertz server.
  • unit_test: Test Hertz handlers with ResponseRecord and PerformRequest without network transmission.
  • monitoring: Export Hertz and Go runtime metrics to Prometheus and visualize them in Grafana.
  • opentelemetry: Trace a Hertz-to-Kitex request, export telemetry through OpenTelemetry Collector, and view traces and metrics in Jaeger and Grafana/VictoriaMetrics.
  • multiple_service: Run multiple Hertz services on different ports.
  • adaptor: Adapt standard-library http.Handler implementations for use in Hertz, including a Jade template example.
  • sentinel: Use Sentinel for traffic control with Hertz.
  • reverseproxy: Use the Hertz reverse proxy extension, with examples for standard proxying, TLS, discovery, response modification, SSE, middleware, and WebSocket.
  • hlog: Use Hertz logging and its logger extensions.
  • trailer: Read and write HTTP trailers with Hertz.
  • graphql-go: Serve GraphQL requests from a Hertz server.

Client

Hz

  • hz/thrift: Use hz with Thrift IDL to generate server code.
  • hz/protobuf: Use hz with Protobuf IDL to generate server code.
  • hz/hz_client: Use hz to generate client code.
  • hz/template: Use a custom hz template to generate a server project.
  • hz/plugin: Use third-party hz code-generation plugins with Thrift and Protobuf projects.
  • hz/struct_reuse: Reuse Kitex-generated Thrift structs as Hertz models.

Note

Commands for an example should be run from the hertz-examples repository root unless that example’s own README says otherwise.

Directories

Path Synopsis
jade command
Code generated by "jade.go"; DO NOT EDIT.
Code generated by "jade.go"; DO NOT EDIT.
custom_error command
loose_zero command
bizdemo
hertz_gorm module
hertz_jwt module
hertz_session module
tiktok_demo module
client
add_parameters command
config command
forward_proxy command
middleware command
send_request command
streaming_read command
upload_file command
file
download command
html command
html-fs command
staticFile command
upload command
hlog
custom command
logrus command
slog command
standard command
zap command
zerolog command
hz
plugin/thrift module
middleware
CORS command
basicauth command
csrf/custom_keylookup command
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
csrf/custom_next command
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
csrf/default command
custom command
gzip/default command
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
gzip/extensions command
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
gzip/paths command
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
gzip/paths_regexes command
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
pprof/default command
client command
opentelemetry
hertz/client command
hertz/server command
kitex/server command
parameter
cookie command
form command
query command
protocol
http1 command
tls command
render
html command
json command
protobuf command
text command
* Copyright 2023 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2023 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
xml command
* Copyright 2023 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2023 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
yaml_customize command
* Copyright 2023 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2023 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
reverseproxy
customize_error command
modify_response command
sse command
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
standard command
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
tls command
use_middleware command
websocket command
sentinel
hertz/client command
hertz/server command
sse
client command
server command
streaming
streaming_read command
streaming_write command
* Copyright 2023 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright 2023 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.

Jump to

Keyboard shortcuts

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