From e6c10e0f58af0526c2f796e83ce04e1c68d2d938 Mon Sep 17 00:00:00 2001 From: lvlcn-t <75443136+lvlcn-t@users.noreply.github.com> Date: Sat, 7 Sep 2024 19:54:10 +0200 Subject: [PATCH] feat(rest): add simple example --- example/go.mod | 1 + example/go.sum | 4 ++ example/rest/simple/app.go | 82 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 example/rest/simple/app.go diff --git a/example/go.mod b/example/go.mod index 20d7343..b290be8 100644 --- a/example/go.mod +++ b/example/go.mod @@ -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 diff --git a/example/go.sum b/example/go.sum index 42504bc..8a4f1f5 100644 --- a/example/go.sum +++ b/example/go.sum @@ -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= @@ -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= diff --git a/example/rest/simple/app.go b/example/rest/simple/app.go new file mode 100644 index 0000000..18a3a66 --- /dev/null +++ b/example/rest/simple/app.go @@ -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) +}