1st
This commit is contained in:
commit
f3ab0096fc
2 changed files with 70 additions and 0 deletions
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module github.com/zeevdiukman/go-server
|
||||
|
||||
go 1.24.0
|
||||
67
server.go
Normal file
67
server.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
_Name string
|
||||
_Port int
|
||||
_CertKey [2]string
|
||||
|
||||
*http.Server
|
||||
}
|
||||
|
||||
// New creates a new Server instance.
|
||||
// Returns:
|
||||
// - *Server: a new server instance.
|
||||
func New() *Server {
|
||||
s := &Server{}
|
||||
s.Server = &http.Server{}
|
||||
return s
|
||||
}
|
||||
|
||||
// Name sets the name of the server.
|
||||
// Parameters:
|
||||
// - name: the name to set for the server.
|
||||
// Returns:
|
||||
// - *Server: the server instance with the updated name.
|
||||
func (s *Server) Name(name string) *Server {
|
||||
s._Name = name
|
||||
return s
|
||||
}
|
||||
|
||||
// Port sets the port for the server.
|
||||
// Parameters:
|
||||
// - port: the port number to set for the server.
|
||||
// Returns:
|
||||
// - *Server: the server instance with the updated port.
|
||||
func (s *Server) Port(port int) *Server {
|
||||
s._Port = port
|
||||
s.Addr = ":" + strconv.Itoa(port)
|
||||
return s
|
||||
}
|
||||
func (s *Server) CertKey(path string, certFile string, keyFile string) *Server {
|
||||
s._CertKey = [2]string{path + certFile, path + keyFile}
|
||||
return s
|
||||
}
|
||||
|
||||
// ListenAndServeTLS starts the server with TLS encryption.
|
||||
// Parameters:
|
||||
// - certFile: the path to the certificate file.
|
||||
// - keyFile: the path to the key file.
|
||||
// Returns:
|
||||
// - error: an error if the server fails to start.
|
||||
func (s *Server) ListenAndServeTLS() error {
|
||||
certFile := s._CertKey[0]
|
||||
keyFile := s._CertKey[1]
|
||||
return s.Server.ListenAndServeTLS(certFile, keyFile)
|
||||
}
|
||||
func (s *Server) ListenAndServe() error {
|
||||
return s.Server.ListenAndServe()
|
||||
}
|
||||
func (s *Server) Router(router http.Handler) *Server {
|
||||
s.Handler = router
|
||||
return s
|
||||
}
|
||||
Loading…
Reference in a new issue