-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (62 loc) · 1.41 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"errors"
"fmt"
"net/http"
"os"
"github.com/chickenzord/decentrss/internal/database"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
var (
bindPort = "8080"
)
func init() {
if port := os.Getenv("BIND_PORT"); port != "" {
bindPort = port
}
}
func main() {
e := echo.New()
e.HideBanner = true
e.Use(middleware.Recover())
e.Use(middleware.Logger())
e.Use(middleware.Gzip())
e.GET("/ping", func(c echo.Context) error {
return c.String(200, "pong")
})
e.GET("/feed", func(c echo.Context) error {
url := c.QueryParam("url")
if url == "" {
return c.String(400, "url is required")
}
feed, err := database.GetFeed(url)
if err != nil {
if errors.Is(err, &database.ErrFeedNotFound{}) {
return c.String(404, err.Error())
}
return c.String(500, err.Error())
}
c.Response().Header().Set("Content-Type", "application/json")
if err := feed.WriteJSON(c.Response().Writer); err != nil {
return c.String(500, err.Error())
}
return nil
})
e.POST("/feed", func(c echo.Context) error {
url, itemCount, err := database.ParseAndSaveFeed(c.Request().Body)
if err != nil {
return c.String(500, err.Error())
}
return c.JSON(http.StatusAccepted, echo.Map{
"success": true,
"data": echo.Map{
"item_count": itemCount,
"url": url,
},
})
})
if err := e.Start(fmt.Sprintf(":%s", bindPort)); err != nil {
panic(err)
}
}