-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute.go
80 lines (67 loc) · 1.63 KB
/
route.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
74
75
76
77
78
79
80
package main
import (
"github.com/anthonynguyen/go-manga"
"github.com/labstack/echo"
"net/http"
)
type ViewData struct {
Failed bool
Message string
Query string
Backend string
Data interface{}
}
func setupRoutes(e *echo.Echo) {
e.GET("/", route_main)
e.GET("/search", route_search)
e.GET("/manga/:backend/:id", route_manga)
e.GET("/chapter/:backend/:id", route_chapter)
}
func route_main(c echo.Context) error {
return c.Render(http.StatusOK, "index", ViewData{
Failed: true,
Message: "Use the bar above to search",
})
}
func route_search(c echo.Context) error {
query := c.QueryParam("q")
all, err := manga.Search(query)
if err != nil {
return c.Render(http.StatusBadRequest, "search", ViewData{
Failed: true,
Message: err.Error(),
Query: query,
})
}
return c.Render(http.StatusOK, "search", ViewData{
Failed: false,
Query: query,
Data: all,
})
}
func route_manga(c echo.Context) error {
requestedBackend := c.Param("backend")
requestedID := c.Param("id")
result, err := manga.Manga(requestedBackend, requestedID)
if err != nil {
return c.Render(http.StatusInternalServerError, "manga", ViewData{
Failed: true,
Message: err.Error(),
Backend: requestedBackend,
})
}
return c.Render(http.StatusOK, "manga", ViewData{
Failed: false,
Data: result,
Backend: requestedBackend,
})
}
func route_chapter(c echo.Context) error {
requestedBackend := c.Param("backend")
requestedID := c.Param("id")
result, err := manga.Chapter(requestedBackend, requestedID)
if err != nil {
return c.String(http.StatusInternalServerError, "")
}
return c.JSON(http.StatusOK, result)
}