Skip to content

Instantly share code, notes, and snippets.

@nicolascb
Created August 11, 2022 12:00
Show Gist options
  • Save nicolascb/cc5a51002e5a9ac9e737f4bb831376da to your computer and use it in GitHub Desktop.
Save nicolascb/cc5a51002e5a9ac9e737f4bb831376da to your computer and use it in GitHub Desktop.
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