1
This commit is contained in:
1
server/.gitignore
vendored
Normal file
1
server/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
dist/
|
91
server/handlers_domains.go
Normal file
91
server/handlers_domains.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reCoreD-UI/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Succeed bool `json:"succeed"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func errorHandler(c *gin.Context, err error) {
|
||||
c.JSON(http.StatusInternalServerError, Response{
|
||||
Succeed: false,
|
||||
Message: err.Error(),
|
||||
Data: nil,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) getDomains(c *gin.Context) {
|
||||
domains, err := s.controller.GetDomains("")
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
errorHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Succeed: true,
|
||||
Data: domains,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) createDomain(c *gin.Context) {
|
||||
domain := &models.Domain{}
|
||||
|
||||
if err := c.BindJSON(domain); err != nil {
|
||||
c.JSON(http.StatusBadRequest, Response{
|
||||
Succeed: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.controller.CreateDomain(domain); err != nil {
|
||||
errorHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, Response{
|
||||
Succeed: true,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) updateDomain(c *gin.Context) {
|
||||
domain := &models.Domain{}
|
||||
|
||||
if err := c.BindJSON(domain); err != nil {
|
||||
c.JSON(http.StatusBadRequest, Response{
|
||||
Succeed: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.controller.UpdateDomain(domain); err != nil {
|
||||
errorHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Succeed: true,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) deleteDomain(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := s.controller.DeleteDomain(id); err != nil {
|
||||
errorHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNoContent, Response{
|
||||
Succeed: true,
|
||||
})
|
||||
}
|
2
server/handlers_records.go
Normal file
2
server/handlers_records.go
Normal file
@@ -0,0 +1,2 @@
|
||||
package server
|
||||
|
45
server/route.go
Normal file
45
server/route.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (s *Server) setupRoute() {
|
||||
username, password, err := s.controller.GetAdmin()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
server := s.webServer.Group(s.prefix, gin.BasicAuth(gin.Accounts{
|
||||
username: password,
|
||||
}))
|
||||
|
||||
apiHandler := gin.New()
|
||||
groupV1 := apiHandler.Group("/api/v1")
|
||||
|
||||
domains := groupV1.Group("/domains")
|
||||
domains.
|
||||
GET("/", s.getDomains).
|
||||
POST("/", s.createDomain).
|
||||
PUT("/", s.updateDomain).
|
||||
DELETE("/:id", s.deleteDomain)
|
||||
|
||||
records := groupV1.Group("/records")
|
||||
records.
|
||||
GET("/")
|
||||
|
||||
server.Use(func(ctx *gin.Context) {
|
||||
uri := ctx.Request.RequestURI
|
||||
logrus.Debug(uri)
|
||||
|
||||
if strings.HasPrefix(uri, path.Join(s.prefix, uri)) {
|
||||
apiHandler.HandleContext(ctx)
|
||||
} else {
|
||||
// TODO: Static Page
|
||||
}
|
||||
})
|
||||
}
|
43
server/server.go
Normal file
43
server/server.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reCoreD-UI/controllers"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
controller *controllers.Controller
|
||||
webServer *gin.Engine
|
||||
listen string
|
||||
prefix string
|
||||
}
|
||||
|
||||
func NewServer(c *cli.Context) (*Server, error) {
|
||||
controller, err := controllers.NewController(c.String("mysql-dsn"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Server{
|
||||
controller: controller,
|
||||
webServer: gin.New(),
|
||||
listen: net.JoinHostPort(
|
||||
c.String("listen"),
|
||||
c.String("port"),
|
||||
),
|
||||
prefix: c.String("prefix"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) Run() error {
|
||||
logrus.Debug("server running")
|
||||
defer logrus.Debug("server exit")
|
||||
|
||||
s.setupRoute()
|
||||
|
||||
return s.webServer.Run(s.listen)
|
||||
}
|
Reference in New Issue
Block a user