Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate server creation, closes #46
- Loading branch information
Showing
3 changed files
with
82 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/cardigann/cardigann/config" | ||
"github.com/cardigann/cardigann/logger" | ||
) | ||
|
||
// Server is an http server which wraps the Handler | ||
type Server struct { | ||
Bind, Port, Passphrase string | ||
version string | ||
config config.Config | ||
} | ||
|
||
func New(conf config.Config, version string) (*Server, error) { | ||
bind, err := config.GetGlobalConfig("bind", "0.0.0.0", conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
port, err := config.GetGlobalConfig("port", "5060", conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
passphrase, err := config.GetGlobalConfig("passphrase", "", conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if envPort := os.Getenv("PORT"); envPort != "" { | ||
port = envPort | ||
} | ||
|
||
if version == "" { | ||
version = "dev" | ||
} | ||
|
||
return &Server{ | ||
Bind: bind, | ||
Port: port, | ||
Passphrase: passphrase, | ||
config: conf, | ||
version: version, | ||
}, nil | ||
} | ||
|
||
func (s *Server) Listen() error { | ||
logger.Logger.Infof("Cardigann %s", s.version) | ||
|
||
listenOn := fmt.Sprintf("%s:%s", s.Bind, s.Port) | ||
logger.Logger.Infof("Listening on %s", listenOn) | ||
|
||
h, err := NewHandler(Params{ | ||
Passphrase: s.Passphrase, | ||
Config: s.config, | ||
Version: s.version, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return http.ListenAndServe(listenOn, h) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters