From 498d44601877d6cb2bd1614e17b9032207fb9665 Mon Sep 17 00:00:00 2001 From: tiff Date: Sun, 26 Jan 2025 16:47:01 -0500 Subject: [PATCH] Update something --- cmd/api/api-spec.yaml | 140 ++++++++++++++++++++++++++++++++++++++++++ cmd/api/main.go | 14 +++-- go.mod | 2 + go.sum | 2 + 4 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 cmd/api/api-spec.yaml diff --git a/cmd/api/api-spec.yaml b/cmd/api/api-spec.yaml new file mode 100644 index 0000000..2185f1a --- /dev/null +++ b/cmd/api/api-spec.yaml @@ -0,0 +1,140 @@ +openapi: 3.0.0 +info: + title: Calculator API + version: 1.0.0 +servers: + - url: http://localhost:3000 +paths: + /add: + post: + summary: Add two numbers + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + number1: + type: number + format: int + number2: + type: number + format: int + responses: + "200": + description: Successfully added two numbers + content: + application/json: + schema: + type: object + properties: + result: + type: number + format: int + /subtract: + post: + summary: Subtract two numbers + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + number1: + type: number + format: int + number2: + type: number + format: int + responses: + "200": + description: Successfully subtracted two numbers + content: + application/json: + schema: + type: object + properties: + result: + type: number + format: int + /multiply: + post: + summary: Multiply two numbers + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + number1: + type: number + format: int + number2: + type: number + format: int + responses: + "200": + description: Successfully multiplied two numbers + content: + application/json: + schema: + type: object + properties: + result: + type: number + format: int + /divide: + post: + summary: Divide two numbers + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + dividend: + type: number + format: int + divisor: + type: number + format: int + responses: + "200": + description: Successfully divided two numbers + content: + application/json: + schema: + type: object + properties: + result: + type: number + format: int + + /sum: + post: + summary: Add all numbers in an array + requestBody: + required: true + content: + application/json: + schema: + type: array + items: + type: number + format: int + + responses: + "200": + description: Successfully divided two numbers + content: + application/json: + schema: + type: object + properties: + result: + type: number + format: int diff --git a/cmd/api/main.go b/cmd/api/main.go index 52b031f..416fceb 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -3,32 +3,36 @@ package main import ( "context" "fmt" - "log" + "log/slog" "net/http" "os/signal" "syscall" "time" + "github.com/pocketbase/pocketbase" + "github.com/pocketbase/pocketbase/apis" + "github.com/pocketbase/pocketbase/core" "go-calc/internal/server" ) func gracefulShutdown(apiServer *http.Server, done chan bool) { // Create context that listens for the interrupt signal from the OS. - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + + logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) + slog.InfoContext(ctx.Context, msg string, args ...any), stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() // Listen for the interrupt signal. <-ctx.Done() - log.Println("shutting down gracefully, press Ctrl+C again to force") + logger.Info("shutting down gracefully, press Ctrl+C again to force") // The context is used to inform the server it has 5 seconds to finish // the request it is currently handling ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := apiServer.Shutdown(ctx); err != nil { - log.Printf("Server forced to shutdown with error: %v", err) - } + } log.Println("Server exiting") diff --git a/go.mod b/go.mod index 1e289d9..d99bcc3 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module go-calc go 1.23.2 require github.com/joho/godotenv v1.5.1 + +require github.com/rs/cors v1.11.1 // indirect diff --git a/go.sum b/go.sum index d61b19e..4d021da 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA= +github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=