Skip to content

Commit 9eef90d

Browse files
committed
Documentation improvements
1 parent 0a5eb29 commit 9eef90d

File tree

10 files changed

+91
-31
lines changed

10 files changed

+91
-31
lines changed

api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
// API describes a JSON API server. API handles return data or an error, and all responses are wrapped in a common
16-
// response object.
16+
// response object; [web.JSONResponse].
1717
type API struct {
1818
server *Server
1919
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ go 1.19
55
require (
66
github.com/ecnepsnai/logtic v1.9.5
77
github.com/gorilla/websocket v1.5.3
8-
golang.org/x/time v0.7.0
8+
golang.org/x/time v0.8.0
99
)

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@ github.com/ecnepsnai/logtic v1.9.5 h1:p1IAUPGHNve0597vChLHGYFPXx1qR3+y66yIZefdvl
22
github.com/ecnepsnai/logtic v1.9.5/go.mod h1:fs2kkqGqiX77ejVNBKpSV/dMVtn9bTg9YtHLP9MC0U8=
33
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
44
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
5-
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
6-
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
7-
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
8-
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
5+
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
6+
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=

handle.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ type SocketHandle func(request Request, conn *WSConn)
1919

2020
// HandleOptions describes options for a route
2121
type HandleOptions struct {
22-
// AuthenticateMethod method called to determine if a request is properly authenticated or not.
23-
// Optional - Omit this entirely if no authentication is needed for the request.
24-
// Return nil to signal an unauthenticated request, which will be rejected.
25-
// Objects returned will be passed to the handle as the UserData object.
22+
// AuthenticateMethod method called to determine if a request is properly authenticated or not. If a method is
23+
// provided, then it is called for each incoming request. The value returned by this method is passed as the
24+
// UserData field of a [web.Request]. Returning nil signals an unauthenticated request, which will be handled by
25+
// the UnauthorizedMethod (if provided) or a default handle. If the AuthenticateMethod is not provided, then the
26+
// UserData field is nil.
2627
AuthenticateMethod func(request *http.Request) interface{}
2728
// PreHandle is an optional method that is called immediately upon receiving the HTTP request, before authentication
2829
// and before rate limit checks. This method allows servers to provide early handling of a request before any
2930
// processing happens.
3031
//
31-
// The value of the error is not used, only if an error or nil was returned. If an error is returned then no more
32-
// processing is performed. It is assumed that a response will have been written to w.
32+
// The returned error is only used as a nil check, the value of any error isn't used. If an error is returned then
33+
// no more processing is performed. It is assumed that a response will have been written to w.
3334
//
3435
// If nil is returned then the request will continue normally, no status should have been written to w. Any headers
3536
// added may be overwritten by the handle.
3637
PreHandle func(w http.ResponseWriter, request *http.Request) error
37-
// UnauthorizedMethod method called when an unauthenticated request occurs (AuthenticateMethod returned nil)
38-
// to customize the response seen by the user.
39-
// Optional - Omit this to have a default response.
38+
// UnauthorizedMethod method called when an unauthenticated request occurs, i.e.AuthenticateMethod returned nil,
39+
// which allows you to customize the response seen by the user.
40+
// If omitted, a default handle is used.
4041
UnauthorizedMethod func(w http.ResponseWriter, request *http.Request)
41-
// MaxBodyLength defines the maximum length accepted for any HTTP request body. Requests that
42-
// exceed this limit will receive a 413 Payload Too Large response.
43-
// The default value of 0 will not reject requests with large bodies.
42+
// MaxBodyLength defines the maximum length accepted for any HTTP request body. Requests that exceed this limit will
43+
// receive a "413 Payload Too Large" response. The default value of 0 will not reject requests with large bodies.
4444
MaxBodyLength uint64
4545
// DontLogRequests if true then requests to this handle are not logged
4646
DontLogRequests bool

http_easy.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ import (
1212
"github.com/ecnepsnai/web/router"
1313
)
1414

15-
// HTTPEasy describes a HTTPEasy server. HTTPEasy handles are expected to return a reader and specify the content
16-
// type and length themselves.
15+
// HTTPEasy describes a simple to use HTTP router. HTTPEasy handles are expected to return a reader and specify the
16+
// content type and length themselves.
1717
//
18-
// The HTTPEasy server supports HTTPEasy range requests, should the client request it and the application provide a
19-
// supported Reader (io.ReadSeekCloser).
18+
// HTTP abstracts many features away from the caller, providing a simpler experience when a only a simple HTTP server
19+
// is needed. If you require more control, use the HTTP router.
20+
//
21+
// The HTTPEasy server supports HTTP range requests, should the client request it and the application provide a
22+
// supported Reader [io.ReadSeekCloser].
2023
type HTTPEasy struct {
2124
server *Server
2225
}

request.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ type Decoder interface {
2121
Decode(v any) error
2222
}
2323

24-
// DecodeJSON unmarshal the JSON body to the provided interface
24+
// DecodeJSON unmarshal the JSON body to the provided interface.
25+
//
26+
// Equal to calling:
27+
//
28+
// r.Decode(v, json.NewDecoder(r.HTTP.Body))
2529
func (r Request) DecodeJSON(v any) *Error {
2630
return r.Decode(v, json.NewDecoder(r.HTTP.Body))
2731
}
@@ -41,6 +45,8 @@ func (r Request) Decode(v any, decoder Decoder) *Error {
4145
// RealRemoteAddr will try to get the real IP address of the incoming connection taking proxies into
4246
// consideration. This function looks for the `X-Real-IP`, `X-Forwarded-For`, and `CF-Connecting-IP`
4347
// headers, and if those don't exist will return the remote address of the connection.
48+
//
49+
// Will never return nil, if it is unable to get a valid address it will return 0.0.0.0
4450
func (r Request) RealRemoteAddr() net.IP {
4551
return RealRemoteAddr(r.HTTP)
4652
}

response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ type HTTPResponse struct {
3535
Cookies []http.Cookie
3636
// The content type of the response. Will overwrite any 'content-type' header in Headers.
3737
ContentType string
38-
// The length of the content.
38+
// The length of the content. Will overwrite any 'content-length' header in Headers.
3939
ContentLength uint64
4040
}

server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ type Server struct {
1818
// The port that this server is listening on. Only populated if the server was created with web.New().
1919
ListenPort uint16
2020
// The JSON API server. API handles return data or an error, and all responses are wrapped in a common
21-
// response object.
21+
// response object; [web.JSONResponse].
2222
API API
2323
// HTTPEasy describes a easy HTTP server. HTTPEasy handles are expected to return a reader and specify the content
2424
// type and length themselves.
2525
//
2626
// The HTTPEasy server supports HTTP range requests, should the client request it and the application provide a
27-
// supported Reader (io.ReadSeekCloser).
27+
// supported Reader [io.ReadSeekCloser].
2828
HTTPEasy HTTPEasy
2929
// The HTTP server. HTTP handles are exposed to the raw http request and response writers.
3030
HTTP HTTP

util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
// RealRemoteAddr will try to get the real IP address of the incoming connection taking proxies into
99
// consideration. This function looks for the `X-Real-IP`, `X-Forwarded-For`, and `CF-Connecting-IP`
1010
// headers, and if those don't exist will return the remote address of the connection.
11+
//
12+
// Will never return nil, if it is unable to get a valid address it will return 0.0.0.0
1113
func RealRemoteAddr(r *http.Request) net.IP {
1214
if ip := net.ParseIP(r.Header.Get("X-Real-IP")); ip != nil {
1315
return ip

web.go

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,62 @@
11
/*
2-
Package web is a HTTP server for Golang applications.
2+
Package web is a full-featured HTTP router and server for Go applications, suitable for serving static files, REST APIs,
3+
and more.
34
4-
It is suitable for both front-end and back-end use, being able to deliver static content, act as a REST-ful JSON server,
5-
and as a WebSocket server.
5+
Web includes these features:
6+
- HTTP range support
7+
- Static file serving
8+
- Directory listings
9+
- Websockets
10+
- Per-IP rate limiting
11+
- Per-request contextual data
612
7-
It includes simple controls to allow for user authentication with contextual data being available in every request, and
8-
provides simple per-user rate-limiting.
13+
Web offers four APIs for developers to choose from:
14+
15+
# API
16+
17+
API provides everything you need to build poweful REST APIs using JSON. Define your routes and easily accept and return
18+
data as JSON.
19+
20+
Example:
21+
22+
router := web.New("[::]:8080")
23+
router.API.Get("/users", getUsers, options)
24+
router.API.Get("/users/:username", getUsers, options)
25+
26+
For more information, see the documentation of [web.API].
27+
28+
# HTTPEasy
29+
30+
HTTPEasy provides a straightforward interface to accept HTTP requets and return data.
31+
32+
Example:
33+
34+
router := web.New("[::]:8080")
35+
router.HTTPEasy.Get("/index.html", getIndex, options)
36+
router.HTTPEasy.Get("/cat.jpg", getKitty, options)
37+
38+
For more information, see the documentation of [web.HTTPEasy].
39+
40+
# HTTP
41+
42+
HTTP provides full access to the original HTTP request, allowing you total control over the response, whatever that may be.
43+
44+
Example:
45+
46+
router := web.New("[::]:8080")
47+
router.HTTP.Get("/index.html", getIndex, options)
48+
router.HTTP.Get("/cat.jpg", getKitty, options)
49+
50+
For more information, see the documentation of [web.HTTP].
51+
52+
# Websockets
53+
54+
This package also provides a wrapper for [github.com/gorilla/websocket]
55+
56+
Example:
57+
58+
router := web.New("[::]:8080")
59+
router.Socket("/ws", handleSocket, options)
960
*/
1061
package web
1162

0 commit comments

Comments
 (0)