Skip to content

A simple HTTP server written in Gin framework with example bash scripts and systemd service files to daemonize the process in Linux.

License

Notifications You must be signed in to change notification settings

Mechstud/Daemonize-Webserver-Example

Repository files navigation

Simple Webserver running in background

chmod +x startBackground.sh
  • Run the bash script to start the service in background.
bash startBackground.sh

Test by running following HTTP cURL requests:

curl -X GET "http://127.0.0.1:12345/api/v1/ping"

curl -X POST "http://127.0.0.1:12345/api/v1/datetime"

Simple Webserver running as daemon

  • Create a daemon service file in the /etc/systemd/system folder.

You can either create a file using editor like nano, and then write the contents there:

sudo nano /etc/systemd/system/simplewebserver.service

# Now write/copy the contents from simplewebserver.service file in the root of this repository.

OR, copy the file directly there:

sudo cp simplewebserver.service /etc/systemd/system/simplewebserver.service
  • Ensure that the webserver binary is built
go build -o SimpleWebserver
  • Ensure that the binary is executable
chmod +x SimpleWebserver
  • Start the daemon service
sudo systemctl start simplewebserver
  • Check the status of the daemon service
sudo systemctl status simplewebserver
  • Stop the daemon service
sudo systemctl stop simplewebserver
  • Restart the daemon service
sudo systemctl restart simplewebserver
  • Enable the daemon for Simple Webserver, so that it gets started on OS reboot
sudo systemctl enable simplewebserver
  • Disable the daemon service, so that it does not get started on reboot
sudo systemctl disable simplewebserver
  • In case of any errors, you can debug by checking the logs
sudo journalctl -u simplewebserver # OR, sudo journalctl --unit=simplewebserver

# Or, if you want to tail the log, then use
sudo journalctl -u simplewebserver -f
  • Whenever we manually edit the service file using nano/vim, etc, then we need to ensure the daemon is reload and service is restarted.
sudo systemctl daemon-reload

sudo systemctl restart simplewebserver
  • To edit the service file, without needing a daemon-reload:
sudo systemctl edit simplewebserver --full
  • Get a complete list of systemd service configuration parameters
man systemd.service

OR, visit: https://man7.org/linux/man-pages/man5/systemd.service.5.html

About

A simple HTTP server written in Gin framework with example bash scripts and systemd service files to daemonize the process in Linux.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published