blob: dd8151a4fa780b390a28ee9dea56bc76a74bfca9 [file] [log] [blame]
iomodob892d072021-02-22 00:23:16 +04001package server
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "net/http"
8 "os"
9 "time"
10
iomodo5e0dc112021-03-25 20:49:55 +040011 "github.com/giolekva/pcloud/core/kg/api/rest"
iomodo3b0b9512021-03-15 00:13:06 +040012 "github.com/giolekva/pcloud/core/kg/common"
iomodob892d072021-02-22 00:23:16 +040013 "github.com/giolekva/pcloud/core/kg/log"
14 "github.com/giolekva/pcloud/core/kg/model"
15 "github.com/giolekva/pcloud/core/kg/store"
16 "github.com/gorilla/mux"
17)
18
19// HTTPServerImpl http server implementation
20type HTTPServerImpl struct {
iomodo5e0dc112021-03-25 20:49:55 +040021 Log common.LoggerIface
22 srv *http.Server
23 routers *rest.Routers
24 config *model.Config
25 store store.Store
iomodob892d072021-02-22 00:23:16 +040026}
27
28var _ Server = &HTTPServerImpl{}
29
30// NewHTTPServer creates new HTTP Server
iomodo3b0b9512021-03-15 00:13:06 +040031func NewHTTPServer(logger common.LoggerIface, config *model.Config, store store.Store) Server {
iomodob892d072021-02-22 00:23:16 +040032 a := &HTTPServerImpl{
iomodo5e0dc112021-03-25 20:49:55 +040033 Log: logger,
34 routers: rest.NewRouter(mux.NewRouter()),
35 config: config,
36 store: store,
iomodob892d072021-02-22 00:23:16 +040037 }
38
39 pwd, _ := os.Getwd()
40 a.Log.Info("HTTP server current working", log.String("directory", pwd))
41 return a
42}
43
44// Start method starts a http server
45func (a *HTTPServerImpl) Start() error {
46 a.Log.Info("Starting HTTP Server...")
47
48 a.srv = &http.Server{
iomodoc0479a62021-02-22 20:08:36 +040049 Addr: fmt.Sprintf("%s:%d", a.config.HTTP.Host, a.config.HTTP.Port),
iomodo5e0dc112021-03-25 20:49:55 +040050 Handler: a.routers.Root,
iomodoc0479a62021-02-22 20:08:36 +040051 ReadTimeout: time.Duration(a.config.HTTP.ReadTimeout) * time.Second,
52 WriteTimeout: time.Duration(a.config.HTTP.WriteTimeout) * time.Second,
53 IdleTimeout: time.Duration(a.config.HTTP.IdleTimeout) * time.Second,
iomodob892d072021-02-22 00:23:16 +040054 }
55
iomodoc0479a62021-02-22 20:08:36 +040056 a.Log.Info("HTTP Server is listening on", log.Int("port", a.config.HTTP.Port))
iomodob892d072021-02-22 00:23:16 +040057 if err := a.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
iomodoc0479a62021-02-22 20:08:36 +040058 a.Log.Error("Failed to listen and serve: %v", log.Err(err))
iomodob892d072021-02-22 00:23:16 +040059 return err
60 }
61 return nil
62}
63
64// Shutdown method shuts http server down
65func (a *HTTPServerImpl) Shutdown() error {
66 a.Log.Info("Stopping HTTP Server...")
67 if a.srv == nil {
iomodoc0479a62021-02-22 20:08:36 +040068 return errors.New("No http server present")
iomodob892d072021-02-22 00:23:16 +040069 }
70
71 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
72 defer cancel()
73 if err := a.srv.Shutdown(ctx); err != nil {
74 a.Log.Error("Unable to shutdown server", log.Err(err))
75 }
76
77 // a.srv.Close()
78 // a.srv = nil
79 a.Log.Info("HTTP Server stopped")
80 return nil
81}