serverlib

package module
v0.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2024 License: MIT Imports: 12 Imported by: 0

README

serverlib

Serverlib is a small GO module that lets you quickly create an http server.

It includes :

  • A session manager
  • Template management

Sessions are injected into the request context, making them easy to use.

This module is by no means a web framework, it's only used for my personal projects.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSession added in v0.0.4

func GetSession(w http.ResponseWriter, r *http.Request) (sessions.Session, bool)

GetSession retrieves the session associated with the request's cookie. shorthand for ServerInstance.GetSession(w, r)

func LogDebug

func LogDebug(message string, value string)

LogDebug logs a debug message if the server's log level is set to Debug or higher. It takes two parameters: - message: A string representing the debug message. - value: A string representing additional information to log with the message.

func LogError

func LogError(message string, value string)

LogError logs an error message with a specified value if the server's log level is set to Error or higher. Parameters:

  • message: A string representing the error message to be logged.
  • value: A string representing additional information or context about the error.

func LogInfo

func LogInfo(message string, value string)

LogInfo logs an informational message if the server's log level is set to Info or higher. It takes two parameters: - message: A string representing the message to be logged. - value: A string representing additional information to be logged alongside the message.

Types

type LogLevel

type LogLevel int
const (
	None LogLevel = iota
	Info
	Debug
	Error
)

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents an HTTP server with routing and session management capabilities. It includes an HTTP server, a router for handling HTTP requests, a session manager for managing user sessions, a session key for session security, and a template engine for rendering HTML templates.

var ServerInstance *Server

ServerInstance represents the singleton instance of the server.

func NewServer

func NewServer(config ...ServerConfig) *Server

NewServer creates a new instance of Server with the provided configuration. If no configuration is provided, it uses default settings with an address of ":8080" and a new ServeMux as the handler.

Parameters:

  • config: Optional variadic parameter of type ServerConfig. If provided, the first element is used as the server configuration.

Returns:

  • *Server: A pointer to the newly created Server instance.

func (*Server) AddTemplateSource

func (s *Server) AddTemplateSource(source string)

AddTemplateSource adds a new template source to the server's template manager. The source parameter specifies the template source path to be added.

func (*Server) GetSession

func (s *Server) GetSession(w http.ResponseWriter, r *http.Request) (sessions.Session, bool)

GetSession retrieves the session associated with the request's cookie. If the session does not exist, a new session is created and a new cookie is set.

Parameters:

  • w: The HTTP response writer.
  • r: The HTTP request.

Returns:

  • sessions.Session: The session associated with the request.
  • bool: A boolean indicating whether the session was retrieved (true) or newly created (false).

func (*Server) Handle

func (s *Server) Handle(pattern string, handler http.Handler)

Handle registers a handler to handle HTTP requests with the given pattern.

func (*Server) HandleFunc

func (s *Server) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

HandleFunc registers a function to handle HTTP requests with the given pattern.

func (*Server) Render

func (s *Server) Render(w io.Writer, template string, data map[string]interface{})

Render renders the specified template with the given data and writes the result to the response writer.

func (*Server) SessionKey

func (s *Server) SessionKey() string

SessionKey returns the session key associated with the server instance. This key is used to identify and manage user sessions.

func (*Server) Sessions

func (s *Server) Sessions() sessions.Sessions

SessionManager returns the server's session manager. It provides access to the session manager associated with the server instance.

func (*Server) SetLogLevel

func (s *Server) SetLogLevel(level LogLevel)

SetLogLevel sets the logging level for the server.

Parameters:

level (LogLevel): The desired logging level.

Usage:

server.SetLogLevel(LogLevelDebug)

func (*Server) Start

func (s *Server) Start() error

Start starts the server.

func (*Server) Stop

func (s *Server) Stop() error

Stop stops the server.

func (*Server) Templates

func (s *Server) Templates() *templates.Templates

Templates returns the server's templates. It provides access to the templates associated with the server instance.

type ServerConfig

type ServerConfig struct {
	Address                      string
	DisableGeneralOptionsHandler bool
	TLSConfig                    *tls.Config
	ReadTimeout                  time.Duration
	ReadHeaderTimeout            time.Duration
	WriteTimeout                 time.Duration
	IdleTimeout                  time.Duration
	MaxHeaderBytes               int
	ConnState                    func(net.Conn, http.ConnState)
	ErrorLog                     *log.Logger
	BaseContext                  func(net.Listener) context.Context
	ConnContext                  func(ctx context.Context, c net.Conn) context.Context
	SessionManager               sessions.Sessions
	SessionKey                   string
	DateFormat                   func(time.Time) string
	LogLevel                     LogLevel
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL