func run() { logFile, logErr := os.OpenFile(*logFileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666) if logErr != nil { fmt.Println("Fail to find", *logFile, "cServer start Failed") os.Exit(1) } log.SetOutput(logFile) log.Printf("CM Server Start") CACHE = NewCache(PATH + "cache") defer CACHE.Close() CACHE.All() gin.SetMode(gin.ReleaseMode) //g := gin.Default() g := gin.New() g.Use(gin.Recovery()) g.Use(gin.LoggerWithWriter(logFile)) g.Use(gzip.Gzip(gzip.DefaultCompression)) g.POST("/cm/settings", postSettings) g.GET("/cm/settings", getSettings) g.GET("/cm/menus", func(c *gin.Context) { c.JSON(http.StatusOK, CACHE.menus) }) g.NoRoute(func(c *gin.Context) { c.String(http.StatusNotFound, "404") }) g.Run(":8011") }
func main() { r := gin.New() r.Use(gin.LoggerWithWriter(log.Writer())) r.Use(gin.RecoveryWithWriter(log.Writer())) tmpl, err := template.New("").Funcs(tmpl.TemplateFuncs).ParseGlob("templates/*") if err != nil { panic(err) } r.SetHTMLTemplate(tmpl) r.Static(path.Static("/"), "static") r.Static(path.Css("/"), "assets/css") r.Static(path.Js("/"), "assets/js") r.Static(path.Font("/"), "assets/fonts") r.NoRoute(ctl.Render404) r.GET(path.Root, posts.Index) r.GET(path.Posts, posts.Index) r.GET(path.NewPost, posts.New) r.POST(path.NewPost, posts.Create) r.GET(path.Post(":id"), posts.Show) r.GET(path.EditPost(":id"), posts.Edit) r.POST(path.EditPost(":id"), posts.Update) r.POST(path.DeletePost(":id"), posts.Destroy) r.GET(path.Register, users.New) r.POST(path.Register, users.Create) r.GET(path.Login, users.SessionNew) r.POST(path.Login, users.SessionCreate) r.GET(path.Logout, users.SessionDestroy) r.Run(":8080") }
func main() { r := gin.New() r.Use(gin.Recovery(), gin.LoggerWithWriter(setting.LogIO)) r.Use(func(c *gin.Context) { // Run this on all requests // Should be moved to a proper middleware c.Writer.Header().Set("Access-Control-Allow-Origin", "*") c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization") c.Next() }) gin.SetMode(setting.GinMode) r.OPTIONS("/*cors", func(c *gin.Context) { // Empty 200 response }) user := r.Group("/user") { user.POST("/token", handlers.Token) user.POST("/register", handlers.Register) } authorized := r.Group("/api", middlewares.Auth(handlers.MySigningKey)) authorized.POST("/balance", handlers.Balance) r.Run(setting.AppCfg.HttpPort) }
func ginTestEngine() (*gin.Engine, *bytes.Buffer) { gin.SetMode(gin.TestMode) router := gin.New() logOutput := new(bytes.Buffer) router.Use(gin.LoggerWithWriter(logOutput)) router.Use(gin.Recovery()) return router, logOutput }
func main() { defer func() { if err := recover(); err != nil { util.Logln(fmt.Sprintf("recover:%v", err)) } }() gin.SetMode(gin.ReleaseMode) router := gin.New() router.Use(util.Counter(), gin.LoggerWithWriter(util.GetLogBuf()), gin.Recovery()) router.GET("/*path", imageHandler) router.Run(":" + util.ServePort) }
/*********************gin start*************************/ func openGin(profile string) *gin.Engine { if profile == Product { gin.SetMode(gin.ReleaseMode) } server := gin.Default() // server.Use(static.ServeRoot("/", "./static")) ginLog, err := os.Create("logs/server.log") if err != nil { fmt.Println("err:", err) } server.Use(gin.LoggerWithWriter(ginLog)) return server }
func main() { initEnv() utils.InitLog() models.InitDB() models.InitCron() router := endpoint.GetMainEngine() router.Use(gin.LoggerWithWriter(utils.LogWriter)) router.Use(gin.Recovery()) port := os.Getenv("PORT") s := &http.Server{ Addr: ":" + port, Handler: router, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, } s.ListenAndServe() }
func LoggerWithWriterMiddleware(out io.Writer) Middleware { return Middleware{Func: gin.LoggerWithWriter(out)} }
func main() { gin.SetMode(gin.ReleaseMode) router := gin.Default() //写入日志 os.Mkdir("push_log", os.ModeDir) os.Chdir("push_log") /*go func() { r := rand.New(rand.NewSource(time.Now().UnixNano())) timeLog := time.NewTicker(60 * time.Second) for { select { case <-timeLog.C: yourfile, _ := os.Create("log_" + strconv.Itoa(r.Intn(100)) + ".log") router.Use(gin.LoggerWithWriter(yourfile)) } } }()*/ yourfile, _ := os.Create("server" + time.Now().Format("2006-01-02") + ".log") router.Use(gin.LoggerWithWriter(yourfile)) //默认生成jpush的推送对象 //jpush := NewJpush("量子股或有100个涨停", 0, "all", make(map[string]interface{})) //默认路由 router.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "欢迎来到量子股推送") }) //推送路由群组 push := router.Group("/push") { //推送设备 push.GET("/device", func(c *gin.Context) { //推送的标题 title requestTitle := c.Query("title") if requestTitle == "" { c.JSON(http.StatusOK, ReturnMessage{0, "推送标题title不能为空", "/"}) return } title := ReturnTitle(requestTitle) //推送对象 audience 格式是num_value 0_all 1_shuye,ben requestAudience := c.Query("audience") if requestAudience == "" { c.JSON(http.StatusOK, ReturnMessage{0, "推送对象audience不能为空", "/"}) return } err, sTyle, result := ReturnAudience(requestAudience) if err != nil { c.JSON(http.StatusOK, ReturnMessage{0, err.Error(), "/"}) return } /*=====推送对象的类型 0是全部对象,1是别名,2是注册id=====*/ style := sTyle audience := result //推送额外字段 extras url_value url,text_www.baidu.com,go requestExtras := c.Query("extras") err, result = ReturnExtras(requestExtras) if err != nil { c.JSON(http.StatusOK, ReturnMessage{0, err.Error(), "/"}) return } extras := result.(map[string]interface{}) /*=============发送推送===========*/ jpush := Jpush{title, style, audience, extras} err, info := jpush.PushDevice() if err != nil { //发生未知错误,如网络连接失败等, c.JSON(http.StatusOK, ReturnMessage{0, err, "/"}) } else { if info.StatusCode == 200 { //推送成功 c.JSON(http.StatusOK, ReturnMessage{1, HttpStatusCode[info.StatusCode], "/"}) } else { //推送失败 c.JSON(http.StatusOK, ReturnMessage{0, HttpStatusCode[info.StatusCode], "/"}) } } }) } router.NoRoute(func(c *gin.Context) { c.JSON(http.StatusOK, ReturnMessage{0, "请访问正确的路由", "/"}) }) /*8080端口*/ router.Run(":8080") // listen and serve on 0.0.0.0:8080 }