commit f3ab0096fcbaaed2cdb55fd116de481cefe18dcd Author: Zeev Diukman Date: Wed Mar 5 09:32:28 2025 +0000 1st diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3c1a835 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/zeevdiukman/go-server + +go 1.24.0 diff --git a/server.go b/server.go new file mode 100644 index 0000000..99b20ac --- /dev/null +++ b/server.go @@ -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 +}