Package HTTPServer:

- Add: error for invalid address check
- Move: errors string InvalidInstance as golib/error type
- Update: add check of listen address in port use function
This commit is contained in:
nabbar
2025-12-22 13:08:08 +01:00
parent e89b03987d
commit 8fabc2f5e1
2 changed files with 13 additions and 5 deletions

View File

@@ -34,9 +34,11 @@ import (
const (
ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgHttpServer
ErrorInvalidInstance
ErrorHTTP2Configure
ErrorServerValidate
ErrorServerStart
ErrorInvalidAddress
ErrorPortUse
)
@@ -51,12 +53,16 @@ func getMessage(code liberr.CodeError) (message string) {
switch code {
case ErrorParamEmpty:
return "given parameters is empty"
case ErrorInvalidInstance:
return "invalid instance"
case ErrorHTTP2Configure:
return "cannot initialize http2 over http srv"
case ErrorServerValidate:
return "config srv seems to be not valid"
case ErrorServerStart:
return "server killed : server start but not listen"
case ErrorInvalidAddress:
return "address seems to be invalid"
case ErrorPortUse:
return "srv port is still used"
}

View File

@@ -28,7 +28,6 @@ package httpserver
import (
"context"
"errors"
"fmt"
"log"
"net"
@@ -43,8 +42,6 @@ import (
libsrv "github.com/nabbar/golib/runner"
)
var errInvalid = errors.New("invalid instance")
func (o *srv) getServer() *http.Server {
if o == nil || o.s == nil {
return nil
@@ -65,7 +62,7 @@ func (o *srv) delServer() {
func (o *srv) setServer(ctx context.Context) error {
if o == nil || o.s == nil {
return errInvalid
return ErrorInvalidInstance.Error()
}
var (
@@ -137,7 +134,7 @@ func (o *srv) Start(ctx context.Context) error {
func (o *srv) Stop(ctx context.Context) error {
if o == nil || o.s == nil || o.r == nil {
return errInvalid
return ErrorInvalidInstance.Error()
}
r := o.r.Load()
@@ -222,6 +219,11 @@ func (o *srv) PortNotUse(ctx context.Context, listen string) error {
if strings.Contains(listen, ":") {
part := strings.Split(listen, ":")
if len(part) < 2 {
return ErrorInvalidAddress.Error()
}
port := part[len(part)-1]
addr := strings.Join(part[:len(part)-1], ":")