goserver

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2025 License: MIT Imports: 11 Imported by: 0

README

goserver

Project Badges

goserver is a Go package that encapsulates the logic for running a development and production server. It automatically manages static file serving, external Go server compilation, hot-reloading, and process control, making it easy to switch between development and production modes.

Basic Usage Example

Here is a minimal example of how to use goserver in your Go project:

package main

import (
	"fmt"
	"os"
	"sync"
	"github.com/cdvelop/goserver"
)

func main() {
	config := &goserver.ServerConfig{
		RootFolder:               "./web",
		MainFileWithoutExtension: "main.server",
		ArgumentsForCompilingServer: func() []string { return []string{} },
		ArgumentsToRunServer:        func() []string { return []string{} },
		PublicFolder:             "public",
		AppPort:                  "8080",
		Writer:                   os.Stdout,
		ExitChan:                 make(chan bool),
	}

	handler := goserver.NewServerHandler(config)

	var wg sync.WaitGroup
	wg.Add(1)
	go handler.Start(&wg)

	// Wait for server to finish (in real use, handle signals for graceful shutdown)
	wg.Wait()
	fmt.Println("Server stopped.")
}

Public API

Main Types
  • ServerConfig
    Configuration structure for the server.

    • RootFolder: Project root folder.
    • MainFileWithoutExtension: Base name of the main server file (without extension).
    • ArgumentsForCompilingServer: Function returning arguments for compiling the server.
    • ArgumentsToRunServer: Function returning arguments for running the server.
    • PublicFolder: Static files folder.
    • AppPort: Application port.
    • Writer: Output for logs.
    • ExitChan: Channel to signal shutdown.
  • ServerHandler
    Main server handler, created with NewServerHandler(*ServerConfig).

Main Methods
  • NewServerHandler(c *ServerConfig) *ServerHandler
    Creates a new server handler.

  • (*ServerHandler) Start(wg *sync.WaitGroup)
    Starts the server (internal or external) as a goroutine.

  • (*ServerHandler) StartExternalServer() error
    Compiles and runs the external server.

  • (*ServerHandler) StartInternalServerFiles()
    Starts an internal HTTP server for static files.

  • (*ServerHandler) StopInternalServer() error
    Stops the internal server.

  • (*ServerHandler) RestartInternalServer() error
    Restarts the internal server.

  • (*ServerHandler) RestartExternalServer() error
    Restarts the external server (stops, recompiles, and runs).

  • (*ServerHandler) RestartServer() (string, error)
    Restarts the current server (internal or external) and returns a status message.

  • (*ServerHandler) NewFileEvent(fileName, extension, filePath, event string) error
    Handles file events (create, write, etc.) for hot-reload.

  • (*ServerHandler) MainFilePath() string
    Returns the path to the main server file.

  • (*ServerHandler) Name() string
    Returns the server name ("GoServer").

  • (*ServerHandler) UnobservedFiles() []string
    Returns files that should not be watched (executables, temporary files).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ServerConfig

type ServerConfig struct {
	RootFolder                  string          // eg: web
	MainFileWithoutExtension    string          // eg: main.server
	ArgumentsForCompilingServer func() []string // eg: []string{"-X 'main.version=v1.0.0'"}
	ArgumentsToRunServer        func() []string // eg: []string{"dev" }
	PublicFolder                string          // eg: public
	AppPort                     string          // eg : 8080
	Writer                      io.Writer       // For logging output
	ExitChan                    chan bool       // Canal global para señalizar el cierre
}

type ServerHandler

type ServerHandler struct {
	*ServerConfig
	// contains filtered or unexported fields
}

func NewServerHandler

func NewServerHandler(c *ServerConfig) *ServerHandler

func (*ServerHandler) MainFilePath

func (h *ServerHandler) MainFilePath() string

MainFilePath eg: <root>/web/main.server.go

func (*ServerHandler) Name

func (h *ServerHandler) Name() string

func (*ServerHandler) NewFileEvent

func (h *ServerHandler) NewFileEvent(fileName, extension, filePath, event string) error

event: create,write,remove,rename

func (*ServerHandler) RestartExternalServer

func (h *ServerHandler) RestartExternalServer() error

func (*ServerHandler) RestartInternalServer

func (h *ServerHandler) RestartInternalServer() error

func (*ServerHandler) RestartServer

func (h *ServerHandler) RestartServer() (string, error)

RestartServer reinicia el servidor actual (interno o externo) y devuelve un mensaje de estado

func (*ServerHandler) Start

func (h *ServerHandler) Start(wg *sync.WaitGroup)

Start inicia el servidor como goroutine

func (*ServerHandler) StartExternalServer

func (h *ServerHandler) StartExternalServer() error

func (*ServerHandler) StartInternalServerFiles

func (h *ServerHandler) StartInternalServerFiles()

func (*ServerHandler) StopInternalServer

func (h *ServerHandler) StopInternalServer() error

func (*ServerHandler) UnobservedFiles

func (h *ServerHandler) UnobservedFiles() []string

UnobservedFiles returns the list of files that should not be tracked by file watchers eg: main.exe, main_temp.exe

Jump to

Keyboard shortcuts

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