Skip to content

Commit

Permalink
feat: 添加前端 api.
Browse files Browse the repository at this point in the history
  • Loading branch information
falling-ts committed Mar 3, 2023
1 parent 8f2e192 commit 431ed66
Show file tree
Hide file tree
Showing 42 changed files with 1,555 additions and 203 deletions.
56 changes: 56 additions & 0 deletions app/http/controllers/auth_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package controllers

import (
"gower/app/services/route"
"net/http"
)

type AuthController struct {
Controllers
}

var Auth = new(AuthController)

// RegisterForm get register form
func (a *AuthController) RegisterForm(c route.Context) {
c.HTML(http.StatusOK, "auth/register", data{
"Title": "register",
})
}

// Register exec register
func (a *AuthController) Register(c route.Context) {
c.JSON(http.StatusOK, data{
"code": 0,
"msg": "SUCCESS",
"data": nil,
})
}

// LoginForm get login form
func (a *AuthController) LoginForm(c route.Context) {
c.HTML(http.StatusOK, "auth/login", data{
"Title": "login",
})
}

// Login exec login
func (a *AuthController) Login(c route.Context) {
c.HTML(http.StatusOK, "auth/login", data{
"Title": "login",
})
}

// Me get personal center
func (a *AuthController) Me(c route.Context) {
c.HTML(http.StatusOK, "auth/me", data{
"Title": "me",
})
}

// Logout exec logout
func (a *AuthController) Logout(c route.Context) {
c.HTML(http.StatusOK, "auth/login", data{
"Title": "login",
})
}
2 changes: 2 additions & 0 deletions app/http/controllers/controllers.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package controllers

type Controllers struct{}

