Added tests, added note when port in use during setup, added port env variable to documentation

This commit is contained in:
Marc Ole Bulling
2022-10-29 00:02:21 +02:00
parent 510ee9d493
commit c5c01b9bab
4 changed files with 63 additions and 1 deletions

View File

@@ -73,6 +73,8 @@ Available environment variables
| GOKAPI_MAX_MEMORY_UPLOAD | Sets the amount of RAM in MB that can be allocated for an upload. | Yes | 20 |
| | Any upload with a size greater than that will be written to a temporary file | | |
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
| GOKAPI_PORT | Sets the webserver port | Yes | 53842 |
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
| TMPDIR | Sets the path which contains temporary files | No | Non-Docker: Default OS path |
| | | | Docker: [DATA_DIR] |
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+

View File

@@ -23,8 +23,11 @@ import (
"log"
"net"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"syscall"
"time"
)
@@ -121,6 +124,9 @@ func startSetupWebserver() {
fmt.Println("Please open http://" + resolveHostIp() + ":" + port + "/setup to setup Gokapi.")
listener, err := net.Listen("tcp", ":"+port)
if err != nil {
if isErrorAddressAlreadyInUse(err) {
fmt.Println("This port is already in use. Use parameter -p or env variable GOKAPI_PORT to change the port.")
}
log.Fatalf("Setup Webserver: %v", err)
}
if statusChannel != nil {
@@ -139,6 +145,25 @@ func startSetupWebserver() {
}
}
func isErrorAddressAlreadyInUse(err error) bool {
var eOsSyscall *os.SyscallError
if !errors.As(err, &eOsSyscall) {
return false
}
var errErrno syscall.Errno
if !errors.As(eOsSyscall, &errErrno) {
return false
}
if errErrno == syscall.EADDRINUSE {
return true
}
const WSAEADDRINUSE = 10048
if runtime.GOOS == "windows" && errErrno == WSAEADDRINUSE {
return true
}
return false
}
func resolveHostIp() string {
netInterfaceAddresses, err := net.InterfaceAddrs()
if err != nil {

View File

@@ -1 +0,0 @@
package flagparser

View File

@@ -0,0 +1,36 @@
package models
import "testing"
func TestE2EInfoEncrypted_HasBeenSetUp(t *testing.T) {
type fields struct {
Version int
Nonce []byte
Content []byte
AvailableFiles []string
}
tests := []struct {
name string
fields fields
want bool
}{
{"empty", fields{}, false},
{"version 0", fields{Version: 0}, false},
{"version 1, empty", fields{Version: 1}, false},
{"version 0, not empty", fields{Version: 0, Content: []byte("content")}, false},
{"version 1, not empty", fields{Version: 1, Content: []byte("content")}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &E2EInfoEncrypted{
Version: tt.fields.Version,
Nonce: tt.fields.Nonce,
Content: tt.fields.Content,
AvailableFiles: tt.fields.AvailableFiles,
}
if got := e.HasBeenSetUp(); got != tt.want {
t.Errorf("HasBeenSetUp() = %v, want %v", got, tt.want)
}
})
}
}