main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package main
  26. import (
  27. "fmt"
  28. "log"
  29. "os"
  30. "os/signal"
  31. "syscall"
  32. sasl "git.semlanik.org/semlanik/gostfix/sasl"
  33. scanner "git.semlanik.org/semlanik/gostfix/scanner"
  34. service "git.semlanik.org/semlanik/gostfix/service"
  35. web "git.semlanik.org/semlanik/gostfix/web"
  36. "github.com/pkg/profile"
  37. )
  38. type GostfixEngine struct {
  39. services []service.NanoService
  40. stats []*service.NanoServiceStats
  41. }
  42. func NewGostfixEngine() (e *GostfixEngine) {
  43. mailScanner := scanner.NewMailScanner()
  44. saslService, err := sasl.NewSaslServer()
  45. if err != nil {
  46. log.Fatalf("Unable to intialize sasl server %s\n", err)
  47. }
  48. webServer := web.NewServer(mailScanner, e)
  49. e = &GostfixEngine{}
  50. e.services = append(e.services, saslService, mailScanner, webServer)
  51. e.stats = make([]*service.NanoServiceStats, len(e.services))
  52. return
  53. }
  54. func (e *GostfixEngine) Run() {
  55. done := make(chan bool, 1)
  56. for i, s := range e.services {
  57. if e.stats[i] == nil {
  58. e.stats[i] = &service.NanoServiceStats{}
  59. }
  60. go func(s service.NanoService, stats *service.NanoServiceStats) {
  61. stats.Name = s.ServiceName()
  62. stats.Status = service.NanoServiceStatus_NanoServiceRunning
  63. log.Printf("Running %s", s.ServiceName())
  64. s.Run()
  65. stats.Status = service.NanoServiceStatus_NanoServiceStopped
  66. log.Printf("%s is stopped", s.ServiceName())
  67. done <- true
  68. }(s, e.stats[i])
  69. }
  70. sigs := make(chan os.Signal, 1)
  71. signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
  72. go func() {
  73. sig := <-sigs
  74. log.Printf("Exit by signal %v", sig)
  75. log.Printf("Server is stopping")
  76. for i, s := range e.services {
  77. if e.stats[i] != nil && e.stats[i].Status == service.NanoServiceStatus_NanoServiceRunning {
  78. s.Stop()
  79. }
  80. }
  81. }()
  82. log.Printf("Server is running succesfully")
  83. for i := 0; i < len(e.services); i++ {
  84. <-done
  85. }
  86. fmt.Printf("Server shutdown!!!!")
  87. log.Printf("Server shutdown")
  88. }
  89. func (e *GostfixEngine) ReadStats() []*service.NanoServiceStats {
  90. return e.stats
  91. }
  92. func main() {
  93. defer profile.Start().Stop()
  94. engine := NewGostfixEngine()
  95. engine.Run()
  96. }