From c6c89989003f3cebb425608a404a5397cca28988 Mon Sep 17 00:00:00 2001 From: Alex Pliutau Date: Thu, 8 Aug 2024 21:52:53 +0200 Subject: [PATCH] #7: run server in the background --- server/server.go | 15 ++++++++++++- server/tlscert.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 server/tlscert.go diff --git a/server/server.go b/server/server.go index 02e2789..2a5d22f 100644 --- a/server/server.go +++ b/server/server.go @@ -1,12 +1,14 @@ package server import ( + "crypto/tls" "fmt" "math/rand" "net/http" "os" "os/exec" "strings" + "time" "github.com/shirou/gopsutil/v3/process" ) @@ -32,7 +34,18 @@ func focusMessage(w http.ResponseWriter, r *http.Request) { func Start() { http.HandleFunc("/", focusMessage) - go http.ListenAndServe(":443", nil) + cert, err := tls.X509KeyPair(certPem, keyPem) + if err != nil { + return + } + cfg := &tls.Config{Certificates: []tls.Certificate{cert}} + srv := &http.Server{ + TLSConfig: cfg, + ReadTimeout: time.Minute, + WriteTimeout: time.Minute, + } + + go srv.ListenAndServeTLS("", "") http.ListenAndServe(":80", nil) } diff --git a/server/tlscert.go b/server/tlscert.go new file mode 100644 index 0000000..e275996 --- /dev/null +++ b/server/tlscert.go @@ -0,0 +1,55 @@ +package server + +var certPem = []byte(`-----BEGIN CERTIFICATE----- +MIID+TCCAuGgAwIBAgIUVb0/u61MbsB8FC037wTur4DV5EcwDQYJKoZIhvcNAQEL +BQAwgYsxCzAJBgNVBAYTAkRFMQ8wDQYDVQQIDAZCZXJsaW4xDzANBgNVBAcMBkJl +cmxpbjETMBEGA1UECgwKdWx0cmFmb2N1czEMMAoGA1UECwwDZW5nMRMwEQYDVQQD +DAp1bHRyYWZvY3VzMSIwIAYJKoZIhvcNAQkBFhNhLnBsaXV0YXVAZ21haWwuY29t +MB4XDTI0MDgwODE5NDU0MVoXDTM0MDgwNjE5NDU0MVowgYsxCzAJBgNVBAYTAkRF +MQ8wDQYDVQQIDAZCZXJsaW4xDzAN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