func init() { numCpu := runtime.NumCPU() runtime.GOMAXPROCS(numCpu) // until "go 1.5" logger.Infof("GOMAXPROCS set to %d.", numCpu) if envPort := os.Getenv("PORT"); 0 < len(envPort) { listenPort = fmt.Sprintf(":%s", envPort) logger.Infof("port set to %s.", listenPort) } }
func startHttpServer() { router := gin.New() // don't need to use Logger, if we use apache/nginx. router.Use(gin.Recovery()) routes.Use(router) if gin.Mode() == gin.ReleaseMode { logger.Infof("GIN Listening and serving HTTP on %s", listenPort) endless.ListenAndServe(listenPort, router) // use endless for graceful restart/stop. } else { router.Run(listenPort) } }
func Show(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { } var task models.Task db.DB.First(&task, id) logger.Infof("id=%d task found.", id) data := map[string]interface{}{"task": task} c.JSON(http.StatusOK, gin.H{"data": data}) }
func Show(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { } var owner models.Owner db.DB.First(&owner) logger.Infof("id=%d owner found.", id) data := map[string]interface{}{"owner": owner} c.JSON(http.StatusOK, gin.H{"data": data}) }
func Show(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { } var pet models.Pet db.DB.First(&pet, id) logger.Infof("id=%d pet found.", id) data := map[string]interface{}{"pet": pet} c.JSON(http.StatusOK, gin.H{"data": data}) }
func Index(c *gin.Context) { page, _ := strconv.Atoi(c.Query("page")) if page < 1 { page = 1 } perpage, _ := config.Int("app.perpage") offset := (page - 1) * perpage if page < 2 { offset = 0 } var count int var owners []models.Owner db.DB.Find(&owners).Count(&count) db.DB.Limit(perpage).Offset(offset).Find(&owners) logger.Infof("%d owners found.", len(owners)) data := map[string]interface{}{"owners": owners} c.JSON(http.StatusOK, gin.H{"data": data, "page": page, "perpage": perpage, "total": count}) }
func main() { appName, _ := config.String("app.name") logger.Infof("starting %s", appName) startHttpServer() }