Compare commits
1 Commits
main
...
5f3074e9bf
Author | SHA1 | Date | |
---|---|---|---|
5f3074e9bf |
1
.gitignore
vendored
1
.gitignore
vendored
@ -21,4 +21,3 @@
|
|||||||
# Go workspace file
|
# Go workspace file
|
||||||
go.work
|
go.work
|
||||||
|
|
||||||
go-web
|
|
||||||
|
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,6 +1,3 @@
|
|||||||
[submodule "framework/gin"]
|
[submodule "framework/gin"]
|
||||||
path = framework/gin
|
path = framework/gin
|
||||||
url = gitea@git.vinchent.xyz:vinchent/gin.git
|
url = gitea@git.vinchent.xyz:vinchent/gin.git
|
||||||
[submodule "framework/cobra"]
|
|
||||||
path = framework/cobra
|
|
||||||
url = gitea@git.vinchent.xyz:vinchent/cobra.git
|
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
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())
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package web
|
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
|
||||||
|
|
||||||
func Routes(r *gin.Engine) {
|
|
||||||
r.Static("/dist/", "./dist/")
|
|
||||||
|
|
||||||
// demo.Register(r)
|
|
||||||
}
|
|
Submodule framework/cobra deleted from 6d888d3313
@ -1,8 +0,0 @@
|
|||||||
package commands
|
|
||||||
|
|
||||||
import "github.com/spf13/cobra"
|
|
||||||
|
|
||||||
func AddKernelCommands(root *cobra.Command) {
|
|
||||||
// root.AddCommand(DemoCommand)
|
|
||||||
// root.AddCommand(initAppCommand())
|
|
||||||
}
|
|
@ -1,189 +0,0 @@
|
|||||||
package framework
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Container interface {
|
|
||||||
// Bind binds a service provider to the container.
|
|
||||||
//
|
|
||||||
// If the service exists, the old one will be replaced by the new one.
|
|
||||||
// No error is returned in this case.
|
|
||||||
//
|
|
||||||
// Return any Init() error.
|
|
||||||
Bind(provider ServiceProvider) error
|
|
||||||
|
|
||||||
// IsBound returns true the provider is bound to the container.
|
|
||||||
IsBound(name string) bool
|
|
||||||
|
|
||||||
// Make gets or creates a service by its name.
|
|
||||||
//
|
|
||||||
// Return error if the service cannot be initiated or doesn't exist.
|
|
||||||
Make(name string) (interface{}, error)
|
|
||||||
|
|
||||||
// MustMake makes a service, supposing that it is not instantiated before.
|
|
||||||
// Return nil if error.
|
|
||||||
MustMake(name string) interface{}
|
|
||||||
|
|
||||||
// MakeNew creates a new instance of the service with different parameters.
|
|
||||||
//
|
|
||||||
// Used for non singleton services.
|
|
||||||
MakeNew(name string, params []interface{}) (interface{}, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type GoWebContainer struct {
|
|
||||||
// Must implement Container interfaces.
|
|
||||||
Container
|
|
||||||
|
|
||||||
// Service providers by name.
|
|
||||||
providers map[string]ServiceProvider
|
|
||||||
|
|
||||||
// Instantiated services by name.
|
|
||||||
instances map[string]interface{}
|
|
||||||
|
|
||||||
// RWMutex to protect the changes in container.
|
|
||||||
mu sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func errUnknownService(name string) error {
|
|
||||||
return fmt.Errorf("service %q is not registered in the container", name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGoWebContainer() *GoWebContainer {
|
|
||||||
return &GoWebContainer{
|
|
||||||
providers: map[string]ServiceProvider{},
|
|
||||||
instances: map[string]interface{}{},
|
|
||||||
mu: sync.RWMutex{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) PrintProviders(w io.Writer) {
|
|
||||||
ret := make([]string, len(c.providers))
|
|
||||||
for _, provider := range c.providers {
|
|
||||||
name := provider.Name()
|
|
||||||
|
|
||||||
ret = append(ret, name)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(w, "%v", ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) Bind(provider ServiceProvider) error {
|
|
||||||
c.bindProvider(provider)
|
|
||||||
// Instantiate Later ?
|
|
||||||
if provider.InstantiateLater() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instantiate now !
|
|
||||||
_, err := c.instantiate(provider, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) bindProvider(provider ServiceProvider) {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
name := provider.Name()
|
|
||||||
c.providers[name] = provider
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) IsBound(name string) bool {
|
|
||||||
return c.getServiceProvider(name) != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) getServiceProvider(name string) ServiceProvider {
|
|
||||||
c.mu.RLock()
|
|
||||||
defer c.mu.RUnlock()
|
|
||||||
|
|
||||||
provider, ok := c.providers[name]
|
|
||||||
if ok {
|
|
||||||
return provider
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) Make(name string) (interface{}, error) {
|
|
||||||
return c.makeServiceInstance(name, nil, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) MustMake(name string) interface{} {
|
|
||||||
ins, err := c.makeServiceInstance(name, nil, false)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return ins
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) MakeNew(name string, params []interface{}) (interface{}, error) {
|
|
||||||
return c.makeServiceInstance(name, params, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) getServiceInstance(name string) interface{} {
|
|
||||||
c.mu.RLock()
|
|
||||||
defer c.mu.RUnlock()
|
|
||||||
|
|
||||||
instance, ok := c.instances[name]
|
|
||||||
if ok {
|
|
||||||
return instance
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) makeServiceInstance(
|
|
||||||
name string,
|
|
||||||
params []interface{},
|
|
||||||
forceNew bool,
|
|
||||||
) (interface{}, error) {
|
|
||||||
provider := c.getServiceProvider(name)
|
|
||||||
if provider == nil {
|
|
||||||
return nil, errUnknownService(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
if forceNew {
|
|
||||||
return c.instantiate(provider, params)
|
|
||||||
}
|
|
||||||
|
|
||||||
instance := c.getServiceInstance(name)
|
|
||||||
|
|
||||||
if instance != nil {
|
|
||||||
return instance, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// create a new instance
|
|
||||||
return c.instantiate(provider, params)
|
|
||||||
}
|
|
||||||
|
|
||||||
// instantiate instantiates a new instance.
|
|
||||||
// If no params are provided, then default params are used.
|
|
||||||
func (c *GoWebContainer) instantiate(
|
|
||||||
provider ServiceProvider,
|
|
||||||
params []interface{},
|
|
||||||
) (interface{}, error) {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
if err := provider.Init(c); err != nil {
|
|
||||||
// TODO: Error should be wrapped.
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if params == nil {
|
|
||||||
params = provider.Params(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
construct := provider.Register(c)
|
|
||||||
instance, err := construct(params...)
|
|
||||||
if err != nil {
|
|
||||||
// TODO: Error should be wrapped.
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.instances[provider.Name()] = instance
|
|
||||||
|
|
||||||
return instance, nil
|
|
||||||
}
|
|
@ -1,88 +0,0 @@
|
|||||||
package framework
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// {{{ contract
|
|
||||||
|
|
||||||
const DummyProviderName = "container:test"
|
|
||||||
|
|
||||||
type IDummyService interface {
|
|
||||||
GetDummy() DummyStruct
|
|
||||||
}
|
|
||||||
|
|
||||||
type DummyStruct struct {
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
// {{{ provider
|
|
||||||
type DummyServiceProvider struct{}
|
|
||||||
|
|
||||||
func (p *DummyServiceProvider) Name() string {
|
|
||||||
return DummyProviderName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *DummyServiceProvider) Register(c Container) NewInstance {
|
|
||||||
return NewDummyService
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *DummyServiceProvider) Init(c Container) error {
|
|
||||||
log.Println("init")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *DummyServiceProvider) InstantiateLater() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *DummyServiceProvider) Params(c Container) []interface{} {
|
|
||||||
return []interface{}{c}
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
// {{{ service
|
|
||||||
|
|
||||||
type DummyService struct {
|
|
||||||
IDummyService
|
|
||||||
|
|
||||||
// parameters
|
|
||||||
c Container
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DummyService) GetDummy() DummyStruct {
|
|
||||||
return DummyStruct{
|
|
||||||
Name: "Dummy!",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDummyService(params ...interface{}) (interface{}, error) {
|
|
||||||
c := params[0].(Container)
|
|
||||||
|
|
||||||
log.Println("new dummy service")
|
|
||||||
|
|
||||||
return &DummyService{c: c}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
func TestBind(t *testing.T) {
|
|
||||||
container := NewGoWebContainer()
|
|
||||||
provider := &DummyServiceProvider{}
|
|
||||||
|
|
||||||
container.Bind(provider)
|
|
||||||
|
|
||||||
dummyService := container.MustMake(DummyProviderName).(IDummyService)
|
|
||||||
|
|
||||||
want := DummyStruct{
|
|
||||||
Name: "Dummy!",
|
|
||||||
}
|
|
||||||
got := dummyService.GetDummy()
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(want, got) {
|
|
||||||
t.Errorf("want %v, got %v ", want, got)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package contract
|
|
||||||
|
|
||||||
const AppName = "goweb:app"
|
|
||||||
|
|
||||||
type App interface {
|
|
||||||
Version() string
|
|
||||||
BaseFolder() string
|
|
||||||
ConfigFolder() string
|
|
||||||
LogFolder() string
|
|
||||||
ProvidersFolder() string
|
|
||||||
MiddlewaresFolder() string
|
|
||||||
CommandsFolder() string
|
|
||||||
RuntimeFolder() string
|
|
||||||
TestsFolder() string
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
Submodule framework/gin updated: a5849d15be...f05f966a08
@ -1,24 +0,0 @@
|
|||||||
package framework
|
|
||||||
|
|
||||||
// NewInstance defines the function to create a new instance for a service.
|
|
||||||
type NewInstance func(...interface{}) (interface{}, error)
|
|
||||||
|
|
||||||
// ServiceProvider is the interface for a service provider
|
|
||||||
type ServiceProvider interface {
|
|
||||||
// Register a service into the service container, return a NewInstance constructer
|
|
||||||
Register(Container) NewInstance
|
|
||||||
|
|
||||||
// Init is called when instantiating the service.
|
|
||||||
Init(Container) error
|
|
||||||
|
|
||||||
// InstantiateLater decides if the service is instantiated at register phase.
|
|
||||||
// If true, then this service shall be instantiated later.
|
|
||||||
InstantiateLater() bool
|
|
||||||
|
|
||||||
// Params defines the parameters needed to instantiate a service.
|
|
||||||
// NOTE: First one should always be the container.
|
|
||||||
Params(Container) []interface{}
|
|
||||||
|
|
||||||
// Name is the name of the service provider
|
|
||||||
Name() string
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.vinchent.xyz/vinchent/go-web/framework"
|
|
||||||
"git.vinchent.xyz/vinchent/go-web/framework/contract"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GoWebAppProvider struct {
|
|
||||||
BaseFolder string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebAppProvider) Register(c framework.Container) framework.NewInstance {
|
|
||||||
return NewGoWebApp
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebAppProvider) Init(c framework.Container) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebAppProvider) InstantiateLater() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebAppProvider) Params(c framework.Container) []interface{} {
|
|
||||||
return []interface{}{c, goweb.BaseFolder}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebAppProvider) Name() string {
|
|
||||||
return contract.AppName
|
|
||||||
}
|
|
@ -1,102 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"git.vinchent.xyz/vinchent/go-web/framework"
|
|
||||||
"git.vinchent.xyz/vinchent/go-web/framework/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GoWebApp struct {
|
|
||||||
container framework.Container
|
|
||||||
baseFolder string
|
|
||||||
}
|
|
||||||
|
|
||||||
const AppVersion = "0.0.1"
|
|
||||||
|
|
||||||
func ErrParamsAmount(want int, got int) error {
|
|
||||||
return fmt.Errorf("want %d arguments, got %d", want, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewGoWebApp creates a new GoWeb app.
|
|
||||||
//
|
|
||||||
// \params[in]: container
|
|
||||||
// \params[in]: baseFolder
|
|
||||||
func NewGoWebApp(params ...interface{}) (interface{}, error) {
|
|
||||||
if len(params) != 2 {
|
|
||||||
return nil, ErrParamsAmount(2, len(params))
|
|
||||||
}
|
|
||||||
|
|
||||||
container := params[0].(framework.Container)
|
|
||||||
baseFolder := params[1].(string)
|
|
||||||
return &GoWebApp{
|
|
||||||
container: container,
|
|
||||||
baseFolder: baseFolder,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) Version() string {
|
|
||||||
return AppVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) BaseFolder() string {
|
|
||||||
if goweb.baseFolder != "" {
|
|
||||||
return goweb.baseFolder
|
|
||||||
}
|
|
||||||
|
|
||||||
// not defined yet
|
|
||||||
var baseFolder string
|
|
||||||
flag.StringVar(
|
|
||||||
&baseFolder,
|
|
||||||
"base_folder",
|
|
||||||
utils.GetExecDirectory(),
|
|
||||||
"base_folder of the app. Use the current path as default value",
|
|
||||||
)
|
|
||||||
|
|
||||||
return baseFolder
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) ConfigFolder() string {
|
|
||||||
return filepath.Join(goweb.BaseFolder(), "config")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) StorageFolder() string {
|
|
||||||
return filepath.Join(goweb.BaseFolder(), "storage")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) LogFolder() string {
|
|
||||||
// storage/log
|
|
||||||
return filepath.Join(goweb.StorageFolder(), "log")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) ProvidersFolder() string {
|
|
||||||
return filepath.Join(goweb.BaseFolder(), "providers")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) WebFolder() string {
|
|
||||||
return filepath.Join(goweb.BaseFolder(), "web")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) MiddlewaresFolder() string {
|
|
||||||
// http/middlewares
|
|
||||||
return filepath.Join(goweb.WebFolder(), "middlewares")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) ConsoleFolder() string {
|
|
||||||
return filepath.Join(goweb.BaseFolder(), "console")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) CommandsFolder() string {
|
|
||||||
// console/commands
|
|
||||||
return filepath.Join(goweb.ConsoleFolder(), "commands")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) RuntimeFolder() string {
|
|
||||||
return filepath.Join(goweb.StorageFolder(), "runtime")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebApp) TestsFolder() string {
|
|
||||||
return filepath.Join(goweb.BaseFolder(), "tests")
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
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{}{goweb.HttpEngine}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (goweb *GoWebKernelProvider) Name() string {
|
|
||||||
return contract.AppName
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import "os"
|
|
||||||
|
|
||||||
// Get the current folder with / suffix
|
|
||||||
func GetExecDirectory() string {
|
|
||||||
path, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return path + "/"
|
|
||||||
}
|
|
12
go.mod
12
go.mod
@ -2,10 +2,7 @@ module git.vinchent.xyz/vinchent/go-web
|
|||||||
|
|
||||||
go 1.22.5
|
go 1.22.5
|
||||||
|
|
||||||
require (
|
require github.com/gin-gonic/gin v1.10.0
|
||||||
github.com/gin-gonic/gin v1.10.0
|
|
||||||
github.com/spf13/cobra v1.8.1
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bytedance/sonic v1.11.6 // indirect
|
github.com/bytedance/sonic v1.11.6 // indirect
|
||||||
@ -20,10 +17,9 @@ require (
|
|||||||
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
|
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
|
||||||
github.com/goccy/go-json v0.10.2 // indirect
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // 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/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
github.com/kr/pretty v0.3.1 // indirect
|
github.com/kr/text v0.2.0 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
@ -32,7 +28,7 @@ require (
|
|||||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||||
github.com/quic-go/qpack v0.4.0 // indirect
|
github.com/quic-go/qpack v0.4.0 // indirect
|
||||||
github.com/quic-go/quic-go v0.43.1 // indirect
|
github.com/quic-go/quic-go v0.43.1 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/rogpeppe/go-internal v1.9.0 // indirect
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
go.uber.org/mock v0.4.0 // indirect
|
go.uber.org/mock v0.4.0 // indirect
|
||||||
@ -49,5 +45,3 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
replace github.com/gin-gonic/gin => ./framework/gin
|
replace github.com/gin-gonic/gin => ./framework/gin
|
||||||
|
|
||||||
replace github.com/spf13/cobra => ./framework/cobra
|
|
||||||
|
5
go.sum
5
go.sum
@ -39,8 +39,6 @@ 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 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
|
||||||
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
|
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/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 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
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=
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
@ -66,7 +64,6 @@ github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
|
|||||||
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
|
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
|
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
|
||||||
@ -75,8 +72,6 @@ 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/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 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
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.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.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
46
main.go
46
main.go
@ -1,24 +1,42 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vinchent.xyz/vinchent/go-web/app/console"
|
"context"
|
||||||
"git.vinchent.xyz/vinchent/go-web/app/web"
|
"fmt"
|
||||||
"git.vinchent.xyz/vinchent/go-web/framework"
|
"log"
|
||||||
"git.vinchent.xyz/vinchent/go-web/framework/providers/app"
|
"net/http"
|
||||||
"git.vinchent.xyz/vinchent/go-web/framework/providers/kernel"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
container := framework.NewGoWebContainer()
|
core := gin.New()
|
||||||
|
registerRouter(core)
|
||||||
container.Bind(&app.GoWebAppProvider{})
|
server := &http.Server{
|
||||||
|
Addr: ":8080",
|
||||||
engine, err := web.NewHttpEngine()
|
Handler: core,
|
||||||
if err != nil {
|
|
||||||
panic("Cannot start http server")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
container.Bind(&kernel.GoWebKernelProvider{HttpEngine: engine})
|
go func() {
|
||||||
|
server.ListenAndServe()
|
||||||
|
}()
|
||||||
|
|
||||||
console.RunCommand(container)
|
// 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user