A lightweight web server for validating credit card numbers, written in Go.
- Go 1.22.5 or later.
- A terminal or command prompt to run the application.
-
Clone the repository to your local machine:
$ git clone https://github.com/ibnaleem/cc-validation-webserver.git $ cd cc-validation-webserver -
Initialise Go modules if you haven't done so:
$ go mod tidy
-
Build the webserver:
$ go build
-
This will create the binary
./webserver.
$ ./webserverBy 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/.
-
Method:
POST -
Content-Type:
application/json -
Request Payload:
The server expects a JSON payload containing a
credit-cardfield. 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 (
trueorfalse) 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
-
Invalid JSON: If the provided JSON is malformed.
- Status code:
400 Bad Request - Example response:
{"error": "Invalid JSON"}
- Status code:
-
Missing Credit Card Field: If the JSON does not contain the
credit-cardfield.- Status code:
400 Bad Request - Example response:
{"error": "Missing expected field in JSON"}
- Status code:
-
Invalid Credit Card Number: If the
credit-cardfield 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"}
- Status code:
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 "".
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"))
}This project is licensed under the GNU General Public License - see the LICENSE file for details.