@@ -6,15 +6,13 @@ import (
66 "net/http"
77 "os"
88 "path/filepath"
9-
9+
1010 "github.com/lightninglabs/kirin/auth"
1111 "github.com/lightninglabs/kirin/proxy"
12+ "github.com/lightningnetwork/lnd/build"
1213 "gopkg.in/yaml.v2"
1314)
1415
15- /**
16- */
17-
1816// Main is the true entrypoint of Kirin.
1917func Main () {
2018 // TODO: Prevent from running twice.
@@ -25,22 +23,21 @@ func Main() {
2523 }
2624}
2725
26+ // start sets up the proxy server and runs it. This function blocks until a
27+ // shutdown signal is received.
2828func start () error {
29+ // First, parse configuration file and set up logging.
2930 configFile := filepath .Join (kirinDataDir , defaultConfigFilename )
30- var cfg config
31- b , err := ioutil .ReadFile (configFile )
31+ cfg , err := getConfig (configFile )
3232 if err != nil {
33- return err
33+ return fmt . Errorf ( "unable to parse config file: %v" , err )
3434 }
35-
36- err = yaml .Unmarshal (b , & cfg )
35+ err = setupLogging (cfg )
3736 if err != nil {
38- return err
39- }
40- if cfg .ListenAddr == "" {
41- return fmt .Errorf ("missing listen address for server" )
37+ return fmt .Errorf ("unable to set up logging: %v" , err )
4238 }
4339
40+ // Create the auxiliary services the proxy needs to work.
4441 authenticator , err := auth .NewLndAuthenticator (cfg .Authenticator )
4542 if err != nil {
4643 return err
@@ -50,13 +47,68 @@ func start() error {
5047 return err
5148 }
5249
53- // Start the reverse proxy.
50+ // Finally start the reverse proxy.
5451 server := & http.Server {
5552 Addr : cfg .ListenAddr ,
5653 Handler : http .HandlerFunc (servicesProxy .ServeHTTP ),
5754 }
58-
5955 tlsKeyFile := filepath .Join (kirinDataDir , defaultTLSKeyFilename )
6056 tlsCertFile := filepath .Join (kirinDataDir , defaultTLSCertFilename )
57+
58+ // The ListenAndServeTLS below will block until shut down or an error
59+ // occurs. So we can just defer a cleanup function here that will close
60+ // everything on shutdown.
61+ defer cleanup (server )
6162 return server .ListenAndServeTLS (tlsCertFile , tlsKeyFile )
6263}
64+
65+ // getConfig loads and parses the configuration file then checks it for valid
66+ // content.
67+ func getConfig (configFile string ) (* config , error ) {
68+ cfg := & config {}
69+ b , err := ioutil .ReadFile (configFile )
70+ if err != nil {
71+ return nil , err
72+ }
73+ err = yaml .Unmarshal (b , cfg )
74+ if err != nil {
75+ return nil , err
76+ }
77+
78+ // Then check the configuration that we got from the config file, all
79+ // required values need to be set at this point.
80+ if cfg .ListenAddr == "" {
81+ return nil , fmt .Errorf ("missing listen address for server" )
82+ }
83+ return cfg , nil
84+ }
85+
86+ // setupLogging parses the debug level and initializes the log file rotator.
87+ func setupLogging (cfg * config ) error {
88+ if cfg .DebugLevel == "" {
89+ cfg .DebugLevel = defaultLogLevel
90+ }
91+
92+ // Now initialize the logger and set the log level.
93+ logFile := filepath .Join (kirinDataDir , defaultLogFilename )
94+ err := logWriter .InitLogRotator (
95+ logFile , defaultMaxLogFileSize , defaultMaxLogFiles ,
96+ )
97+ if err != nil {
98+ return err
99+ }
100+ return build .ParseAndSetDebugLevels (cfg .DebugLevel , logWriter )
101+ }
102+
103+ // cleanup closes the given server and shuts down the log rotator.
104+ func cleanup (server * http.Server ) {
105+ err := server .Close ()
106+ if err != nil {
107+ log .Errorf ("Error closing server: %v" , err )
108+ }
109+ log .Info ("Shutdown complete" )
110+ err = logWriter .Close ()
111+ if err != nil {
112+ log .Errorf ("Could not close log rotator: %v" , err )
113+ }
114+ }
0 commit comments