-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (80 loc) · 1.98 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"log"
"net/http"
"os"
"safe_house/models"
"time"
"github.com/gin-gonic/gin"
)
func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
func main() {
r := gin.Default()
r.Use(DB())
// recover from panics with a 500
r.Use(func(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
c.AbortWithStatus(http.StatusInternalServerError)
log.Println("Recovered Panic (main.go):", r)
}
}()
c.Next()
})
r.GET("ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "pong"})
})
// AUTH ROUTES
{
r.POST("login", LoginHandler)
r.GET("logout", LogoutHandler)
r.POST("signup", UserCreate)
}
user := r.Group("user")
user.Use(Auth())
{
user.GET("/", UserShow) // Show the current user
user.PATCH("/", UserUpdate)
user.DELETE("/", UserDelete)
}
matches := r.Group("matches")
matches.Use(Auth())
{
matches.POST("/", MatchesList)
matches.POST("/:user_id", MatchesShow)
}
threads := r.Group("threads")
threads.Use(Auth())
{
threads.GET("/", MessageThreadIndex)
threads.POST("/", MessageThreadCreate)
threads.GET("/:user_id", MessageThreadShow)
threads.PATCH("/:thread_id/accept", MessageThreadStatus(models.MTOpened))
threads.PATCH("/:thread_id/close", MessageThreadStatus(models.MTClosed))
threads.PATCH("/:thread_id/block", MessageThreadStatus(models.MTBlocked))
threads.PATCH("/:thread_id/public_key", MessageThreadStatus(models.MTPubKeyChange))
}
messages := r.Group("messages")
messages.Use(Auth())
{
messages.GET("/:thread_id", MessageIndex)
messages.POST("/:thread_id", MessageCreate)
messages.GET("/:thread_id/subscribe", MessageThreadSubscribe)
}
Database.AutoMigrate(
&models.User{},
&models.MessageThread{},
&models.MessageThreadUser{},
&models.Message{},
)
s := &http.Server{
Addr: ":" + os.Getenv("PORT"),
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 3 * time.Minute,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}