allow callable (returning data) in request action handler
This commit is contained in:
		
							parent
							
								
									ce4d67d33a
								
							
						
					
					
						commit
						520244fb19
					
				
					 5 changed files with 42 additions and 10 deletions
				
			
		
							
								
								
									
										4
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							|  | @ -1,5 +1,5 @@ | ||||||
| bin* | bin/ | ||||||
| .env | *.env* | ||||||
| settings.go | settings.go | ||||||
| *.log | *.log | ||||||
| *.sqlite | *.sqlite | ||||||
|  |  | ||||||
|  | @ -6,6 +6,7 @@ import ( | ||||||
| 	"git.sr.ht/~cco/go-scopes/config" | 	"git.sr.ht/~cco/go-scopes/config" | ||||||
| 	"git.sr.ht/~cco/go-scopes/logging" | 	"git.sr.ht/~cco/go-scopes/logging" | ||||||
| 	"git.sr.ht/~cco/go-scopes/server" | 	"git.sr.ht/~cco/go-scopes/server" | ||||||
|  | 	"git.sr.ht/~cco/go-scopes/storage/sql" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func Config() lib.Config { | func Config() lib.Config { | ||||||
|  | @ -32,12 +33,29 @@ func Config() lib.Config { | ||||||
| 	return app_c | 	return app_c | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func ConfigDB() *sql.Cfg { | ||||||
|  | 	ovr := Overrides().Use | ||||||
|  | 	return &sql.Cfg{ | ||||||
|  | 		Driver:  "postgres", | ||||||
|  | 		Connstr: ovr("user=cco password=dummy dbname=scps", PGSQL_CONNSTR), | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // collect here the names of fields that may be overridden via | // collect here the names of fields that may be overridden via | ||||||
| // explicit Override() or SCOPES_* environment settings. | // explicit Override() or SCOPES_* environment settings. | ||||||
| const ( | const ( | ||||||
| 	HOME        = "home" | 	HOME          = "home" | ||||||
| 	LOGFILE     = "logfile" | 	LOGFILE       = "logfile" | ||||||
| 	LOGLEVEL    = "loglevel" | 	LOGLEVEL      = "loglevel" | ||||||
| 	SERVER_PORT = "server_port" | 	SERVER_PORT   = "server_port" | ||||||
| 	DOCROOT     = "docroot" | 	DOCROOT       = "docroot" | ||||||
|  | 	PGSQL_CONNSTR = "pgsql_connstr" | ||||||
| ) | ) | ||||||
|  | 
 | ||||||
|  | // put something like this (without comment markup) | ||||||
|  | // in separate (non-versioned) settings.go file: | ||||||
|  | /*func Overrides() config.Settings { | ||||||
|  | 	return config.Settings{ | ||||||
|  | 		SERVER_PORT: "8126", | ||||||
|  | 	} | ||||||
|  | }*/ | ||||||
|  |  | ||||||
|  | @ -64,5 +64,3 @@ func (spec *mhSpec) AddActionProc( | ||||||
| func MsgHandler() *mhSpec { | func MsgHandler() *mhSpec { | ||||||
| 	return &mhSpec{config.Default("")} | 	return &mhSpec{config.Default("")} | ||||||
| } | } | ||||||
| 
 |  | ||||||
| type msgProc func(lib.Context, lib.Message) (int, lib.Data) |  | ||||||
|  |  | ||||||
|  | @ -89,6 +89,17 @@ func Async(ctx lib.Context, msg lib.Message) (int, lib.Data) { | ||||||
| 	return http.StatusOK, lib.Map{"status": "OK"} | 	return http.StatusOK, lib.Map{"status": "OK"} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func Call(clbl callable) msgProc { | ||||||
|  | 	return func(ctx lib.Context, msg lib.Message) (int, lib.Data) { | ||||||
|  | 		data, err := clbl(ctx, msg) | ||||||
|  | 		if err != nil { | ||||||
|  | 			data = lib.Map{"error": err.Error()} | ||||||
|  | 			return http.StatusInternalServerError, data | ||||||
|  | 		} | ||||||
|  | 		return http.StatusOK, data | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func Sync(timeout int) msgProc { | func Sync(timeout int) msgProc { | ||||||
| 	return func(ctx lib.Context, msg lib.Message) (int, lib.Data) { | 	return func(ctx lib.Context, msg lib.Message) (int, lib.Data) { | ||||||
| 		return sync(ctx, msg, timeout) | 		return sync(ctx, msg, timeout) | ||||||
|  | @ -107,3 +118,7 @@ func sync(ctx lib.Context, msg lib.Message, to int) (int, lib.Data) { | ||||||
| 	} | 	} | ||||||
| 	return http.StatusNoContent, nil | 	return http.StatusNoContent, nil | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // message processing function types | ||||||
|  | type msgProc func(lib.Context, lib.Message) (int, lib.Data) | ||||||
|  | type callable func(lib.Context, lib.Message) (lib.Data, error) | ||||||
|  |  | ||||||
|  | @ -5,7 +5,8 @@ import ( | ||||||
| 	"git.sr.ht/~cco/go-scopes/storage/sql" | 	"git.sr.ht/~cco/go-scopes/storage/sql" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var Store = func(db *sql.Storage, msg lib.Message) { | // var Store = func(db *sql.Storage, msg lib.Message) { | ||||||
|  | func Store(db *sql.Storage, msg lib.Message) { | ||||||
| 	q := db.BuildQuery("insert_msg", "messages") | 	q := db.BuildQuery("insert_msg", "messages") | ||||||
| 	db.Exec(q, msg.Domain(), msg.Action(), msg.Class(), msg.Item(), msg.Payload()) | 	db.Exec(q, msg.Domain(), msg.Action(), msg.Class(), msg.Item(), msg.Payload()) | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue