Skip to content

Commit

Permalink
Merge pull request #1 from dingdinglz/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dingdinglz authored Nov 27, 2024
2 parents 3f752d5 + fb66676 commit f436c77
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web/
10 changes: 10 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dingadmin

import "github.com/gofiber/fiber/v2"

func bindRouters(app *App) {
MainRoute := app.Server.Group(app.config.Prefix)
MainRoute.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", app.GetRenderMap(), "layout")
})
}
21 changes: 21 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dingadmin

type Config struct {
Port int
Prefix string
Name string
Title string
Author string
AuthorLink string
Theme string
}

var DefaultConfig Config = Config{
Port: 8080,
Prefix: "/admin",
Name: "dingadmin",
Title: "dingadmin",
Author: "dinglz",
AuthorLink: "https://github.com/dingdinglz",
Theme: "./web/admin/",
}
50 changes: 50 additions & 0 deletions ding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dingadmin

import (
"strconv"

"github.com/dingdinglz/dingadmin/tool"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/template/html/v2"
)

type App struct {
Server *fiber.App
config Config
dingMenus []Menu
dingMenuGroups []MenuGroup
}

func New(configs ...Config) *App {
app := &App{}
if len(configs) > 0 {
app.config = configs[0]
} else {
app.config = DefaultConfig
}
engine := html.New(app.config.Theme, ".html")
engine.ShouldReload = true
app.Server = fiber.New(fiber.Config{Views: engine})
return app
}

func NewWithFiberConfig(serverConfig fiber.Config, configs ...Config) *App {
app := &App{}
if len(configs) > 0 {
app.config = configs[0]
} else {
app.config = DefaultConfig
}
engine := html.New(app.config.Theme, ".html")
serverConfig.Views = engine
app.Server = fiber.New(serverConfig)
return app
}

func (app *App) Serve() error {
bindRouters(app)
app.Server.Use(logger.New(), recover.New())
return app.Server.Listen(tool.StringBuilder("0.0.0.0:", strconv.Itoa(app.config.Port)))
}
12 changes: 12 additions & 0 deletions ding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dingadmin

import "testing"

func TestDingAdmin(t *testing.T) {
cfg := DefaultConfig
cfg.Theme = "./dingadmin-layui/"
app := New(cfg)
app.AddMenu(Menu{Name: "test", Text: "测试菜单", Href: "/test"})
app.AddMenuGroup(MenuGroup{Text: "测试菜单组", Menus: []Menu{{Name: "test2", Text: "测试菜单2", Href: "/test2"}}})
app.Serve()
}
1 change: 1 addition & 0 deletions dingadmin-layui
Submodule dingadmin-layui added at 52d9b7
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module github.com/dingdinglz/dingadmin

go 1.22.4

require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/gofiber/template/html/v2 v2.1.2
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/gofiber/template v1.8.3 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.15.0 // indirect
)
41 changes: 41 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
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/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/gofiber/template v1.8.3 h1:hzHdvMwMo/T2kouz2pPCA0zGiLCeMnoGsQZBTSYgZxc=
github.com/gofiber/template v1.8.3/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8=
github.com/gofiber/template/html/v2 v2.1.2 h1:wkK/mYJ3nIhongTkG3t0QgV4ADdgOYJYVSAF2AHnh8Y=
github.com/gofiber/template/html/v2 v2.1.2/go.mod h1:E98Z/FzvpaSib06aWEgYk6GXNf3ctoyaJH8yW5ay5ak=
github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM=
github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 20 additions & 0 deletions menu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dingadmin

type Menu struct {
Name string
Text string
Href string
}

type MenuGroup struct {
Menus []Menu
Text string
}

func (app *App) AddMenu(m Menu) {
app.dingMenus = append(app.dingMenus, m)
}

func (app *App) AddMenuGroup(m MenuGroup) {
app.dingMenuGroups = append(app.dingMenuGroups, m)
}
7 changes: 7 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dingadmin

import "github.com/gofiber/fiber/v2"

func (app *App) GetRenderMap() fiber.Map {
return fiber.Map{"DingAdminConfig": app.config, "DingAdminMenus": app.dingMenus, "DingAdminMenuGroups": app.dingMenuGroups}
}
11 changes: 11 additions & 0 deletions tool/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tool

import "strings"

func StringBuilder(s ...string) string {
var i strings.Builder
for _, cnt := range s {
i.WriteString(cnt)
}
return i.String()
}

0 comments on commit f436c77

Please sign in to comment.