Skip to content

Commit

Permalink
feat: add static file service
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Dec 25, 2023
1 parent 716b184 commit 0ff50b9
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 24 deletions.
2 changes: 1 addition & 1 deletion adapter/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
"github.com/gin-gonic/gin"
)

func Register(app *gin.Engine) {
func Register(app *gin.RouterGroup) {
app.POST("/mj/notify", midjourney.NotifyAPI)
}
2 changes: 1 addition & 1 deletion addition/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gin-gonic/gin"
)

func Register(app *gin.Engine) {
func Register(app *gin.RouterGroup) {
{
app.POST("/card", card.HandlerAPI)

Expand Down
2 changes: 1 addition & 1 deletion admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/gin-gonic/gin"
)

func Register(app *gin.Engine) {
func Register(app *gin.RouterGroup) {
channel.Register(app)

app.GET("/admin/analytics/info", InfoAPI)
Expand Down
2 changes: 1 addition & 1 deletion auth/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth

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

func Register(app *gin.Engine) {
func Register(app *gin.RouterGroup) {
app.POST("/verify", VerifyAPI)
app.POST("/reset", ResetAPI)
app.POST("/register", RegisterAPI)
Expand Down
2 changes: 1 addition & 1 deletion channel/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package channel

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

func Register(app *gin.Engine) {
func Register(app *gin.RouterGroup) {
app.GET("/admin/channel/list", GetChannelList)
app.POST("/admin/channel/create", CreateChannel)
app.GET("/admin/channel/get/:id", GetChannel)
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ services:
REDIS_PORT: 6379
REDIS_PASSWORD: ""
REDIS_DB: 0
DEBUG: false
SERVE_STATIC: true
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/bincooo/claude-api v1.0.2
github.com/chai2010/webp v1.1.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-contrib/static v0.0.1
github.com/gin-gonic/gin v1.9.1
github.com/go-redis/redis/v8 v8.11.5
github.com/go-sql-driver/mysql v1.7.1
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ github.com/gaukas/godicttls v0.0.3/go.mod h1:l6EenT4TLWgTdwslVb4sEMOCf7Bv0JAK67d
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
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-contrib/static v0.0.1 h1:JVxuvHPuUfkoul12N7dtQw7KRn/pSMq7Ue1Va9Swm1U=
github.com/gin-contrib/static v0.0.1/go.mod h1:CSxeF+wep05e0kCOsqWdAWbSszmc31zTIbD8TvWl7Hs=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand All @@ -185,11 +188,15 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/go-playground/validator/v10 v10.15.4 h1:zMXza4EpOdooxPel5xDqXEdXG5r+WggpvnAKMsalBjs=
github.com/go-playground/validator/v10 v10.15.4/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
Expand Down Expand Up @@ -377,6 +384,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lukasjarosch/go-docx v0.4.7 h1:+yXUfj8ZJatMjL88MC0MEQQ5HSHzmZNyuWBAQxh6bmA=
Expand Down Expand Up @@ -543,6 +551,8 @@ github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSW
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
Expand Down
34 changes: 19 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ import (
"github.com/spf13/viper"
)

func main() {
utils.ReadConf()
channel.InitManager()

if cli.Run() {
return
func registerApiRouter(engine *gin.Engine) {
var app *gin.RouterGroup
if !viper.GetBool("serve_static") {
app = engine.Group("")
} else {
app = engine.Group("/api")
}

app := gin.New()
worker := middleware.RegisterMiddleware(app)
defer worker()

{
auth.Register(app)
admin.Register(app)
Expand All @@ -36,14 +32,22 @@ func main() {
addition.Register(app)
conversation.Register(app)
}
}

if viper.GetBool("debug") {
app.Use(gin.Logger())
} else {
gin.SetMode(gin.ReleaseMode)
func main() {
utils.ReadConf()
channel.InitManager()

if cli.Run() {
return
}

app.Use(gin.Recovery())
app := utils.NewEngine()
worker := middleware.RegisterMiddleware(app)
defer worker()

utils.RegisterStaticRoute(app)
registerApiRouter(app)

if err := app.Run(fmt.Sprintf(":%s", viper.GetString("server.port"))); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion manager/broadcast/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package broadcast

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

func Register(app *gin.Engine) {
func Register(app *gin.RouterGroup) {
app.GET("/broadcast/view", ViewBroadcastAPI)
app.GET("/broadcast/list", GetBroadcastListAPI)
app.POST("/broadcast/create", CreateBroadcastAPI)
Expand Down
2 changes: 1 addition & 1 deletion manager/conversation/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package conversation

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

func Register(app *gin.Engine) {
func Register(app *gin.RouterGroup) {
router := app.Group("/conversation")
{
router.GET("/list", ListAPI)
Expand Down
2 changes: 1 addition & 1 deletion manager/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/gin-gonic/gin"
)

func Register(app *gin.Engine) {
func Register(app *gin.RouterGroup) {
app.GET("/chat", ChatAPI)
app.GET("/v1/models", ModelAPI)
app.GET("/v1/charge", ChargeAPI)
Expand Down
34 changes: 34 additions & 0 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package utils

import (
"fmt"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"strings"
)
Expand All @@ -26,3 +28,35 @@ func ReadConf() {
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
}

func NewEngine() *gin.Engine {
engine := gin.New()

if viper.GetBool("debug") {
engine.Use(gin.Logger())
} else {
gin.SetMode(gin.ReleaseMode)
}

engine.Use(gin.Recovery())

return engine
}

func RegisterStaticRoute(engine *gin.Engine) {
// static files are in ~/app/dist

if !viper.GetBool("serve_static") {
return
}

if !IsFileExist("./app/dist") {
fmt.Println("[service] app/dist not found, please run `npm run build`")
return
}

engine.Use(static.Serve("/", static.LocalFile("./app/dist", true)))
engine.NoRoute(func(c *gin.Context) {
c.File("./app/dist/index.html")
})
}

0 comments on commit 0ff50b9

Please sign in to comment.