Documentation ¶
Overview ¶
Package service provides primitives, structures and functions for building HTTP services. For more information look at https://github.com/ampho-cms/backend/wiki/Service.
Example (Extend) ¶
This example shows how to extend a base service.
// Author: Alexander Shepetko // Email: [email protected] // License: MIT package main import ( "log" "ampho.xyz/core/config" "ampho.xyz/core/service" ) type AwesomeService struct { service.Service } // This example shows how to extend a base service. func main() { const svName = "hello" // Config cfg, err := config.New(svName, "yaml", config.DefaultSearchPaths(svName)...) if err != nil { log.Fatalf("failed to init config: %v", err) } // Create a base service instance base, err := service.New(cfg) if err != nil { log.Fatal(err) } // Embed base service svc := AwesomeService{base} // Base setup code here // ... // Run the service until SIGINT svc.Run() }
Output:
Example (NewDefault) ¶
This example shows how to instantiate and run a service using default configuration.
package main import ( "log" "ampho.xyz/core/config" "ampho.xyz/core/service" ) func main() { const svName = "hello" // Config cfg, err := config.New(svName, "yaml", config.DefaultSearchPaths(svName)...) if err != nil { log.Fatalf("failed to init config: %v", err) } // Create a service instance svc, err := service.New(cfg) if err != nil { log.Fatal(err) } // Service setup code here // ... // Run the service until SIGINT svc.Run() }
Output:
Example (NewTesting) ¶
This example shows how to instantiate and use a service using testing configuration.
package main import () func main() { // TODO }
Output:
Index ¶
Examples ¶
Constants ¶
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Base ¶
type Base struct {
// contains filtered or unexported fields
}
Base is the base service structure.
func (*Base) BeforeStart ¶
BeforeStart schedules a function to be called before service start.
func (*Base) Run ¶
func (s *Base) Run()
Run starts the service and blocks until SIGINT received. Usually it should be a last call in the `main()`.
type Service ¶
type Service interface { // Config returns configuration. Config() config.Config // Router returns router. Router() *mux.Router // Server returns HTTP server. Server() *http.Server // BeforeStart schedules a function to be called before service start. BeforeStart(fn func(svc Service)) // AfterStop schedules a function to be called after service stop. AfterStop(fn func(svc Service)) // Start starts the service. Assumed to be called in a goroutine. Start() // Stop stops the service. Stop() // Run starts the service and blocks until SIGINT received. Usually it should be a last call in the `main()`. Run() }
Service is the service interface.