forked from golang/example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helloserver: add basic hello world web server
We want to revive golang.org/x/example as a test case for an experiment with a 'gonew' command. This CL adds a basic "hello, world" web server as golang.org/x/example/helloserver. Change-Id: Icf76c756f7b256285d4c5e6a33655996597eb2da Reviewed-on: https://go-review.googlesource.com/c/example/+/513997 Auto-Submit: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Cameron Balahan <[email protected]>
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module golang.org/x/example/helloserver | ||
|
||
go 1.19 | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Hello is a simple hello, world demonstration web server. | ||
// | ||
// It serves version information on /version and answers | ||
// any other request like /name by saying "Hello, name!". | ||
// | ||
// See golang.org/x/example/outyet for a more sophisticated server. | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"html" | ||
"log" | ||
"net/http" | ||
"os" | ||
"runtime/debug" | ||
"strings" | ||
) | ||
|
||
func usage() { | ||
fmt.Fprintf(os.Stderr, "usage: helloserver [options]\n") | ||
flag.PrintDefaults() | ||
os.Exit(2) | ||
} | ||
|
||
var ( | ||
greeting = flag.String("g", "Hello", "Greet with `greeting`") | ||
addr = flag.String("addr", "localhost:8080", "address to serve") | ||
) | ||
|
||
func main() { | ||
// Parse flags. | ||
flag.Usage = usage | ||
flag.Parse() | ||
|
||
// Parse and validate arguments (none). | ||
args := flag.Args() | ||
if len(args) != 0 { | ||
usage() | ||
} | ||
|
||
// Register handlers. | ||
// All requests not otherwise mapped with go to greet. | ||
// /version is mapped specifically to version. | ||
http.HandleFunc("/", greet) | ||
http.HandleFunc("/version", version) | ||
|
||
log.Printf("serving http://%s\n", *addr) | ||
log.Fatal(http.ListenAndServe(*addr, nil)) | ||
} | ||
|
||
func version(w http.ResponseWriter, r *http.Request) { | ||
info, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
http.Error(w, "no build information available", 500) | ||
return | ||
} | ||
|
||
fmt.Fprintf(w, "<!DOCTYPE html>\n<pre>\n") | ||
fmt.Fprintf(w, "%s\n", html.EscapeString(info.String())) | ||
} | ||
|
||
func greet(w http.ResponseWriter, r *http.Request) { | ||
name := strings.Trim(r.URL.Path, "/") | ||
if name == "" { | ||
name = "Gopher" | ||
} | ||
|
||
fmt.Fprintf(w, "<!DOCTYPE html>\n") | ||
fmt.Fprintf(w, "Hello, %s!\n", html.EscapeString(name)) | ||
} |