mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
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:
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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], ":")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user