Skip to content

Commit 30e81af

Browse files
committed
refactor(template, auth): simplify Dist function and update HTTP request handling with fiber client
1 parent 1e58f77 commit 30e81af

File tree

5 files changed

+30
-33
lines changed

5 files changed

+30
-33
lines changed

entgo-sveltekit/template/template.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ package template
33
import (
44
"embed"
55
"io/fs"
6-
"log"
7-
"net/http"
86
)
97

108
//go:embed all:dist
119
var content embed.FS
1210

1311
// All returns the content of the all directory.
14-
func Dist() http.FileSystem {
15-
dist, err := fs.Sub(content, "dist")
16-
if err != nil {
17-
log.Fatal(err)
18-
}
19-
return http.FS(dist)
12+
func Dist() fs.FS {
13+
return content
2014
}

oauth2/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212

1313
require (
1414
github.com/andybalholm/brotli v1.2.0 // indirect
15+
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
1516
github.com/gofiber/schema v1.6.0 // indirect
1617
github.com/gofiber/template v1.8.3 // indirect
1718
github.com/gofiber/utils v1.1.0 // indirect
@@ -25,6 +26,7 @@ require (
2526
github.com/tinylib/msgp v1.5.0 // indirect
2627
github.com/valyala/bytebufferpool v1.0.0 // indirect
2728
github.com/valyala/fasthttp v1.68.0 // indirect
29+
github.com/x448/float16 v0.8.4 // indirect
2830
golang.org/x/crypto v0.44.0 // indirect
2931
golang.org/x/net v0.47.0 // indirect
3032
golang.org/x/sys v0.38.0 // indirect

oauth2/middleware/auth.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"oauth2/models"
77

88
"github.com/gofiber/fiber/v3"
9+
"github.com/gofiber/fiber/v3/client"
910
"github.com/gofiber/fiber/v3/middleware/session"
1011
)
1112

@@ -22,26 +23,33 @@ func OAUTHRedirect(ctx fiber.Ctx) error {
2223

2324
// Next, lets for the HTTP request to call the github oauth enpoint to get our access token
2425

25-
a := fiber.AcquireAgent()
26-
req := a.Request()
27-
req.Header.SetMethod(fiber.MethodPost)
28-
req.Header.Set("accept", "application/json")
29-
req.SetRequestURI(fmt.Sprintf("https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", models.ClientID, models.ClientSecret, code))
30-
if err := a.Parse(); err != nil {
31-
models.SYSLOG.Errorf("could not create HTTP request: %v", err)
32-
}
33-
26+
a := client.New()
27+
req := a.R()
28+
req.SetMethod("POST")
29+
req.SetURL(fmt.Sprintf("https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", models.ClientID, models.ClientSecret, code))
30+
req.SetHeader("accept", "application/json")
3431
var retCode int
3532
var retBody []byte
36-
var errs []error
3733
// Send out the HTTP request
3834
var t *models.OAuthAccessResponse
39-
40-
if retCode, retBody, errs = a.Struct(&t); len(errs) > 0 {
35+
resp, clientErr := req.Send()
36+
if clientErr != nil {
37+
models.SYSLOG.Errorf("could not create HTTP request: %v", clientErr)
38+
}
39+
if clientErr == nil {
40+
retCode = resp.StatusCode()
41+
retBody = resp.Body()
42+
clientErr = resp.JSON(&t)
43+
}
44+
if clientErr != nil {
4145
models.SYSLOG.Tracef("received: %v", string(retBody))
42-
models.SYSLOG.Errorf("could not send HTTP request: %v", errs)
46+
models.SYSLOG.Errorf("could not send HTTP request: %v", clientErr)
4347
return ctx.SendStatus(fiber.StatusInternalServerError)
4448
}
49+
var errs []error
50+
if clientErr != nil {
51+
errs = append(errs, clientErr)
52+
}
4553
models.SYSLOG.Tracef("received : %v %v %v", retCode, string(retBody), errs)
4654

4755
var sess *session.Session

sveltekit-embed/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package main
22

33
import (
4-
"app/template"
5-
"os"
6-
74
"github.com/gofiber/fiber/v3"
85
"github.com/gofiber/fiber/v3/middleware/cors"
96
"github.com/gofiber/fiber/v3/middleware/logger"
107
"github.com/gofiber/fiber/v3/middleware/static"
8+
9+
"app/template"
1110
)
1211

1312
const (
@@ -31,7 +30,7 @@ func main() {
3130
}))
3231
// Serve static files
3332
app.All("/*", static.New("", static.Config{
34-
FS: os.DirFS(template.Dist()),
33+
FS: template.Dist(),
3534
// TODO: Migrate to NotFoundHandler (fiber.Handler) - NotFoundFile is deprecated
3635
// NotFoundFile: "index.html",
3736
IndexNames: []string{"index.html"},

sveltekit-embed/template/template.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ package template
33
import (
44
"embed"
55
"io/fs"
6-
"log"
7-
"net/http"
86
)
97

108
//go:embed all:dist
119
var content embed.FS
1210

1311
// All returns the content of the all directory.
14-
func Dist() http.FileSystem {
15-
dist, err := fs.Sub(content, "dist")
16-
if err != nil {
17-
log.Fatal(err)
18-
}
19-
return http.FS(dist)
12+
func Dist() fs.FS {
13+
return content
2014
}

0 commit comments

Comments
 (0)