0x4a52466c696e74 преди 3 месеца
родител
ревизия
1aed4f1f22
променени са 4 файла, в които са добавени 63 реда и са изтрити 17 реда
  1. 12 4
      application/app_config.go
  2. 2 3
      application/application.go
  3. 14 10
      application/z_test.go
  4. 35 0
      commands/store.go

+ 12 - 4
application/app_config.go

@@ -1,6 +1,9 @@
 package application
 
-import "git.ali33.ru/fcg-xvii/rest"
+import (
+	"git.ali33.ru/fcg-xvii/rest"
+	"git.ali33.ru/fcg-xvii/rest/commands"
+)
 
 func HTTPHeadersCrossOrigin() func() map[string]string {
 	return func() map[string]string {
@@ -29,6 +32,13 @@ type AppConfig struct {
 	OnSocketDisconnect func(rest.IStream)
 }
 
+func (s *AppConfig) GetCommandsMethod() func(string) (rest.IExecuter, bool) {
+	if s.Commands != nil {
+		return s.Commands
+	}
+	return commands.GetCommand
+}
+
 func (s *AppConfig) GetHttpPrefix() string {
 	if len(s.HttpPrefix) > 0 {
 		return s.HttpPrefix
@@ -47,7 +57,5 @@ func (s *AppConfig) GetHTTPHeaders() func() map[string]string {
 	if s.HTTPHeaders != nil {
 		return s.HTTPHeaders
 	}
-	return func() map[string]string {
-		return map[string]string{}
-	}
+	return HTTPHeadersCrossOrigin()
 }

+ 2 - 3
application/application.go

@@ -85,9 +85,8 @@ func (s *Application) work() {
 }
 
 func (s *Application) Executer(r rest.IRequestIn) (rest.IExecuter, bool) {
-	log.Println("EXECUTER", r.RCommand())
-	if command, check := s.conf.Commands(r.RCommand()); check {
-		log.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
+	method := s.conf.GetCommandsMethod()
+	if command, check := method(r.RCommand()); check {
 		return command, true
 	}
 	return nil, false

+ 14 - 10
application/z_test.go

@@ -8,8 +8,15 @@ import (
 
 	"git.ali33.ru/fcg-xvii/rest"
 	"git.ali33.ru/fcg-xvii/rest/application"
+	"git.ali33.ru/fcg-xvii/rest/commands"
 )
 
+func init() {
+	commands.Register("hello", func() rest.IExecuter {
+		return &Hello{}
+	})
+}
+
 type Hello struct {
 	Name string `rest:"required"`
 }
@@ -35,13 +42,13 @@ type HelloResponse struct {
 ////////////////////////////
 
 var (
-	commands = map[string]func() rest.IExecuter{
+	commandsList = map[string]func() rest.IExecuter{
 		"/api/hello": func() rest.IExecuter { return &Hello{} },
 	}
 )
 
 func Command(cName string) (rest.IExecuter, bool) {
-	if command, check := commands[cName]; check {
+	if command, check := commandsList[cName]; check {
 		return command(), true
 	}
 	return nil, false
@@ -49,14 +56,11 @@ func Command(cName string) (rest.IExecuter, bool) {
 
 func TestApp(t *testing.T) {
 	conf := &application.AppConfig{
-		Addr:            "192.168.100.3:5000",
-		Http:            true,
-		Websocket:       false,
-		Secret:          []byte("secret"),
-		HttpPrefix:      "/api/",
-		WebsocketPrefix: "/ws/",
-		HTTPHeaders:     application.HTTPHeadersCrossOrigin(),
-		Commands:        Command,
+		Addr:       "192.168.100.3:5000",
+		Http:       true,
+		Websocket:  false,
+		Secret:     []byte("secret"),
+		HttpPrefix: "/api/",
 	}
 
 	app := application.New(conf, nil, context.Background())

+ 35 - 0
commands/store.go

@@ -0,0 +1,35 @@
+package commands
+
+import (
+	"fmt"
+
+	"git.ali33.ru/fcg-xvii/rest"
+)
+
+var (
+	store     = make(map[string]func() rest.IExecuter)
+	URLPresix = "api"
+)
+
+func Register(name string, cmd func() rest.IExecuter) {
+	store[fmt.Sprintf("/%s/%s", URLPresix, name)] = cmd
+}
+
+func RegisterSection(section, name string, cmd func() rest.IExecuter) {
+	store[fmt.Sprintf("/%s/%s/%s", URLPresix, section, name)] = cmd
+}
+
+func GetCommand(command string) (rest.IExecuter, bool) {
+	if cmd, ok := store[command]; ok {
+		return cmd(), true
+	}
+	return nil, false
+}
+
+func ListCommands() []string {
+	commands := make([]string, len(store), 0)
+	for name := range store {
+		commands = append(commands, name)
+	}
+	return commands
+}