-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
42 lines (33 loc) · 906 Bytes
/
main.go
File metadata and controls
42 lines (33 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"net/http"
"regexp"
"strconv"
)
const (
serverPort = 8080
)
func isValidStatusCode(statusCode int) bool {
return statusCode >= 100 && statusCode <= 599
}
func handleStatusCode(w http.ResponseWriter, r *http.Request) {
re := regexp.MustCompile(`^/(\d+)$`)
matches := re.FindStringSubmatch(r.URL.Path)
if len(matches) != 2 {
http.Error(w, "Invalid URL format", http.StatusBadRequest)
return
}
statusCode, err := strconv.Atoi(matches[1])
if err != nil || !isValidStatusCode(statusCode) {
http.Error(w, "Invalid status code", http.StatusBadRequest)
return
}
// Responding with the specified status code and status text
http.Error(w, http.StatusText(statusCode), statusCode)
}
func main() {
http.HandleFunc("/", handleStatusCode)
fmt.Printf("Server is running on port '%d'\n", serverPort)
http.ListenAndServe(fmt.Sprintf(":%d", serverPort), nil)
}