-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (45 loc) · 1.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"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"
)
func main() {
portStr := os.Getenv("PORT")
if portStr == "" {
portStr = "80"
}
port, err := strconv.Atoi(portStr)
if err != nil {
log.Fatal(err)
}
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(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 ctx.SendStatus(500)
}
log.Printf("%v", projects)
c.Set(username, &projects, cache.DefaultExpiration)
return ctx.JSON(projects)
})
log.Fatal(app.Listen(fmt.Sprintf(":%d", port)))
}