Skip to content

Commit 1b6b03e

Browse files
committed
Add OpenAPI security schemes configuration and upgrade Okapi version
1 parent 4ea6a98 commit 1b6b03e

File tree

10 files changed

+1619
-66
lines changed

10 files changed

+1619
-66
lines changed

Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
########################
22
# Builder Stage
33
########################
4-
FROM golang:1.24.4 AS build
4+
FROM golang:1.24.5 AS build
55

66
WORKDIR /app
77

@@ -28,6 +28,10 @@ RUN apk --update --no-cache add tzdata ca-certificates curl
2828
COPY --from=build /app/okapi-example /usr/local/bin/okapi-example
2929
RUN chmod a+x /usr/local/bin/okapi-example && ln -s /usr/local/bin/okapi-example /okapi-example
3030

31+
## Copy data directory
32+
COPY data /app/data
33+
# Set working directory
34+
WORKDIR /app
3135
# Expose HTTP Port
3236
EXPOSE 8080
3337

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ go run .
4444
```shell
4545
docker run --rm --name okapi-example -p 8080:8080 jkaninda/okapi-example
4646
```
47+
Use `JWT_SIGNING_SECRET` environment variable if you want to change JWT secret, default: `supersecret`
4748

4849
Visit [`http://localhost:8080`](http://localhost:8080) to see the response:
4950

controllers/controller.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
package controllers
2626

2727
import (
28+
"encoding/json"
2829
"fmt"
2930
"github.com/jkaninda/logger"
31+
"github.com/jkaninda/okapi"
32+
"github.com/jkaninda/okapi-example/middlewares"
3033
"github.com/jkaninda/okapi-example/models"
3134
"net/http"
35+
"os"
3236
"strconv"
33-
34-
"github.com/jkaninda/okapi"
35-
"github.com/jkaninda/okapi-example/middlewares"
3637
)
3738

3839
type BookController struct{}
@@ -41,9 +42,9 @@ type AuthController struct{}
4142

4243
var (
4344
books = []*models.Book{
44-
{Id: 1, Name: "Book One", Price: 100},
45-
{Id: 2, Name: "Book Two", Price: 200},
46-
{Id: 3, Name: "Book Three", Price: 300},
45+
{Id: 1, Title: "Book One", Price: 100},
46+
{Id: 2, Title: "Book Two", Price: 200},
47+
{Id: 3, Title: "Book Three", Price: 300},
4748
}
4849
)
4950

@@ -59,8 +60,8 @@ func (hc *HomeController) WhoAmI(c okapi.Context) error {
5960
logger.Warn("no email found")
6061
}
6162
return c.OK(models.WhoAmIResponse{
62-
Hostname: c.Request().Host,
63-
RealIp: c.RealIP(),
63+
Host: c.Request().Host,
64+
RealIp: c.RealIP(),
6465
CurrentUser: models.UserInfo{
6566
Name: c.Header("current_user_name"),
6667
Email: email,
@@ -69,7 +70,11 @@ func (hc *HomeController) WhoAmI(c okapi.Context) error {
6970
})
7071
}
7172
func (bc *BookController) GetBooks(c okapi.Context) error {
72-
// Simulate fetching books from a database
73+
_, err := bc.readBooksFromFile()
74+
if err != nil {
75+
logger.Error("Error reading books from file", "error", err)
76+
return c.ErrorInternalServerError(models.ErrorResponse{Success: false, Status: http.StatusInternalServerError, Details: err.Error()})
77+
}
7378
return c.OK(books)
7479
}
7580

@@ -97,6 +102,11 @@ func (bc *BookController) GetBook(c okapi.Context) error {
97102
}
98103
// Simulate a fetching book from a database
99104

105+
_, err = bc.readBooksFromFile()
106+
if err != nil {
107+
logger.Error("Error reading books from file", "error", err)
108+
return c.ErrorInternalServerError(models.ErrorResponse{Success: false, Status: http.StatusInternalServerError, Details: err.Error()})
109+
}
100110
for _, book := range books {
101111
if book.Id == i {
102112
return c.OK(book)
@@ -138,3 +148,20 @@ func (bc *AuthController) WhoAmI(c okapi.Context) error {
138148
},
139149
)
140150
}
151+
152+
func (bc *BookController) readBooksFromFile() ([]*models.Book, error) {
153+
if books != nil && len(books) > 3 {
154+
return books, nil
155+
}
156+
booksFile, err := os.ReadFile("data/books.json")
157+
if err != nil {
158+
logger.Error("Error reading books file", "error", err)
159+
return nil, fmt.Errorf("failed to read books data: %w", err)
160+
}
161+
err = json.Unmarshal(booksFile, &books)
162+
if err != nil {
163+
logger.Error("Error unmarshalling books data", "error", err)
164+
return nil, fmt.Errorf("failed to parse books data: %w", err)
165+
}
166+
return books, nil
167+
}

0 commit comments

Comments
 (0)