Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
a4e6197b
Commit
a4e6197b
authored
Mar 15, 2012
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net/http: couple more triv.go modernizations
R=golang-dev, rsc CC=golang-dev
https://golang.org/cl/5834049
parent
aec01c36
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
9 deletions
+16
-9
src/pkg/net/http/triv.go
src/pkg/net/http/triv.go
+16
-9
No files found.
src/pkg/net/http/triv.go
View file @
a4e6197b
...
@@ -17,6 +17,7 @@ import (
...
@@ -17,6 +17,7 @@ import (
"os"
"os"
"os/exec"
"os/exec"
"strconv"
"strconv"
"sync"
)
)
// hello world, the web server
// hello world, the web server
...
@@ -29,14 +30,21 @@ func HelloServer(w http.ResponseWriter, req *http.Request) {
...
@@ -29,14 +30,21 @@ func HelloServer(w http.ResponseWriter, req *http.Request) {
// Simple counter server. POSTing to it will set the value.
// Simple counter server. POSTing to it will set the value.
type
Counter
struct
{
type
Counter
struct
{
n
int
mu
sync
.
Mutex
// protects n
n
int
}
}
// This makes Counter satisfy the expvar.Var interface, so we can export
// This makes Counter satisfy the expvar.Var interface, so we can export
// it directly.
// it directly.
func
(
ctr
*
Counter
)
String
()
string
{
return
fmt
.
Sprintf
(
"%d"
,
ctr
.
n
)
}
func
(
ctr
*
Counter
)
String
()
string
{
ctr
.
mu
.
Lock
()
defer
ctr
.
mu
.
Unlock
()
return
fmt
.
Sprintf
(
"%d"
,
ctr
.
n
)
}
func
(
ctr
*
Counter
)
ServeHTTP
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
func
(
ctr
*
Counter
)
ServeHTTP
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
ctr
.
mu
.
Lock
()
defer
ctr
.
mu
.
Unlock
()
switch
req
.
Method
{
switch
req
.
Method
{
case
"GET"
:
case
"GET"
:
ctr
.
n
++
ctr
.
n
++
...
@@ -110,23 +118,22 @@ func Logger(w http.ResponseWriter, req *http.Request) {
...
@@ -110,23 +118,22 @@ func Logger(w http.ResponseWriter, req *http.Request) {
http
.
Error
(
w
,
"oops"
,
404
)
http
.
Error
(
w
,
"oops"
,
404
)
}
}
var
webroot
=
flag
.
String
(
"root"
,
"/home/rsc"
,
"web root directory"
)
var
webroot
=
flag
.
String
(
"root"
,
os
.
Getenv
(
"HOME"
)
,
"web root directory"
)
func
main
()
{
func
main
()
{
flag
.
Parse
()
flag
.
Parse
()
// The counter is published as a variable directly.
// The counter is published as a variable directly.
ctr
:=
new
(
Counter
)
ctr
:=
new
(
Counter
)
http
.
Handle
(
"/counter"
,
ctr
)
expvar
.
Publish
(
"counter"
,
ctr
)
expvar
.
Publish
(
"counter"
,
ctr
)
http
.
Handle
(
"/counter"
,
ctr
)
http
.
Handle
(
"/"
,
http
.
HandlerFunc
(
Logger
))
http
.
Handle
(
"/"
,
http
.
HandlerFunc
(
Logger
))
http
.
Handle
(
"/go/"
,
http
.
StripPrefix
(
"/go/"
,
http
.
FileServer
(
http
.
Dir
(
*
webroot
))))
http
.
Handle
(
"/go/"
,
http
.
StripPrefix
(
"/go/"
,
http
.
FileServer
(
http
.
Dir
(
*
webroot
))))
http
.
Handle
(
"/flags"
,
http
.
HandlerFunc
(
FlagServer
))
http
.
Handle
(
"/args"
,
http
.
HandlerFunc
(
ArgServer
))
http
.
Handle
(
"/go/hello"
,
http
.
HandlerFunc
(
HelloServer
))
http
.
Handle
(
"/chan"
,
ChanCreate
())
http
.
Handle
(
"/chan"
,
ChanCreate
())
http
.
Handle
(
"/date"
,
http
.
HandlerFunc
(
DateServer
))
http
.
HandleFunc
(
"/flags"
,
FlagServer
)
http
.
HandleFunc
(
"/args"
,
ArgServer
)
http
.
HandleFunc
(
"/go/hello"
,
HelloServer
)
http
.
HandleFunc
(
"/date"
,
DateServer
)
err
:=
http
.
ListenAndServe
(
":12345"
,
nil
)
err
:=
http
.
ListenAndServe
(
":12345"
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Panicln
(
"ListenAndServe:"
,
err
)
log
.
Panicln
(
"ListenAndServe:"
,
err
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment