Define common application exit codes for backend executables in Exit.go.

Improve logging of webapi start.
This commit is contained in:
Kleissner
2021-11-15 03:00:24 +01:00
parent 9626fa8088
commit d06c2e0b1a
6 changed files with 37 additions and 12 deletions

View File

@@ -26,6 +26,6 @@ func initUserBlockchain() {
if err != nil {
Filters.LogError("initUserBlockchain", "error: %s\n", err.Error())
os.Exit(1)
os.Exit(ExitBlockchainCorrupt)
}
}

View File

@@ -16,7 +16,7 @@ import (
)
// Version is the current core library version
const Version = "Alpha 4/05.11.2021"
const Version = "Alpha 4/15.11.2021"
var config struct {
LogFile string `yaml:"LogFile"` // Log file

23
Exit.go Normal file
View File

@@ -0,0 +1,23 @@
/*
File Name: Exit.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package core
// Exit codes signal why the application exited. These are universal between clients developed by the Peernet organization.
// Clients are encouraged to log additional details in a log file. 3rd party clients may define additional exit codes.
const (
ExitSuccess = 0 // This is actually never used.
ExitErrorConfigAccess = 1 // Error accessing the config file.
ExitErrorConfigRead = 2 // Error reading the config file.
ExitErrorConfigParse = 3 // Error parsing the config file.
ExitErrorLogInit = 4 // Error initializing log file.
ExitParamWebapiInvalid = 5 // Parameter for webapi is invalid.
ExitPrivateKeyCorrupt = 6 // Private key is corrupt.
ExitPrivateKeyCreate = 7 // Cannot create a new private key.
ExitBlockchainCorrupt = 8 // Blockchain is corrupt.
ExitGraceful = 9 // Graceful shutdown.
STATUS_CONTROL_C_EXIT = 0xC000013A // The application terminated as a result of a CTRL+C. This is a Windows NTSTATUS value.
)

View File

@@ -42,7 +42,7 @@ func initPeerID() {
}
Filters.LogError("initPeerID", "private key in config is corrupted! Error: %s\n", err.Error())
os.Exit(1)
os.Exit(ExitPrivateKeyCorrupt)
}
// if the peer ID is empty, create a new user public-private key pair
@@ -50,7 +50,7 @@ func initPeerID() {
peerPrivateKey, peerPublicKey, err = Secp256k1NewPrivateKey()
if err != nil {
Filters.LogError("initPeerID", "generating public-private key pairs: %s\n", err.Error())
os.Exit(1)
os.Exit(ExitPrivateKeyCreate)
}
nodeID = protocol.PublicKey2NodeID(peerPublicKey)

View File

@@ -21,7 +21,7 @@ import (
func init() {
if status, err := core.LoadConfig("Config.yaml"); err != nil {
fmt.Printf("Error loading config file: %s", err.Error())
os.Exit(1)
os.Exit(core.ExitErrorConfigAccess)
}
core.InitLog()

View File

@@ -10,10 +10,10 @@ import (
"crypto/tls"
"encoding/json"
"errors"
"log"
"net/http"
"time"
"github.com/PeernetOfficial/core"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
@@ -73,13 +73,15 @@ func Start(ListenAddresses []string, UseSSL bool, CertificateFile, CertificateKe
Router.HandleFunc("/file/view", apiFileView).Methods("GET")
for _, listen := range ListenAddresses {
go startWebServer(listen, UseSSL, CertificateFile, CertificateKey, Router, "API", TimeoutRead, TimeoutWrite)
go startWebAPI(listen, UseSSL, CertificateFile, CertificateKey, Router, "API", TimeoutRead, TimeoutWrite)
}
}
// startWebServer starts a web-server with given parameters and logs the status. If may block forever and only returns if there is an error.
// startWebAPI starts a web-server with given parameters and logs the status. If may block forever and only returns if there is an error.
// The certificate file and key are only used if SSL is enabled. The read and write timeout may be 0 for no timeout.
func startWebServer(WebListen string, UseSSL bool, CertificateFile, CertificateKey string, Handler http.Handler, Info string, ReadTimeout, WriteTimeout time.Duration) {
func startWebAPI(WebListen string, UseSSL bool, CertificateFile, CertificateKey string, Handler http.Handler, Info string, ReadTimeout, WriteTimeout time.Duration) {
core.Filters.LogError("startWebAPI", "Start API at '%s'\n", WebListen)
tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12} // for security reasons disable TLS 1.0/1.1
server := &http.Server{
@@ -94,12 +96,12 @@ func startWebServer(WebListen string, UseSSL bool, CertificateFile, CertificateK
if UseSSL {
// HTTPS
if err := server.ListenAndServeTLS(CertificateFile, CertificateKey); err != nil {
log.Printf("Error listening on '%s': %v\n", WebListen, err)
core.Filters.LogError("startWebAPI", "Error listening on '%s': %v\n", WebListen, err)
}
} else {
// HTTP
if err := server.ListenAndServe(); err != nil {
log.Printf("Error listening on '%s': %v\n", WebListen, err)
core.Filters.LogError("startWebAPI", "Error listening on '%s': %v\n", WebListen, err)
}
}
}
@@ -110,7 +112,7 @@ func EncodeJSON(w http.ResponseWriter, r *http.Request, data interface{}) (err e
err = json.NewEncoder(w).Encode(data)
if err != nil {
log.Printf("Error writing data for route '%s': %v\n", r.URL.Path, err)
core.Filters.LogError("EncodeJSON", "Error writing data for route '%s': %v\n", r.URL.Path, err)
}
return err