forked from zhravan/golearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.go
More file actions
47 lines (40 loc) · 1.33 KB
/
Copy pathhttp_server.go
File metadata and controls
47 lines (40 loc) · 1.33 KB
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
43
44
45
46
47
package http_server
import (
"fmt"
"log"
"net/http"
"strings"
)
// helloHandler handles requests to paths beginning with /hello/.
// It extracts the first path segment after /hello/ and responds
// with "Hello, {name}!\n". If no name is provided, it returns
// a 400 Bad Request response.
func helloHandler(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/hello/")
// Only take the first segment if multiple are present (e.g., /hello/a/b → "a").
if i := strings.IndexByte(name, '/'); i >= 0 {
name = name[:i]
}
// Handle the case where no name is given (e.g., /hello/).
if name == "" {
http.Error(w, "name required", http.StatusBadRequest)
return
}
// Respond with plain text greeting.
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "Hello, %s!\n", name)
}
// init registers the /hello/ route when the package is loaded.
// This ensures the handler is available even when tests start
// their own server without calling StartServer.
func init() {
http.HandleFunc("/hello/", helloHandler)
}
// StartServer starts an HTTP server on port :8080 using the
// default multiplexer. Any server error is logged instead of
// being silently ignored.
func StartServer() {
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Printf("http server stopped: %v", err)
}
}