Skip to content

Commit

Permalink
feat(rest): add simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Sep 7, 2024
1 parent 5002cf2 commit e6c10e0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/lvlcn-t/go-kit/dependency v0.0.0-20240813221656-294916221526
github.com/lvlcn-t/go-kit/executors v0.3.0
github.com/lvlcn-t/go-kit/metrics v0.3.0
github.com/lvlcn-t/go-kit/rest v0.0.0-20240907124556-5002cf27c2a0
github.com/lvlcn-t/loggerhead v0.3.1
github.com/prometheus/client_golang v1.20.3
go.opentelemetry.io/otel v1.29.0
Expand Down
4 changes: 4 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand All @@ -58,6 +60,8 @@ github.com/lvlcn-t/go-kit/lists v0.3.0 h1:MlxQdLXavtR010oDcuLx0L2Qcz/Ib/OwFTcme+
github.com/lvlcn-t/go-kit/lists v0.3.0/go.mod h1:lueTESRKTfvF6UYuHttUKs/Unu4PGp+Ex/CAN0vcJrE=
github.com/lvlcn-t/go-kit/metrics v0.3.0 h1:SMy+/57GX7CjF6Kd9DsCV/SpXE8ljpDjtM3TYFWuLq4=
github.com/lvlcn-t/go-kit/metrics v0.3.0/go.mod h1:7neTHi/b3g7vTGamJbUmGBzyl9G+hXWs8f3NWYMqE1E=
github.com/lvlcn-t/go-kit/rest v0.0.0-20240907124556-5002cf27c2a0 h1:fugyY1T6Z0Zqvp9CLuvn5IpxqqDvmSamDey0sq0mFnM=
github.com/lvlcn-t/go-kit/rest v0.0.0-20240907124556-5002cf27c2a0/go.mod h1:hqT1WJUsS/o90NGa9gMryDdlmafbp0W6VYb6eR3U05Q=
github.com/lvlcn-t/loggerhead v0.3.1 h1:SXL6qGQVctWzOmmTYwQviNTrP1+dpdbLtGLMuEgiVDI=
github.com/lvlcn-t/loggerhead v0.3.1/go.mod h1:8sbYrIpxKKTaWFoqfy9onMR7tmofsZLYWz8ZULi/jXU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
82 changes: 82 additions & 0 deletions example/rest/simple/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

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

"github.com/gofiber/fiber/v3"
"github.com/lvlcn-t/go-kit/apimanager"
"github.com/lvlcn-t/go-kit/rest"
)

// Define the response type
type response struct {
ID int `json:"id"`
Name string `json:"name"`
}

func main() {
ctx := context.Background()
// Create a new API server
srv := apimanager.New(&apimanager.Config{
Address: ":8080",
})

// Mount a new route
err := srv.Mount(apimanager.Route{
Methods: []string{http.MethodGet},
Path: "/",
Handler: func(c fiber.Ctx) error {
return c.Status(http.StatusOK).JSON(response{
ID: 1,
Name: "Jane Doe",
})
},
})
if err != nil {
panic(err)
}

// Run the server in a goroutine
go func() {
if err = srv.Run(ctx); err != nil {
panic(err)
}
}()

// Wait for the server to start
time.Sleep(time.Second)

// Make an API call to the server
makeAPICall(ctx)

// Shutdown the server
err = srv.Shutdown(ctx)
if err != nil {
panic(err)
}
}

// makeAPICall makes an API call to the server
func makeAPICall(ctx context.Context) {
// Create a new REST endpoint
endpoint := rest.Get("http://localhost:8080")

// Make a request to the server
resp, status, err := rest.Do[response](ctx, endpoint, nil)
if err != nil {
panic(err)
}
// Close the rest client gracefully (optional)
defer rest.Close(ctx)

// Check the status code
if status != http.StatusOK {
panic(fmt.Errorf("unexpected status code: %d", status))
}

// Print the response
fmt.Printf("\nResponse from the server: %+v\n", resp)
}

0 comments on commit e6c10e0

Please sign in to comment.