Skip to content

Commit

Permalink
Merge branch 'caching'
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnBakhmat committed Jun 5, 2024
2 parents bf4ab3b + 0167ea7 commit 0ced52f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
.envrc
/bin
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
github.com/gofiber/fiber/v3 v3.0.0-20240113152607-1588b6b60210
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/shurcooL/githubv4 v0.0.0-20231126234147-1cffa1f02456
golang.org/x/oauth2 v0.16.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shurcooL/githubv4 v0.0.0-20231126234147-1cffa1f02456 h1:6dExqsYngGEiixqa1vmtlUd+zbyISilg0Cf3GWVdeYM=
Expand Down
12 changes: 6 additions & 6 deletions graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
)

type Project struct {
Name string
Description string
Url string
Stars int
Forks int
Languages []string
Name string `json:"name"`
Description string `json:"description"`
Url string `json:"url"`
Stars int `json:"stars"`
Forks int `json:"forks"`
Languages []string `json:"languages"`
}

func GetProjects(username string) ([]Project, error) {
Expand Down
24 changes: 17 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"log"
"os"
"strconv"
"time"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/cors"
"github.com/patrickmn/go-cache"
"johnbakhmat.tech/pinned/graphql"
)

Expand All @@ -22,22 +24,30 @@ func main() {
log.Fatal(err)
}

app := fiber.New()

app.Use(cors.New())
c := cache.New(5*time.Minute, 10*time.Minute)

app := fiber.New()
app.Use(cors.New())
app.Get("/", func(c fiber.Ctx) error {
return c.SendStatus(200)
})
app.Get("/projects/:username", func(c fiber.Ctx) error {
username := c.Params("username", "johnbakhmat")
app.Get("/projects/:username", func(ctx fiber.Ctx) error {
username := ctx.Params("username", "johnbakhmat")

if projects_cache, cache_hit := c.Get(username); cache_hit {
log.Println("Cache hit")
projects := projects_cache.(*[]graphql.Project)
return ctx.JSON(*projects)
}

projects, err := graphql.GetProjects(username)
if err != nil {
log.Println(err)
return c.SendStatus(500)
return ctx.SendStatus(500)
}
log.Printf("%v", projects)
return c.JSON(projects)
c.Set(username, &projects, cache.DefaultExpiration)
return ctx.JSON(projects)
})

log.Fatal(app.Listen(fmt.Sprintf(":%d", port)))
Expand Down

0 comments on commit 0ced52f

Please sign in to comment.