-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
83 lines (62 loc) · 2.26 KB
/
api.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
81
82
83
package main
import (
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/uhuraapp/uhura-api/database"
"github.com/uhuraapp/uhura-api/middleware"
"github.com/uhuraapp/uhura-api/services"
)
func main() {
r := gin.Default()
r.Use(middleware.CORS())
r.Use(middleware.Recovery())
Mount(r.Group("/"))
log.Println("Listening", os.Getenv("PORT"), "...")
log.Println(http.ListenAndServe(":"+os.Getenv("PORT"), middleware.Gzip(r)))
}
func Mount(_r *gin.RouterGroup) {
DB := database.NewPostgresql()
userSubscriptions := services.NewUserSubscriptionService(DB)
channels := services.NewChannelsService(DB)
episodes := services.NewEpisodesService(DB)
auth := services.NewAuthService(DB)
categories := services.NewCategoriesService(DB)
parser := services.NewParser(DB)
users := services.NewUserService(DB)
export := services.NewExportService(DB)
_r.Use(middleware.Authentication(DB))
needAuth := middleware.Protected()
apiAuth := middleware.ApiProtected()
r := _r.Group("/v2")
{
r.OPTIONS("*action", func(c *gin.Context) { c.Data(200, "", []byte{}) })
r.GET("/top/channels", channels.Top)
r.GET("/parser", parser.ByURL)
r.GET("/auth/:provider", auth.ByProvider)
r.GET("/auth/:provider/callback", auth.ByProviderCallback)
r.GET("/user", auth.GetUser)
r.PUT("/user", auth.UpdateUser)
r.DELETE("/user", auth.DeleteUser)
r.POST("/users/sign_in", auth.ByEmailPassword)
r.GET("/users/logout", auth.Logout)
r.POST("/users", auth.SignUp)
r.GET("/users/subscriptions", needAuth, userSubscriptions.Index)
r.POST("/users/subscriptions", needAuth, userSubscriptions.Create)
r.GET("/users/subscriptions/:uri", needAuth, userSubscriptions.Show)
r.DELETE("/users/subscriptions/:uri", needAuth, userSubscriptions.Delete)
r.POST("/channels/:channel_id/episodes/:id/played", needAuth, episodes.Played)
r.DELETE("/channels/:channel_id/episodes/:id/played", needAuth, episodes.UnPlayed)
r.GET("/channels/:channel_id/episodes/:id/download", needAuth, episodes.Download)
r.GET("/channels/:channel_id", channels.Get)
r.GET("/categories", categories.Index)
r.GET("/categories/:uri", categories.Get)
r.GET("/export/:id", export.Get)
}
v := _r.Group("/v3")
{
v.GET("/users.json", apiAuth, users.All)
v.GET("/users/:id", apiAuth, users.Get)
}
}