Skip to content

Commit

Permalink
feat: 新增router和controller
Browse files Browse the repository at this point in the history
  • Loading branch information
minibear2333 committed Jun 12, 2021
1 parent 9c23f23 commit 9fc6162
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
15 changes: 15 additions & 0 deletions internal/routers/api/v1/article.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v1

import "github.com/gin-gonic/gin"

type Article struct{}

func NewArticle() Article {
return Article{}
}

func (a Article) Get(c *gin.Context) {}
func (a Article) List(c *gin.Context) {}
func (a Article) Create(c *gin.Context) {}
func (a Article) Update(c *gin.Context) {}
func (a Article) Delete(c *gin.Context) {}
15 changes: 15 additions & 0 deletions internal/routers/api/v1/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v1

import "github.com/gin-gonic/gin"

type Tag struct{}

func NewTag() Tag {
return Tag{}
}

func (t Tag) Get(c *gin.Context) {}
func (t Tag) List(c *gin.Context) {}
func (t Tag) Create(c *gin.Context) {}
func (t Tag) Update(c *gin.Context) {}
func (t Tag) Delete(c *gin.Context) {}
32 changes: 32 additions & 0 deletions internal/routers/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package routers

import (
"github.com/gin-gonic/gin"
v1 "github.com/golang-minibear2333/gin-blog/internal/routers/api/v1"
)

func NewRouter() *gin.Engine {
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())

article := v1.NewArticle()
tag := v1.NewTag()
apiv1 := r.Group("/api/v1")
{
apiv1.POST("/tags", tag.Create)
apiv1.DELETE("/tags/:id", tag.Delete)
apiv1.PUT("/tags/:id", tag.Update)
apiv1.PATCH("/tags/:id/state", tag.Update)
apiv1.GET("/tags", tag.List)

apiv1.POST("/articles", article.Create)
apiv1.DELETE("/articles/:id", article.Delete)
apiv1.PUT("/articles/:id", article.Update)
apiv1.PATCH("/articles/:id/state", article.Update)
apiv1.GET("/articles/:id", article.Get)
apiv1.GET("/articles", article.List)
}

return r
}
20 changes: 19 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
package main

import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
"github.com/golang-minibear2333/gin-blog/internal/routers"
"net/http"
"time"
)

func main() {
router := routers.NewRouter()
s := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}

// 仅仅是hello测试
func helloWorld() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
Expand Down

0 comments on commit 9fc6162

Please sign in to comment.