Created
August 11, 2022 12:00
-
-
Save nicolascb/cc5a51002e5a9ac9e737f4bb831376da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ( | |
apiAddress = ":8081" | |
) | |
func main() { | |
mux := setupMux() | |
http.ListenAndServe(apiAddress, mux) | |
} | |
func setupMux() *http.ServeMux { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/check-is-prime", isPrimeHandler) | |
return mux | |
} | |
func isPrimeHandler(w http.ResponseWriter, r *http.Request) { | |
number := r.URL.Query().Get("number") | |
n, err := strconv.Atoi(number) | |
if err != nil { | |
http.Error(w, "invalid number", http.StatusBadRequest) | |
return | |
} | |
fmt.Fprint(w, strconv.FormatBool(isPrime(int64(n)))) | |
} | |
func isPrime(n int64) bool { | |
return big.NewInt(n).ProbablyPrime(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment