This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
201 lines (162 loc) · 5.69 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/recoilme/slowpoke"
"github.com/joho/godotenv"
"github.com/recoilme/tgram/routers"
)
// Port - typegram port (and address)
var Port = ":8081"
// LoadEnv parse env file if present or load
func LoadEnv() {
err := godotenv.Load("tgram.env")
if err == nil {
setifset := func(new, def string) string {
if new == "" {
return def
}
return new
}
Port = setifset(os.Getenv("TGRAMPORT"), ":8081") //port with ":", example - :8081
routers.Config.Title = setifset(os.Getenv("TGRAMTITLE"), "typegram")
routers.Config.SiteName = setifset(os.Getenv("TGRAMNAME"), "Typegram")
routers.Config.Description = setifset(os.Getenv("TGRAMDESC"), "open source publishing platform")
routers.Config.Admin = setifset(os.Getenv("TGRAMADMIN"), "recoilme")
routers.Config.AboutPage = setifset(os.Getenv("TGRAMABOUT"), "/@recoilme/1")
routers.Config.Domain = setifset(os.Getenv("TGRAMDOMAIN"), "tgr.am")
routers.Config.NBSecretPassword = setifset(os.Getenv("TGRAMPWD"), "A String Very Very Very Niubilty!!@##$!@#4")
routers.Config.Type2TeleBot = setifset(os.Getenv("TGRAM2TELE"), "")
routers.Config.SMTPHost = setifset(os.Getenv("TGRAMSMTPHOST"), "")
routers.Config.SMTPPort = setifset(os.Getenv("TGRAMSMTPPORT"), "") //port with ":", example - :587
routers.Config.SMTPUser = setifset(os.Getenv("TGRAMSMTPUSER"), "")
routers.Config.SMTPPassword = setifset(os.Getenv("TGRAMSMTPPASS"), "")
routers.Config.FCMAuth = setifset(os.Getenv("TGRAMFCMAUTH"), "")
}
}
func main() {
LoadEnv()
srv := &http.Server{
Addr: Port,
Handler: InitRouter(),
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
// Close db
if err := slowpoke.CloseAll(); err != nil {
log.Fatal("Database Shutdown:", err)
}
log.Println("Server exiting")
}
func globalRecover(c *gin.Context) {
defer func(c *gin.Context) {
if err := recover(); err != nil {
if err := slowpoke.CloseAll(); err != nil {
log.Println("Database Shutdown err:", err)
}
log.Println("Server recovery with err:", err)
gin.RecoveryWithWriter(gin.DefaultErrorWriter)
//c.AbortWithStatus(500)
}
}(c)
c.Next()
}
// InitRouter - init router
func InitRouter() *gin.Engine {
//gin.SetMode(gin.DebugMode)
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(globalRecover)
//r.Use(gin.Recovery())
//gin.DefaultWriter = ioutil.Discard
r.Use(static.Serve("/m", static.LocalFile("./media", false)))
r.Use(static.Serve("/i", static.LocalFile("./img", false))) //where i use it?
r.Use(static.Serve("/a", static.LocalFile("./ava", false)))
r.Use(static.Serve("/", static.LocalFile("./media/txt", false))) //for ssl cert
r.SetFuncMap(template.FuncMap{
"tostr": routers.ToStr,
"todate": routers.ToDate,
"getlead": routers.GetLead,
"var": routers.NewVar,
"set": routers.SetVar,
})
r.LoadHTMLGlob("views/*.html")
r.Use(routers.CheckAuth())
//r.Use(CORSMiddleware())
r.GET("/", routers.Main)
r.GET("/home", routers.Home)
r.GET("/mid", routers.All)
r.GET("/top", routers.Top)
r.GET("/btm", routers.Btm)
r.GET("/register", routers.Register)
r.POST("/register", routers.Register)
r.GET("/login", routers.Login)
r.POST("/login", routers.Login)
r.GET("/@:username/:aid", routers.Article)
r.GET("/@:username", routers.Author)
r.GET("/a/:avatar", routers.Avatar)
r.GET("/favorites/@:username", routers.Favorites)
r.GET("/policy", routers.Policy)
r.GET("/terms", routers.Terms)
// only for registered users
r.Use(routers.GoToRegister())
r.GET("/settings", routers.Settings)
r.POST("/settings", routers.Settings)
r.POST("/logout", routers.Logout)
r.GET("/delete/a/:aid", routers.ArticleDelete)
r.GET("/bad/@:author/:aid/:bad", routers.ArticleBad)
r.GET("/editor/:aid", routers.Editor)
r.POST("/editor/:aid", routers.Editor)
r.GET("follow/:user/*action", routers.Follow)
r.GET("unfollow/:user/*action", routers.Unfollow)
r.GET("fav/:aid/*action", routers.Fav)
r.GET("unfav/:aid/*action", routers.Unfav)
r.GET("vote/:mode/@:author/:aid", routers.Vote)
r.POST("/comments/@:username/:aid", routers.CommentNew)
r.GET("/commentup/@:authorart/:authorcom/:aid/:cid", routers.CommentUp)
r.GET("/commentdel/@:authorart/:authorcom/:aid/:cid", routers.CommentDel)
r.GET("/upload", routers.Upload)
r.POST("/upload", routers.Upload)
r.GET("/export/type2tele", routers.Type2tele)
r.POST("/export/type2tele", routers.Type2tele)
return r
}
// CORSMiddleware - open for request from javascript
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://crazydoge.com/")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
//fmt.Println("OPTIONS")
c.AbortWithStatus(200)
} else {
c.Next()
}
}
}