feat: add new router and ping route to main application
- Remove the import statement for "github.com/gin-gonic/gin" in main.go - Add a new file called router.go in the router directory - Create a new function called New() in router.go that returns a *gin.Engine - Add a GET route for "/ping" in the New() function in router.go Signed-off-by: appleboy <appleboy.tw@gmail.com>
This commit is contained in:
parent
fe12037cbb
commit
1ab24b1efd
11
main.go
11
main.go
|
@ -1,18 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"example/router"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := router.New()
|
||||||
r.GET("/ping", func(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"message": "pong",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
// listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
|
// listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
|
||||||
if err := r.Run(); err != nil {
|
if err := r.Run(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
package router
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func New() *gin.Engine {
|
||||||
|
r := gin.Default()
|
||||||
|
r.GET("/ping", func(c *gin.Context) {
|
||||||
|
c.String(200, "pong")
|
||||||
|
})
|
||||||
|
return r
|
||||||
|
}
|
Loading…
Reference in New Issue