Text to Speech Speech to Text

http.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package http
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "os"
  5. "os/signal"
  6. "strings"
  7. "syscall"
  8. "git.links123.net/Slate/CorpusAI/cmd/http/router"
  9. "git.links123.net/Slate/CorpusAI/service/store/cache"
  10. "git.links123.net/Slate/CorpusAI/service/store/mysql"
  11. "github.com/braintree/manners"
  12. "github.com/spf13/cobra"
  13. )
  14. // RunCommand cobra subcommand http
  15. func RunCommand() *cobra.Command {
  16. var host, port string
  17. cmd := &cobra.Command{
  18. Use: "http",
  19. Short: "Run the http service",
  20. Run: func(cmd *cobra.Command, args []string) {
  21. go Start(host, port)
  22. // 阻塞退出 捕获信号
  23. signalChan := make(chan os.Signal)
  24. signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
  25. logrus.Infof("caught signal %+v, begin garbage collection", <-signalChan)
  26. Stop()
  27. },
  28. }
  29. cmd.PersistentFlags().StringVarP(&host, "host", "o", "127.0.0.1", "server hostname")
  30. cmd.PersistentFlags().StringVarP(&port, "port", "p", "8080", "server port")
  31. return cmd
  32. }
  33. // Start use for cobra or testing
  34. func Start(host, port string) {
  35. // init db
  36. mysql.Init()
  37. // init cache
  38. cache.Init()
  39. // build router
  40. r := router.BuildRouter()
  41. // start server
  42. err := manners.ListenAndServe(strings.Join([]string{host, port}, ":"), r)
  43. if err != nil {
  44. panic(err)
  45. }
  46. }
  47. // Stop stop the http service graceful
  48. func Stop() {
  49. if manners.Close() {
  50. logrus.Info("http server stopped")
  51. }
  52. cache.Close()
  53. mysql.Close()
  54. }