| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame] | 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "os" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/giolekva/pcloud/core/kg/log" |
| 12 | "github.com/giolekva/pcloud/core/kg/model" |
| 13 | "github.com/giolekva/pcloud/core/kg/store" |
| 14 | "github.com/gorilla/mux" |
| 15 | ) |
| 16 | |
| 17 | // HTTPServerImpl http server implementation |
| 18 | type HTTPServerImpl struct { |
| 19 | Log *log.Logger |
| 20 | srv *http.Server |
| 21 | root *mux.Router |
| 22 | config *model.Config |
| 23 | store store.Store |
| 24 | } |
| 25 | |
| 26 | var _ Server = &HTTPServerImpl{} |
| 27 | |
| 28 | // NewHTTPServer creates new HTTP Server |
| 29 | func NewHTTPServer(logger *log.Logger, config *model.Config, store store.Store) Server { |
| 30 | a := &HTTPServerImpl{ |
| 31 | Log: logger, |
| 32 | root: mux.NewRouter(), |
| 33 | config: config, |
| 34 | store: store, |
| 35 | } |
| 36 | |
| 37 | pwd, _ := os.Getwd() |
| 38 | a.Log.Info("HTTP server current working", log.String("directory", pwd)) |
| 39 | return a |
| 40 | } |
| 41 | |
| 42 | // Start method starts a http server |
| 43 | func (a *HTTPServerImpl) Start() error { |
| 44 | a.Log.Info("Starting HTTP Server...") |
| 45 | |
| 46 | a.srv = &http.Server{ |
| iomodo | c0479a6 | 2021-02-22 20:08:36 +0400 | [diff] [blame] | 47 | Addr: fmt.Sprintf("%s:%d", a.config.HTTP.Host, a.config.HTTP.Port), |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame] | 48 | Handler: a.root, |
| iomodo | c0479a6 | 2021-02-22 20:08:36 +0400 | [diff] [blame] | 49 | ReadTimeout: time.Duration(a.config.HTTP.ReadTimeout) * time.Second, |
| 50 | WriteTimeout: time.Duration(a.config.HTTP.WriteTimeout) * time.Second, |
| 51 | IdleTimeout: time.Duration(a.config.HTTP.IdleTimeout) * time.Second, |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame] | 52 | } |
| 53 | |
| iomodo | c0479a6 | 2021-02-22 20:08:36 +0400 | [diff] [blame] | 54 | a.Log.Info("HTTP Server is listening on", log.Int("port", a.config.HTTP.Port)) |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame] | 55 | if err := a.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| iomodo | c0479a6 | 2021-02-22 20:08:36 +0400 | [diff] [blame] | 56 | a.Log.Error("Failed to listen and serve: %v", log.Err(err)) |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame] | 57 | return err |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // Shutdown method shuts http server down |
| 63 | func (a *HTTPServerImpl) Shutdown() error { |
| 64 | a.Log.Info("Stopping HTTP Server...") |
| 65 | if a.srv == nil { |
| iomodo | c0479a6 | 2021-02-22 20:08:36 +0400 | [diff] [blame] | 66 | return errors.New("No http server present") |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 70 | defer cancel() |
| 71 | if err := a.srv.Shutdown(ctx); err != nil { |
| 72 | a.Log.Error("Unable to shutdown server", log.Err(err)) |
| 73 | } |
| 74 | |
| 75 | // a.srv.Close() |
| 76 | // a.srv = nil |
| 77 | a.Log.Info("HTTP Server stopped") |
| 78 | return nil |
| 79 | } |