Skip to content

A lightweight web server for validating credit card numbers, written in Go.

License

Notifications You must be signed in to change notification settings

ibnaleem/cc-validation-webserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

build

Credit Card Validation Webserver

A lightweight web server for validating credit card numbers, written in Go.

Table of Contents

Requirements

  • Go 1.22.5 or later.
  • A terminal or command prompt to run the application.

Building the Webserver

  1. Clone the repository to your local machine:

    $ git clone https://github.com/ibnaleem/cc-validation-webserver.git
    $ cd cc-validation-webserver 
  2. Initialise Go modules if you haven't done so:

    $ go mod tidy
  3. Build the webserver:

    $ go build
  4. This will create the binary ./webserver.

Running the Server

$ ./webserver

By default, the server will start on port 3333:

:: Webserver started on port 3333 ::

You can then access the server at http://127.0.0.1:3333/.

Endpoints

Root Endpoint (/)

  • Method: POST

  • Content-Type: application/json

  • Request Payload:

    The server expects a JSON payload containing a credit-card field. The value of this field should be a string representing the credit card number you want to validate.

    Example request:

    {
        "credit-card": "378282246310005"
    }
  • Response:

    The server will respond with a string value (true or false) depending on whether the credit card number is valid according to the Luhn algorithm.

    • true: If the credit card number is valid.
    • false: If the credit card number is invalid.

    Example response:

    true

Error Responses:

  • Invalid JSON: If the provided JSON is malformed.

    • Status code: 400 Bad Request
    • Example response:
      {"error": "Invalid JSON"}
  • Missing Credit Card Field: If the JSON does not contain the credit-card field.

    • Status code: 400 Bad Request
    • Example response:
      {"error": "Missing expected field in JSON"}
  • Invalid Credit Card Number: If the credit-card field contains a number that cannot be converted to an integer or fails the Luhn check.

    • Status code: 400 Bad Request
    • Example response:
      {"error": "Invalid credit card number"}

Customisation

Changing the Port

By default, the server listens on port 3333. You can change this by setting a different value for the PORT variable in the webserver.go file:

var PORT string = "3333"

Change "3333" to any valid port number you prefer (e.g., "8080"). Note that PORT expect a type of string, meaning var PORT string = 8080 is clearly invalid. Simply encapsulate the port in quotes "".

Changing the Path

To customise the root endpoint (/) to a different one, modify the http.HandleFunc line in the webserver.go file:

http.HandleFunc("/", getRoot)

For example, to change the path to /validate, update it as follows:

http.HandleFunc("/validate", getRoot)

Now, the server will expect requests at http://127.0.0.1:3333/validate.

To ensure consistency and avoid confusion, it's best to also update the function name to match the new path. So, if you change the path to /validate for example, rename the function to reflect this change:

func getValidate(w http.ResponseWriter, r *http.Request) {...}

Additionally, update the fmt.Printf() to reflect the new path:

if r.Header.Get("Content-Type") == "" {
    fmt.Printf("[%s] on validate (/validate) with cURL\n", r.Method)
} else {
    fmt.Printf("[%s] on validate (/validate) with header %s\n", r.Method, r.Header.Get("Content-Type"))
}

License

This project is licensed under the GNU General Public License - see the LICENSE file for details.