Skip to content

Commit 6f6b05d

Browse files
committed
fix: enhance framework with cookie support and update README for new app initialization
1 parent ce92290 commit 6f6b05d

18 files changed

Lines changed: 64 additions & 25 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ A simple, developer-friendly API framework for Go, featuring automatic Swagger (
1515
**Create a new app**
1616

1717
```go
18-
import "github.com/go-simpl/simplapi"
18+
import (
19+
"github.com/go-simpl/simplapi"
20+
21+
_ "github.com/go-simpl/simplapi/pkg/framework/fiberframework"
22+
)
1923

2024
func main() {
21-
app := simplapi.New()
25+
app := simplapi.New("fiber")
2226

2327
// Add routes
2428

example/main.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

pkg/framework/fiberframework/framework.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/go-simpl/simplapi/pkg/context"
88
"github.com/go-simpl/simplapi/pkg/framework"
9+
910
"github.com/gofiber/fiber/v2"
1011
"github.com/gofiber/fiber/v2/middleware/adaptor"
1112
)

pkg/framework/fiberframework/request.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"mime/multipart"
55

66
"github.com/go-simpl/simplapi/pkg/framework"
7+
78
"github.com/gofiber/fiber/v2"
89
)
910

pkg/framework/fiberframework/response.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package fiberframework
22

33
import (
4+
"net/http"
5+
46
"github.com/go-simpl/simplapi/pkg/framework"
7+
58
"github.com/gofiber/fiber/v2"
69
)
710

@@ -23,6 +26,29 @@ func (r *fiberResponse) SetHeader(key string, value string) {
2326
r.c.Response().Header.Set(key, value)
2427
}
2528

29+
func (r *fiberResponse) SetCookie(cookie http.Cookie) {
30+
sameSite := ""
31+
switch cookie.SameSite {
32+
case http.SameSiteLaxMode:
33+
sameSite = "Lax"
34+
case http.SameSiteStrictMode:
35+
sameSite = "Strict"
36+
case http.SameSiteNoneMode:
37+
sameSite = "None"
38+
}
39+
r.c.Cookie(&fiber.Cookie{
40+
Name: cookie.Name,
41+
Value: cookie.Value,
42+
Path: cookie.Path,
43+
Domain: cookie.Domain,
44+
MaxAge: cookie.MaxAge,
45+
Expires: cookie.Expires,
46+
Secure: cookie.Secure,
47+
HTTPOnly: cookie.HttpOnly,
48+
SameSite: sameSite,
49+
})
50+
}
51+
2652
func (r *fiberResponse) SendJSON(data interface{}) error {
2753
return r.c.JSON(data)
2854
}

pkg/framework/ginframework/framework.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"net/http"
55
"net/http/httptest"
66

7-
"github.com/gin-gonic/gin"
87
"github.com/go-simpl/simplapi/pkg/context"
98
"github.com/go-simpl/simplapi/pkg/framework"
9+
10+
"github.com/gin-gonic/gin"
1011
)
1112

1213
type ginFramework struct {

pkg/framework/ginframework/request.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package ginframework
33
import (
44
"mime/multipart"
55

6-
"github.com/gin-gonic/gin"
76
"github.com/go-simpl/simplapi/pkg/framework"
7+
8+
"github.com/gin-gonic/gin"
89
)
910

1011
type ginRequest struct {

pkg/framework/ginframework/response.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package ginframework
22

33
import (
4-
"github.com/gin-gonic/gin"
4+
"net/http"
5+
56
"github.com/go-simpl/simplapi/pkg/framework"
7+
8+
"github.com/gin-gonic/gin"
69
)
710

811
type ginResponse struct {
@@ -23,6 +26,11 @@ func (r *ginResponse) SetHeader(key string, value string) {
2326
r.c.Header(key, value)
2427
}
2528

29+
func (r *ginResponse) SetCookie(cookie http.Cookie) {
30+
r.c.SetSameSite(cookie.SameSite)
31+
r.c.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly)
32+
}
33+
2634
func (r *ginResponse) SendJSON(data interface{}) error {
2735
r.c.JSON(r.c.Writer.Status(), data)
2836
return nil

pkg/framework/iface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type FrameworkRequest interface {
3535

3636
type FrameworkResponse interface {
3737
SetHeader(key string, value string)
38+
SetCookie(cookie http.Cookie)
3839
SetStatusCode(statusCode int)
3940
SendJSON(data interface{}) error
4041
SendString(data string) error

pkg/handler/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ func WrapHandler(handler interface{}, next framework.FrameworkHandler) framework
6161
if bodyDone {
6262
return nil
6363
}
64-
6564
}
6665
}
6766

0 commit comments

Comments
 (0)