2016-04-13 07:22:04 +00:00
|
|
|
package gorush
|
2016-03-24 09:01:43 +00:00
|
|
|
|
|
|
|
import (
|
2017-04-05 04:03:43 +00:00
|
|
|
"crypto/tls"
|
2016-04-10 08:00:17 +00:00
|
|
|
"fmt"
|
2017-01-19 08:56:30 +00:00
|
|
|
"net/http"
|
|
|
|
|
2016-03-24 16:53:45 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2017-08-06 01:53:38 +00:00
|
|
|
"github.com/gin-gonic/gin/binding"
|
2017-01-19 08:56:30 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2017-04-05 04:03:43 +00:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
2016-06-22 02:13:43 +00:00
|
|
|
api "gopkg.in/appleboy/gin-status-api.v1"
|
2016-03-24 09:01:43 +00:00
|
|
|
)
|
|
|
|
|
2017-01-19 08:56:30 +00:00
|
|
|
func init() {
|
|
|
|
// Support metrics
|
|
|
|
m := NewMetrics()
|
|
|
|
prometheus.MustRegister(m)
|
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
func abortWithError(c *gin.Context, code int, message string) {
|
2017-03-29 01:21:31 +00:00
|
|
|
c.AbortWithStatusJSON(code, gin.H{
|
2016-03-24 16:46:37 +00:00
|
|
|
"code": code,
|
|
|
|
"message": message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func rootHandler(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2016-03-27 16:04:01 +00:00
|
|
|
"text": "Welcome to notification server.",
|
2016-03-24 16:46:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func pushHandler(c *gin.Context) {
|
2016-04-10 04:02:44 +00:00
|
|
|
var form RequestPush
|
2016-04-10 05:04:32 +00:00
|
|
|
var msg string
|
2016-03-24 16:46:37 +00:00
|
|
|
|
2017-08-06 01:53:38 +00:00
|
|
|
if err := c.ShouldBindWith(&form, binding.JSON); err != nil {
|
2016-04-10 07:15:54 +00:00
|
|
|
msg = "Missing notifications field."
|
2016-04-10 05:04:32 +00:00
|
|
|
LogAccess.Debug(msg)
|
2016-04-13 06:59:28 +00:00
|
|
|
abortWithError(c, http.StatusBadRequest, msg)
|
2016-04-10 04:02:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(form.Notifications) == 0 {
|
2016-04-13 03:43:07 +00:00
|
|
|
msg = "Notifications field is empty."
|
2016-04-10 05:04:32 +00:00
|
|
|
LogAccess.Debug(msg)
|
2016-04-13 06:59:28 +00:00
|
|
|
abortWithError(c, http.StatusBadRequest, msg)
|
2016-04-10 05:04:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-02 07:55:47 +00:00
|
|
|
if int64(len(form.Notifications)) > PushConf.Core.MaxNotification {
|
2016-04-10 05:04:32 +00:00
|
|
|
msg = fmt.Sprintf("Number of notifications(%d) over limit(%d)", len(form.Notifications), PushConf.Core.MaxNotification)
|
|
|
|
LogAccess.Debug(msg)
|
2016-04-13 06:59:28 +00:00
|
|
|
abortWithError(c, http.StatusBadRequest, msg)
|
2016-03-24 16:46:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-10 03:46:48 +00:00
|
|
|
counts, logs := queueNotification(form)
|
2016-03-24 16:46:37 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2016-04-13 03:43:07 +00:00
|
|
|
"success": "ok",
|
2017-04-06 07:00:49 +00:00
|
|
|
"counts": counts,
|
2017-04-10 03:46:48 +00:00
|
|
|
"logs": logs,
|
2016-03-24 09:01:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-15 07:05:23 +00:00
|
|
|
func configHandler(c *gin.Context) {
|
|
|
|
c.YAML(http.StatusCreated, PushConf)
|
|
|
|
}
|
|
|
|
|
2017-01-19 08:56:30 +00:00
|
|
|
func metricsHandler(c *gin.Context) {
|
|
|
|
promhttp.Handler().ServeHTTP(c.Writer, c.Request)
|
|
|
|
}
|
|
|
|
|
2017-04-05 04:03:43 +00:00
|
|
|
func autoTLSServer() *http.Server {
|
|
|
|
m := autocert.Manager{
|
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
HostPolicy: autocert.HostWhitelist(PushConf.Core.AutoTLS.Host),
|
|
|
|
Cache: autocert.DirCache(PushConf.Core.AutoTLS.Folder),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Server{
|
|
|
|
Addr: ":https",
|
|
|
|
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
|
|
|
|
Handler: routerEngine(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
func routerEngine() *gin.Engine {
|
2016-04-01 01:53:10 +00:00
|
|
|
// set server mode
|
|
|
|
gin.SetMode(PushConf.Core.Mode)
|
2016-03-27 13:12:11 +00:00
|
|
|
|
2016-03-24 09:01:43 +00:00
|
|
|
r := gin.New()
|
|
|
|
|
|
|
|
// Global middleware
|
|
|
|
r.Use(gin.Logger())
|
|
|
|
r.Use(gin.Recovery())
|
2016-03-25 01:23:46 +00:00
|
|
|
r.Use(VersionMiddleware())
|
2016-04-07 06:16:59 +00:00
|
|
|
r.Use(LogMiddleware())
|
2016-06-26 04:21:32 +00:00
|
|
|
r.Use(StatMiddleware())
|
2016-03-24 09:01:43 +00:00
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
r.GET(PushConf.API.StatGoURI, api.StatusHandler)
|
2016-04-15 02:24:39 +00:00
|
|
|
r.GET(PushConf.API.StatAppURI, appStatusHandler)
|
2016-04-15 07:05:23 +00:00
|
|
|
r.GET(PushConf.API.ConfigURI, configHandler)
|
2016-06-26 04:21:32 +00:00
|
|
|
r.GET(PushConf.API.SysStatURI, sysStatsHandler)
|
2016-04-13 06:59:28 +00:00
|
|
|
r.POST(PushConf.API.PushURI, pushHandler)
|
2017-01-19 08:56:30 +00:00
|
|
|
r.GET(PushConf.API.MetricURI, metricsHandler)
|
2016-03-24 09:01:43 +00:00
|
|
|
r.GET("/", rootHandler)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|