type data map[string]any
8 changes: 4 additions & 4 deletions app/http/controllers/home_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type HomeController struct {
var Home = new(HomeController)

func (h *HomeController) Index(c route.Context) {
c.HTML(http.StatusOK, "home/index", map[string]any{
"title": "Main website",
c.HTML(http.StatusOK, "home/index", data{
"Title": "Main website",
})
}

func (h *HomeController) Test(c route.Context) {
c.HTML(http.StatusOK, "home/test", map[string]any{
"title": "Main website",
c.HTML(http.StatusOK, "home/test", data{
"Title": "Main website",
})
}
15 changes: 0 additions & 15 deletions app/http/controllers/pong_controller.go

This file was deleted.

149 changes: 112 additions & 37 deletions app/services/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,60 +27,135 @@ func New() *Route {
return route
}

func (r *Route) Use(middleware ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.Use(middleware...)
// Use adds middleware to the group, see example code in GitHub.
func (r *Route) Use(middleware ...HandlerFunc) IRoutes {
r.Engine.Use(toGinHandlers(middleware)...)
return r
}

func (r *Route) Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.Handle(httpMethod, relativePath, handlers...)
// Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix.
// For example, all the routes that use a common middleware for authorization could be grouped.
func (r *Route) Group(relativePath string, handlers ...HandlerFunc) *Route {
group := r.Engine.Group(relativePath, toGinHandlers(handlers)...)
r.Engine.RouterGroup = *group
return r
}
func (r *Route) Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.Any(relativePath, handlers...)

// Handle registers a new request handle and middleware with the given path and method.
// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
// See the example code in GitHub.
//
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
// functions can be used.
//
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (r *Route) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.Handle(httpMethod, relativePath, toGinHandlers(handlers)...)
return r
}
func (r *Route) GET(relativePath string, handlers ...HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.GET(relativePath, toGinHandlerFunc(handlers)...)

// Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (r *Route) Any(relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.Any(relativePath, toGinHandlers(handlers)...)
return r
}
func (r *Route) POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.POST(relativePath, handlers...)

// GET is a shortcut for route.Handle("GET", path, handlers).
func (r *Route) GET(relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.GET(relativePath, toGinHandlers(handlers)...)
return r
}
func (r *Route) DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.DELETE(relativePath, handlers...)

// POST is a shortcut for route.Handle("POST", path, handlers).
func (r *Route) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.POST(relativePath, toGinHandlers(handlers)...)
return r
}
func (r *Route) PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.PATCH(relativePath, handlers...)

// DELETE is a shortcut for route.Handle("DELETE", path, handlers).
func (r *Route) DELETE(relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.DELETE(relativePath, toGinHandlers(handlers)...)
return r
}
func (r *Route) PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.PUT(relativePath, handlers...)

// PATCH is a shortcut for route.Handle("PATCH", path, handlers).
func (r *Route) PATCH(relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.PATCH(relativePath, toGinHandlers(handlers)...)
return r
}
func (r *Route) OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.OPTIONS(relativePath, handlers...)

// PUT is a shortcut for route.Handle("PUT", path, handlers).
func (r *Route) PUT(relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.PUT(relativePath, toGinHandlers(handlers)...)
return r
}
func (r *Route) HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.HEAD(relativePath, handlers...)

// OPTIONS is a shortcut for route.Handle("OPTIONS", path, handlers).
func (r *Route) OPTIONS(relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.OPTIONS(relativePath, toGinHandlers(handlers)...)
return r
}
func (r *Route) Match(methods []string, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return r.Engine.RouterGroup.Match(methods, relativePath, handlers...)

// HEAD is a shortcut for route.Handle("HEAD", path, handlers).
func (r *Route) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.HEAD(relativePath, toGinHandlers(handlers)...)
return r
}

func (r *Route) StaticFile(relativePath, filepath string) gin.IRoutes {
return r.Engine.RouterGroup.StaticFile(relativePath, filepath)
// Match registers a route that matches the specified methods that you declared.
func (r *Route) Match(methods []string, relativePath string, handlers ...HandlerFunc) IRoutes {
r.Engine.Match(methods, relativePath, toGinHandlers(handlers)...)
return r
}
func (r *Route) StaticFileFS(relativePath, filepath string, fs http.FileSystem) gin.IRoutes {
return r.Engine.RouterGroup.StaticFileFS(relativePath, filepath, fs)

// StaticFile registers a single route in order to serve a single file of the local filesystem.
// router.StaticFile("favicon.ico", "./resources/favicon.ico")
func (r *Route) StaticFile(relativePath, filepath string) IRoutes {
r.Engine.StaticFile(relativePath, filepath)
return r
}
func (r *Route) Static(relativePath, root string) gin.IRoutes {
return r.Engine.RouterGroup.Static(relativePath, root)

// StaticFileFS works just like `StaticFile` but a custom `http.FileSystem` can be used instead..
// router.StaticFileFS("favicon.ico", "./resources/favicon.ico", Dir{".", false})
// Gin by default uses: gin.Dir()
func (r *Route) StaticFileFS(relativePath, filepath string, fs http.FileSystem) IRoutes {
r.Engine.StaticFileFS(relativePath, filepath, fs)
return r
}
func (r *Route) StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {
return r.Engine.RouterGroup.StaticFS(relativePath, fs)

// Static serves files from the given file system root.
// Internally a http.FileServer is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
// To use the operating system's file system implementation,
// use :
//
// router.Static("/static", "/var/www")
func (r *Route) Static(relativePath, root string) IRoutes {
r.Engine.Static(relativePath, root)
return r
}

func toGinHandlerFunc(hfs []HandlerFunc) []gin.HandlerFunc {
ret := make([]gin.HandlerFunc, len(hfs))
for i, hf := range hfs {
ret[i] = func(c *gin.Context) {
hf(c)
}
// StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead.
// Gin by default uses: gin.Dir()
func (r *Route) StaticFS(relativePath string, fs http.FileSystem) IRoutes {
r.Engine.StaticFS(relativePath, fs)
return r
}

func toGinHandlers(handlers HandlersChain) gin.HandlersChain {
ginHandlers := make(gin.HandlersChain, len(handlers))
for i, handler := range handlers {
ginHandlers[i] = toGinHandler(handler)
}

return ginHandlers
}

func toGinHandler(handler HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
handler(c)
}
return ret
}
29 changes: 27 additions & 2 deletions app/services/route/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,39 @@ type Service interface {
Run()
}

type ErrorType uint64

// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(Context)

// HandlersChain defines a HandlerFunc slice.
type HandlersChain []HandlerFunc

// IRouter defines all router handle interface includes single and group router.
type IRouter interface {
IRoutes
Group(string, ...HandlerFunc) *Route
}

// IRoutes defines all router handle interface.
type IRoutes interface {
Use(...HandlerFunc) IRoutes

Handle(string, string, ...HandlerFunc) IRoutes
Any(string, ...HandlerFunc) IRoutes
GET(string, ...HandlerFunc) IRoutes
POST(string, ...HandlerFunc) IRoutes
DELETE(string, ...HandlerFunc) IRoutes
PATCH(string, ...HandlerFunc) IRoutes
PUT(string, ...HandlerFunc) IRoutes
OPTIONS(string, ...HandlerFunc) IRoutes
HEAD(string, ...HandlerFunc) IRoutes
Match([]string, string, ...HandlerFunc) IRoutes

StaticFile(string, string) IRoutes
StaticFileFS(string, string, http.FileSystem) IRoutes
Static(string, string) IRoutes
StaticFS(string, http.FileSystem) IRoutes
}

// Context http request and response.
type Context interface {
Copy() *gin.Context
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/falling-ts/gower v0.0.0-20230228171936-2e53100c86ab h1:V9SjV6I/nbve0nyLdgBw7A7LH1W+nIF1l4p5C/wECtM=
github.com/falling-ts/gower v0.0.0-20230228171936-2e53100c86ab/go.mod h1:8NYKUZFLzMHf1snh5CCL9iFBegcYmLYH+DrbVn7p6V8=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.0 h1:OjyFBKICoexlu99ctXNR2gg+c5pKrKMuyjgARg9qeY8=
Expand Down
1 change: 1 addition & 0 deletions logo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"v1.0.0","components":{"icon":{"name":"icon","text":"","color":"#7be228","fontFamily":"\"Font Awesome 6 Brands\"","fontWeight":"400","fontSize":75,"letterSpacing":0},"main":{"name":"main","text":"o","color":"#2e96e5","fontFamily":"Faster One","fontWeight":"bold","fontSize":85,"letterSpacing":0},"accent":{"name":"accent","text":"wer","color":"#e57d2e","fontFamily":"Lobster","fontWeight":"bold","fontSize":100,"letterSpacing":0}},"global":{"offset":{"size":5,"color":"#844343"},"layout":"HORIZONTAL","shapes":false}}
26 changes: 23 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
{
"name": "gower",
"type": "module",
"version": "0.0.1",
"description": "",
"main": "resources/js/main.js",
"files": [
"public/static"
],
"module": "public/static/main.js",
"main": "public/static/main.umd.js",
"exports": {
".": {
"import": "./dist/main.js",
"require": "./dist/main.umd.cjs"
}
},
"scripts": {
"b": "pnpm vite build",
"build": "pnpm vite build",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "falling-ts",
"license": "ISC",
"devDependencies": {
"@rollup/plugin-replace": "^5.0.2",
"@vitejs/plugin-vue": "^4.0.0",
"animate.css": "^4.1.1",
"autoprefixer": "^10.4.13",
"daisyui": "^2.51.2",
"jquery": "^3.6.3",
"postcss": "^8.4.21",
"resize-observer-polyfill": "^1.5.1",
"simplebar": "^6.2.1",
"stylus": "^0.59.0",
"tailwindcss": "^3.2.7",
"vite": "^4.1.4"
"vite": "^4.1.4",
"vue": "^3.2.47"
}
}
Loading

0 comments on commit 431ed66

Please sign in to comment.