Integrate cobra into the framework part1

This commit is contained in:
Muyao CHEN 2024-09-30 19:38:16 +02:00
parent fc4f94e967
commit 9da3012789
13 changed files with 159 additions and 42 deletions

30
app/console/kernel.go Normal file
View File

@ -0,0 +1,30 @@
package console
import (
"git.vinchent.xyz/vinchent/go-web/framework"
"git.vinchent.xyz/vinchent/go-web/framework/commands"
"github.com/spf13/cobra"
)
func RunCommand(container framework.Container) error {
rootCmd := &cobra.Command{
Use: "goweb",
Short: "goweb command",
Long: "the command line tool for goweb framework.",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.InitDefaultHelpFlag()
return cmd.Help()
},
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
}
rootCmd.SetContainer(container)
commands.AddKernelCommands(rootCmd)
AddAppCommand(rootCmd)
return rootCmd.Execute()
}
func AddAppCommand(rootCmd *cobra.Command) {
// rootCmd.AddCommand(demo.InitFoo())
}

14
app/web/kernel.go Normal file
View File

@ -0,0 +1,14 @@
package web
import "github.com/gin-gonic/gin"
func NewHttpEngine() (*gin.Engine, error) {
// Use ReleaseMode by default to not ouput debug info
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
Routes(r)
return r, nil
}

View File

9
app/web/route.go Normal file
View File

@ -0,0 +1,9 @@
package web
import "github.com/gin-gonic/gin"
func Routes(r *gin.Engine) {
r.Static("/dist/", "./dist/")
// demo.Register(r)
}

View File

@ -0,0 +1,8 @@
package commands
import "github.com/spf13/cobra"
func AddKernelCommands(root *cobra.Command) {
// root.AddCommand(DemoCommand)
// root.AddCommand(initAppCommand())
}

View File

@ -0,0 +1,10 @@
package contract
import "net/http"
const KernelName = "goweb:kernel"
type Kernel interface {
// HttpEngine we use actually gin.Engine, but it is open to change.
HttpEngine() http.Handler
}

View File

@ -5,26 +5,26 @@ import (
"git.vinchent.xyz/vinchent/go-web/framework/contract"
)
type GoWebProvider struct {
type GoWebAppProvider struct {
BaseFolder string
}
func (goweb *GoWebProvider) Register(c framework.Container) framework.NewInstance {
func (goweb *GoWebAppProvider) Register(c framework.Container) framework.NewInstance {
return NewGoWebApp
}
func (goweb *GoWebProvider) Init(c framework.Container) error {
func (goweb *GoWebAppProvider) Init(c framework.Container) error {
return nil
}
func (goweb *GoWebProvider) InstantiateLater() bool {
func (goweb *GoWebAppProvider) InstantiateLater() bool {
return false
}
func (goweb *GoWebProvider) Params(c framework.Container) []interface{} {
func (goweb *GoWebAppProvider) Params(c framework.Container) []interface{} {
return []interface{}{c, goweb.BaseFolder}
}
func (goweb *GoWebProvider) Name() string {
func (goweb *GoWebAppProvider) Name() string {
return contract.AppName
}

View File

@ -75,13 +75,13 @@ func (goweb *GoWebApp) ProvidersFolder() string {
return filepath.Join(goweb.BaseFolder(), "providers")
}
func (goweb *GoWebApp) HttpFolder() string {
return filepath.Join(goweb.BaseFolder(), "http")
func (goweb *GoWebApp) WebFolder() string {
return filepath.Join(goweb.BaseFolder(), "web")
}
func (goweb *GoWebApp) MiddlewaresFolder() string {
// http/middlewares
return filepath.Join(goweb.HttpFolder(), "middlewares")
return filepath.Join(goweb.WebFolder(), "middlewares")
}
func (goweb *GoWebApp) ConsoleFolder() string {

View File

@ -0,0 +1,31 @@
package kernel
import (
"git.vinchent.xyz/vinchent/go-web/framework"
"git.vinchent.xyz/vinchent/go-web/framework/contract"
"github.com/gin-gonic/gin"
)
type GoWebKernelProvider struct {
HttpEngine *gin.Engine
}
func (goweb *GoWebKernelProvider) Register(c framework.Container) framework.NewInstance {
return NewGoWebKernelService
}
func (goweb *GoWebKernelProvider) Init(c framework.Container) error {
return nil
}
func (goweb *GoWebKernelProvider) InstantiateLater() bool {
return false
}
func (goweb *GoWebKernelProvider) Params(c framework.Container) []interface{} {
return []interface{}{c, goweb.HttpEngine}
}
func (goweb *GoWebKernelProvider) Name() string {
return contract.AppName
}

View File

@ -0,0 +1,22 @@
package kernel
import (
"net/http"
"github.com/gin-gonic/gin"
)
type GoWebKernelService struct {
// Since the container is included in the engine, we don't need to pass
// it as an argument.
engine *gin.Engine
}
func NewGoWebKernelService(params ...interface{}) (interface{}, error) {
httpEngine := params[0].(*gin.Engine)
return &GoWebKernelService{engine: httpEngine}, nil
}
func (goweb *GoWebKernelService) HttpEngine() http.Handler {
return goweb.engine
}

9
go.mod
View File

@ -2,7 +2,10 @@ module git.vinchent.xyz/vinchent/go-web
go 1.22.5
require github.com/gin-gonic/gin v1.10.0
require (
github.com/gin-gonic/gin v1.10.0
github.com/spf13/cobra v1.8.1
)
require (
github.com/bytedance/sonic v1.11.6 // indirect
@ -17,6 +20,7 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
@ -28,6 +32,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/quic-go v0.43.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/mock v0.4.0 // indirect
@ -44,3 +49,5 @@ require (
)
replace github.com/gin-gonic/gin => ./framework/gin
replace github.com/spf13/cobra => ./framework/cobra

4
go.sum
View File

@ -39,6 +39,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
@ -73,6 +75,8 @@ github.com/quic-go/quic-go v0.43.1 h1:fLiMNfQVe9q2JvSsiXo4fXOEguXHGGl9+6gLp4RPeZ
github.com/quic-go/quic-go v0.43.1/go.mod h1:132kz4kL3F9vxhW3CtQJLDVwcFe5wdWeJXXijhsO57M=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

46
main.go
View File

@ -1,42 +1,24 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
"git.vinchent.xyz/vinchent/go-web/app/console"
"git.vinchent.xyz/vinchent/go-web/app/web"
"git.vinchent.xyz/vinchent/go-web/framework"
"git.vinchent.xyz/vinchent/go-web/framework/providers/app"
"git.vinchent.xyz/vinchent/go-web/framework/providers/kernel"
)
func main() {
core := gin.New()
registerRouter(core)
server := &http.Server{
Addr: ":8080",
Handler: core,
container := framework.NewGoWebContainer()
container.Bind(&app.GoWebAppProvider{})
engine, err := web.NewHttpEngine()
if err != nil {
panic("Cannot start http server")
}
go func() {
server.ListenAndServe()
}()
container.Bind(&kernel.GoWebKernelProvider{HttpEngine: engine})
// create quit channel
quit := make(chan os.Signal, 1)
// set notifier
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-quit
fmt.Println("YOLO")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatal("server shutdown: ", err)
}
console.RunCommand(container)
}