64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"strconv"
|
|
)
|
|
|
|
func main() {
|
|
var conf_file string
|
|
|
|
// Parse args and read config
|
|
flag.StringVar(&conf_file, "c", "", "Path to config file")
|
|
flag.Parse()
|
|
config, err := getConfig(conf_file)
|
|
if err != nil {
|
|
fmt.Println("Error reading config file " + conf_file)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 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 + ".")
|
|
os.Exit(2)
|
|
}
|
|
defer logfile.Close()
|
|
|
|
errs := make(chan error, 2)
|
|
// Start the HTTP server, which redirect all incoming connections to HTTPS
|
|
http.HandleFunc("/", LoggingWrapper(logfile, GetHandler(config)))
|
|
http_server := &http.Server{Addr: ":"+strconv.Itoa(config.HttpPort), Handler: http.HandlerFunc(redirectTLS)}
|
|
go func() {
|
|
errs <- http_server.ListenAndServe()
|
|
}()
|
|
|
|
// Start the HTTPS server which actually handles most traffic.
|
|
https_server := &http.Server{Addr: ":"+strconv.Itoa(config.HttpsPort), Handler: nil}
|
|
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)
|
|
signal.Notify(stop, os.Interrupt)
|
|
|
|
// Wait for a signal or an error
|
|
select {
|
|
case <-stop:
|
|
fmt.Println("Shutting down!")
|
|
http_server.Shutdown(context.Background())
|
|
https_server.Shutdown(context.Background())
|
|
case err := <-errs:
|
|
log.Fatal(err)
|
|
}
|
|
|
|
}
|