Giter Site home page Giter Site logo

http-recovery's Introduction

Exercise Go #1: Panic/Recover Middleware

exercise status: released

Exercise details

In the recover exercise we will be looking at the panic and recover mechanisms in Go and utilizing them to create middleware for an HTTP server.

Given a simple web server (see main.go) that can potentially panic, create an http.Handler that wraps the existing mux and will recover from any panics and then does the following:

  1. Logs the error, as well as the stack trace.
  2. Sets the status code to http.StatusInternalServerError (500) whenever a panic occurs.
  3. Write a "Something went wrong" message when a panic occurs.
  4. Ensure that partial writes and 200 headers aren't set even if the handler started writing to the http.ResponseWriter BEFORE the panic occurred (this one may be trickier)
  5. If the environment is set to be development, print the stack trace and the error to the webpage as well as to the logs. Otherwise default to the "Something went wrong" message described in (3).

Bonus

As a bonus exercises you can also look at ways to ensure you don't lose functionality like implementing the http.Flusher or the http.Hijacker interfaces.

Useful links

Running the script on Docker

Dockerfile describe the processes:

  • adding ZSH shell in container
  • set up all needed files to build the script
  • build the script and run it

Dockershell.sh

Includes ZSH configuration which will be copied to .zshrc in docker container

http-recovery's People

Contributors

roki57 avatar lemon57 avatar

Watchers

 avatar

http-recovery's Issues

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.