This is a simple REST API written in Go using the Gorilla Mux router. The API allows users to classify numbers based on various properties such as being an Armstrong number, even/odd, prime, or perfect. It also integrates CORS middleware to handle cross-origin requests.
- Classifies numbers based on mathematical properties.
- Handles user input and provides error messages for invalid inputs.
- Implements CORS support to allow cross-origin requests.
- Uses Gorilla Mux for routing.
- Go (Golang) - The main programming language.
- Gorilla Mux - Router for handling API routes.
- JSON Encoding/Decoding - To format API responses.
- Net/HTTP - Standard Go package for handling HTTP requests.
- CORS Middleware - Custom middleware to allow cross-origin requests.
Ensure you have the following installed:
- Go (latest stable version)
git clone https://github.com/yourusername/numbers-api.git
cd numbers-api
go mod tidy
go run main.go
The server will start on port 3000
.
Endpoint: GET /api/classify-number?number={value}
curl -X GET "http://localhost:3000/api/classify-number?number=371"
{
"number": 371,
"is_prime": false,
"is_perfect": false,
"properties": ["armstrong", "odd"]
}
If an invalid number is provided:
{
"number": "alphabet",
"error": true
}
This API supports CORS to allow requests from different origins. The middleware is applied globally.
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
}
go build -o classify-number
./classify-number
Feel free to contribute by forking the repo and submitting a pull request.
This project is licensed under the MIT License.