This commit is contained in:
qowevisa 2024-02-23 19:02:43 +02:00
parent 9cdec27808
commit 6571dc20c0
3 changed files with 42 additions and 23 deletions

View File

@ -2,7 +2,6 @@ package main
import ( import (
"bufio" "bufio"
// "crypto/rand"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"log" "log"
@ -27,39 +26,28 @@ func main() {
log.Fatalf("server: loadkeys: %s", err) log.Fatalf("server: loadkeys: %s", err)
} }
config := tls.Config{Certificates: []tls.Certificate{cert}} config := tls.Config{Certificates: []tls.Certificate{cert}}
host, err := env.GetHost()
if err != nil {
panic(err)
}
port, err := env.GetPort()
if err != nil {
panic(err)
}
srv := http.Server{ srv := http.Server{
Addr: fmt.Sprintf("127.0.0.1:%d", env.Port), Addr: fmt.Sprintf("%s:%d", host, port),
TLSConfig: &config, TLSConfig: &config,
ReadTimeout: time.Minute, ReadTimeout: time.Minute,
WriteTimeout: time.Minute, WriteTimeout: time.Minute,
Handler: http.HandlerFunc(handle),
} }
srv.Handler = http.HandlerFunc(handle) log.Printf("Start http server on %s:%d\n", host, port)
err = srv.ListenAndServe() err = srv.ListenAndServe()
if err != nil { if err != nil {
log.Fatalf("Failed to start server: %v", err) log.Fatalf("Failed to start server: %v", err)
os.Exit(1) os.Exit(1)
} }
defer srv.Close() defer srv.Close()
// listener, err := tls.Listen("tcp", url, &config)
// if err != nil {
// log.Fatal(err)
// }
// defer listener.Close()
// fmt.Println("Server is listening on port 8080")
//
// for {
// // Wait for a connection.
// conn, err := srv.Accept()
// if err != nil {
// log.Print(err)
// continue
// }
// // Handle the connection in a new goroutine.
// // The loop then returns to accepting, so that
// // multiple connections may be served concurrently.
// go handleConnection(conn)
// }
} }
func handleConnection(conn net.Conn) { func handleConnection(conn net.Conn) {

24
env/env.go vendored
View File

@ -1,6 +1,30 @@
package env package env
import (
"os"
"strconv"
"git.qowevisa.me/Qowevisa/gotell/errors"
)
const ( const (
Port = 2993 Port = 2993
ConnectPort = 443 ConnectPort = 443
) )
func GetHost() (string, error) {
host := os.Getenv("GOTELL_HOST")
if host == "" {
return host, errors.ENV_EMPTY
}
return host, nil
}
func GetPort() (int, error) {
portStr := os.Getenv("GOTELL_PORT")
if portStr == "" {
return 0, errors.ENV_EMPTY
}
port, err := strconv.ParseInt(portStr, 10, 32)
return int(port), err
}

7
errors/env.go Normal file
View File

@ -0,0 +1,7 @@
package errors
import "errors"
var (
ENV_EMPTY = errors.New("Environment variable was empty")
)