Exemplo n.º 1
0
func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())
	models.InitDB()

	r := gin.Default()
	r.Static("/static", "./static")
	r.StaticFile("/favicon.ico", "./static/favicon.ico")
	r.LoadHTMLGlob("templates/*")
	// r.LoadHTMLGlob("templates/**/*")

	r.Use(middleware.Current())

	authorized := r.Group("/")
	authorized.Use(middleware.AuthRequired())
	{
		authorized.POST("/posts/*slug", controllers.NewPostAndEditHandler)
		authorized.DELETE("/posts/:slug", controllers.DeleteHandler)
	}
	r.GET("/posts", controllers.GetAllHandler)
	r.GET("/posts/:slug", controllers.GetOneHandler)

	r.GET("/", controllers.HomeHandler)
	r.GET("/compose", controllers.ComposeHandler)
	r.GET("/aside", controllers.AsideHandler)

	r.GET("/page/:num", controllers.PageHandler)

	r.GET("/login", controllers.LoginHandler)
	r.POST("/login", controllers.LoginPostHandler)
	r.GET("/logout", controllers.LogoutHandler)

	r.GET("/about", controllers.AboutHandler)

	r.GET("/test/*id", controllers.TestHandler)

	// r.PUT("/posts/:id", ...)
	// r.DELETE("/posts/:id", ...)
	//r.GET("*",models.PageNotFoundHandler)

	// GET /zoos:列出所有动物园
	// POST /zoos:新建一个动物园
	// GET /zoos/ID:获取某个指定动物园的信息
	// PUT /zoos/ID:更新某个指定动物园的信息(提供该动物园的全部信息)
	// PATCH /zoos/ID:更新某个指定动物园的信息(提供该动物园的部分信息)
	// DELETE /zoos/ID:删除某个动物园
	// GET /zoos/ID/animals:列出某个指定动物园的所有动物
	// DELETE /zoos/ID/animals/ID:删除某个指定动物园的指定动物

	// ?limit=10:指定返回记录的数量
	// ?offset=10:指定返回记录的开始位置。
	// ?page=2&per_page=100:指定第几页,以及每页的记录数。
	// ?sortby=name&order=asc:指定返回结果按照哪个属性排序,以及排序顺序。
	// ?animal_type_id=1:指定筛选条件

	gin.SetMode(gin.DebugMode)
	r.Run(":8000")
}
Exemplo n.º 2
0
func main() {
	models.InitDB()
	r := gin.Default()
	r.Static("/static", "./static")
	r.StaticFile("/favicon.ico", "./static/favicon.ico")
	r.LoadHTMLGlob("templates/*")

	// r.Use(middleware.Mytime())

	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "welcome home",
		})
	})

	r.GET("/user/:name", func(c *gin.Context) {
		name := c.Param("name")
		c.String(200, "hello %s", name)

	})

	// curl  --form user=u1 --form password=p1 localhost:8000/login
	var form LoginForm
	r.POST("/login", func(c *gin.Context) {
		if c.Bind(&form) == nil {
			if form.User == "u1" && form.Password == "p1" {
				c.JSON(200, gin.H{"status": "success to login"})
			} else {
				c.JSON(401, gin.H{"status": "unauthorized"})
			}
		}
	})

	r.GET("/moreJSON", func(c *gin.Context) {
		// You also can use a struct
		var msg struct {
			Name    string `json:"user"`
			Message string
			Number  int
		}
		msg.Name = "Lena"
		msg.Message = "hey"
		msg.Number = 123
		// Note that msg.Name becomes "user" in the JSON
		// Will output  :   {"user": "******", "Message": "hey", "Number": 123}
		c.JSON(http.StatusOK, msg)
	})

	r.GET("/someXML", func(c *gin.Context) {
		c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
	})

	r.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", gin.H{
			"title": "main website",
		})
	})

	r.GET("/redic", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently, "http://www.baidu.com")
	})

	// Group using gin.BasicAuth() middleware
	// gin.Accounts is a shortcut for map[string]string
	// authorized := r.Group("/admin", gin.BasicAuth(gin.Accounts{
	// 	"foo":    "bar",
	// 	"austin": "1234",
	// 	"lena":   "hello2",
	// 	"manu":   "4321",
	// }))

	// // /admin/secrets endpoint
	// // hit "localhost:8080/admin/secrets
	// authorized.GET("/secrets", func(c *gin.Context) {
	// 	// get user, it was set by the BasicAuth middleware
	// 	user := c.MustGet(gin.AuthUserKey).(string)
	// 	if secret, ok := secrets[user]; ok {
	// 		c.JSON(http.StatusOK, gin.H{"user": user, "secret": secret})
	// 	} else {
	// 		c.JSON(http.StatusOK, gin.H{"user": user, "secret": "NO SECRET :("})
	// 	}
	// })

	gin.SetMode(gin.DebugMode)
	r.Run(":8000")
}