GithubHelp home page GithubHelp logo

arcade-10's Introduction

HTTP Server with Panic Recovery

This Go program demonstrates a simple HTTP server that handles panics and recovers from them. The server listens on port 3000 and responds to three different routes:

  • /: Returns a simple "Hello!" message.
  • /panic/: Triggers a panic and demonstrates how the server recovers from it.
  • /panic-after/: Returns a "Hello!" message and then triggers a panic, demonstrating how the server recovers from it even after sending a response.

The server uses a middleware function to recover from panics and return a 500 Internal Server Error response to the client.

Prerequisites

  • Go 1.16
  • Internet connection

Installation

  • Clone the repository
git clone

Routes

  • /: Returns a simple "Hello!" message.
  • /panic/: Triggers a panic and demonstrates how the server recovers from it.
  • /panic-after/: Returns a "Hello!" message and then triggers a panic, demonstrating how the server recovers from it even after sending a response.

Panic Recovery Mechanism

The recoverMw function is a middleware that wraps the main application handler. It uses a deferred function to catch any panics that occur during the execution of the application handler. If a panic is caught, it logs the error and returns a 500 Internal Server Error response to the client.

Code Organization

The code is organized into the following functions:

  • main: The entry point of the program. It sets up the HTTP server and starts listening on port 3000.
  • recoverMw: The panic recovery middleware.
  • panicDemo: A handler that triggers a panic immediately.
  • panicAfterDemo: A handler that returns a response and then triggers a panic.
  • funcThatPanics: A function that simply panics with a message.
  • hello: A handler that returns a simple "Hello!" message.

Running the Server

To run the server, simply execute the Go program:

go run main.go

Then, open a web browser and navigate to http://localhost:3000/ to see the "Hello!" message. Navigate to http://localhost:3000/panic/ or http://localhost:3000/panic-after/ to trigger a panic and see how the server recovers from it.

Notes

This implementation is a simple demonstration of panic recovery and may not be suitable for production use without additional error handling and logging mechanisms. In a real-world scenario, you would want to handle errors more robustly and provide more informative error messages to the client.

arcade-10's People

Contributors

arnab-afk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.