Labstack/echo: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 44: Line 44:
=Registering Handlers Generated by oapi-codegen from an OpenAPI Specification=
=Registering Handlers Generated by oapi-codegen from an OpenAPI Specification=
{{Internal|Oapi-codegen#server|<tt>oapi-codegen</tt> Server Code Generation}}
{{Internal|Oapi-codegen#server|<tt>oapi-codegen</tt> Server Code Generation}}
<code>[[Oapi-codegen#Overview|oapi-codegen]]</code> generates a <code>ServerInterface</code> interface that exposes methods for [[OpenAPI_Specification_Path#Server_Code_Generation_for_Path/Operation_Combinations|each path/operation pair]] specified in the OpenAPI specification:
<syntaxhighlight lang='go'>
// ServerInterface represents all server handlers.
type ServerInterface interface {
  // (GET /pets)
  GetPets(ctx echo.Context, params GetPetsParams) error
  // (POST /pets)
  CreatePet(ctx echo.Context) error
  ...
}
</syntaxhighlight>


==Implement <tt>oapi-codegen</tt>-generated <tt>ServerInterface</tt>==
Implement the <code>ServerInterface</code>:
 
<code>[[Oapi-codegen#Overview|oapi-codegen]]</code> generates a <code>ServerInterface</code> interface that exposes methods to be implemented by the server.
 
Implement that interface:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
type PetStoreServer struct {
type PetStoreServer struct {
  // ...
}
}


Line 60: Line 66:


func (s *PetStoreServer) GetPets(ctx echo.Context, params petstore.GetPetsParams) error {
func (s *PetStoreServer) GetPets(ctx echo.Context, params petstore.GetPetsParams) error {
   return nil
   ...
}
 
func (s *PetStoreServer) CreatePet(ctx echo.Context) error {
  ...
}
}
...
</syntaxhighlight>
</syntaxhighlight>


==Register the Handlers==
Then instantiate an <code>echo</code> server and register the handlers:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
ps := NewPetStoreServer()
ps := NewPetStoreServer()

Revision as of 22:12, 26 January 2024

External

Internal

Overview

Install Dependencies

From the root directory of your module, execute:

go get github.com/labstack/echo/v4@v4.11.4

Server

package main

import (
	"net/http"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

func hello(c echo.Context) error {
	return c.String(http.StatusOK, "hello!")
}

func main() {
	s := echo.New()
	// Middleware
	s.Use(middleware.Logger())
	s.Use(middleware.Recover())
	// Routes
	s.GET("/", hello)
	// Start server
	s.Logger.Fatal(s.Start(":30000"))
}

Registering Handlers Generated by oapi-codegen from an OpenAPI Specification

oapi-codegen Server Code Generation

oapi-codegen generates a ServerInterface interface that exposes methods for each path/operation pair specified in the OpenAPI specification:

// ServerInterface represents all server handlers.
type ServerInterface interface {
  // (GET /pets)
  GetPets(ctx echo.Context, params GetPetsParams) error
  // (POST /pets)
  CreatePet(ctx echo.Context) error
  ...
}

Implement the ServerInterface:

type PetStoreServer struct {
}

func NewPetStoreServer() *PetStoreServer {
	return &PetStoreServer{}
}

func (s *PetStoreServer) GetPets(ctx echo.Context, params petstore.GetPetsParams) error {
  ...
}

func (s *PetStoreServer) CreatePet(ctx echo.Context) error {
  ...
}

...

Then instantiate an echo server and register the handlers:

ps := NewPetStoreServer()
es := echo.New()
// Middleware
es.Use(middleware.Logger())
es.Use(middleware.Recover())

// Routes
petstore.RegisterHandlers(es, ps)

// Start server
es.Logger.Fatal(es.Start(":30000"))