Swap fmt for log, and allow HTTP only operation.
This commit is contained in:
parent
1b42ddadfe
commit
9a083fcbfd
50
main.go
50
main.go
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"crypto/tls"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -19,9 +18,11 @@ func main() {
|
|||
|
||||
func main_body() int {
|
||||
var conf_file string
|
||||
var http_only bool
|
||||
|
||||
// Parse args and read config
|
||||
flag.StringVar(&conf_file, "c", "", "Path to config file")
|
||||
flag.BoolVar(&http_only, "h", false, "HTTP only")
|
||||
flag.Parse()
|
||||
if conf_file == "" {
|
||||
_, err := os.Stat("/etc/shizaru.conf")
|
||||
|
@ -31,35 +32,50 @@ func main_body() int {
|
|||
}
|
||||
config, err := getConfig(conf_file)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading config file " + conf_file)
|
||||
log.Println("Error reading config file " + conf_file)
|
||||
return 1
|
||||
}
|
||||
https := ! http_only
|
||||
|
||||
// Open logfile
|
||||
logfile, err := os.OpenFile(config.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
fmt.Println("Error opening log file " + config.LogPath + ".")
|
||||
log.Println("Error opening log file " + config.LogPath + ".")
|
||||
return 2
|
||||
}
|
||||
defer logfile.Close()
|
||||
|
||||
errs := make(chan error, 2)
|
||||
// Start the HTTP server, which redirect all incoming connections to HTTPS
|
||||
// Configure HTTP and HTTPS servers
|
||||
// By default, all the HTTP server does is redirect everything to HTTPS.
|
||||
// Alternatively, serve *only* on HTTP, for use behind nginx or similar.
|
||||
var http_server *http.Server
|
||||
var https_server *http.Server
|
||||
http.HandleFunc("/", LoggingWrapper(logfile, GetHandler(config)))
|
||||
http_server := &http.Server{Addr: ":"+strconv.Itoa(config.HttpPort), Handler: http.HandlerFunc(GetRedirectTLSHandler(config))}
|
||||
if(http_only) {
|
||||
http_server = &http.Server{Addr: ":"+strconv.Itoa(config.HttpPort), Handler: nil}
|
||||
} else {
|
||||
http_server = &http.Server{Addr: ":"+strconv.Itoa(config.HttpPort), Handler: http.HandlerFunc(GetRedirectTLSHandler(config))}
|
||||
tlscfg := &tls.Config{
|
||||
MinVersion: tls.VersionTLS10,
|
||||
}
|
||||
https_server = &http.Server{Addr: ":"+strconv.Itoa(config.HttpsPort), Handler: nil, TLSConfig: tlscfg}
|
||||
}
|
||||
|
||||
// Start HTTP server
|
||||
errs := make(chan error, 2)
|
||||
go func() {
|
||||
errs <- http_server.ListenAndServe()
|
||||
}()
|
||||
|
||||
tlscfg := &tls.Config{
|
||||
MinVersion: tls.VersionTLS10,
|
||||
// Start HTTPS server
|
||||
if(https) {
|
||||
go func() {
|
||||
errs <- https_server.ListenAndServe()
|
||||
}()
|
||||
log.Println("Listening on ports " + strconv.Itoa(config.HttpPort) + " and " + strconv.Itoa(config.HttpsPort) + "...")
|
||||
} else {
|
||||
log.Println("Listening on port " + strconv.Itoa(config.HttpPort) + "...")
|
||||
}
|
||||
// Start the HTTPS server which actually handles most traffic.
|
||||
https_server := &http.Server{Addr: ":"+strconv.Itoa(config.HttpsPort), Handler: nil, TLSConfig: tlscfg}
|
||||
go func() {
|
||||
errs <- https_server.ListenAndServeTLS(config.CertPath, config.KeyPath)
|
||||
}()
|
||||
fmt.Println("Listening on ports " + strconv.Itoa(config.HttpPort) + " and " + strconv.Itoa(config.HttpsPort) + "...")
|
||||
|
||||
// Listen for signals to gracefully shutdown
|
||||
stop := make(chan os.Signal, 1)
|
||||
|
@ -69,9 +85,11 @@ func main_body() int {
|
|||
// Wait for a signal or an error
|
||||
select {
|
||||
case <-stop:
|
||||
fmt.Println("Shutting down!")
|
||||
log.Println("Shutting down!")
|
||||
http_server.Shutdown(context.Background())
|
||||
https_server.Shutdown(context.Background())
|
||||
if(https) {
|
||||
https_server.Shutdown(context.Background())
|
||||
}
|
||||
case err := <-errs:
|
||||
log.Println("Fatal: " + err.Error())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue