Update something

This commit is contained in:
tiff 2025-01-26 16:47:01 -05:00
parent 915e44b69a
commit 498d446018
4 changed files with 153 additions and 5 deletions

140
cmd/api/api-spec.yaml Normal file
View File

@ -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

View File

@ -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")

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=