Skip to content

Commit 8fa83a3

Browse files
committed
Split out web.Server and its methods into server.go
Also, add helpers.go to store the helper methods
1 parent 42fa074 commit 8fa83a3

File tree

4 files changed

+434
-405
lines changed

4 files changed

+434
-405
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ GOFMT=gofmt -s -tabs=false -tabwidth=4
22

33
GOFILES=\
44
fcgi.go\
5+
helpers.go\
56
scgi.go\
7+
server.go\
68
status.go\
79
web.go\
810

helpers.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package web
2+
3+
import (
4+
"bytes"
5+
"net/http"
6+
"net/url"
7+
"os"
8+
"regexp"
9+
"strings"
10+
"time"
11+
)
12+
13+
// internal utility methods
14+
func webTime(t time.Time) string {
15+
ftime := t.Format(time.RFC1123)
16+
if strings.HasSuffix(ftime, "UTC") {
17+
ftime = ftime[0:len(ftime)-3] + "GMT"
18+
}
19+
return ftime
20+
}
21+
22+
func dirExists(dir string) bool {
23+
d, e := os.Stat(dir)
24+
switch {
25+
case e != nil:
26+
return false
27+
case !d.IsDir():
28+
return false
29+
}
30+
31+
return true
32+
}
33+
34+
func fileExists(dir string) bool {
35+
info, err := os.Stat(dir)
36+
if err != nil {
37+
return false
38+
}
39+
40+
return !info.IsDir()
41+
}
42+
43+
// Urlencode is a helper method that converts a map into URL-encoded form data.
44+
// It is a useful when constructing HTTP POST requests.
45+
func Urlencode(data map[string]string) string {
46+
var buf bytes.Buffer
47+
for k, v := range data {
48+
buf.WriteString(url.QueryEscape(k))
49+
buf.WriteByte('=')
50+
buf.WriteString(url.QueryEscape(v))
51+
buf.WriteByte('&')
52+
}
53+
s := buf.String()
54+
return s[0 : len(s)-1]
55+
}
56+
57+
var slugRegex = regexp.MustCompile(`(?i:[^a-z0-9\-_])`)
58+
59+
// Slug is a helper function that returns the URL slug for string s.
60+
// It's used to return clean, URL-friendly strings that can be
61+
// used in routing.
62+
func Slug(s string, sep string) string {
63+
if s == "" {
64+
return ""
65+
}
66+
slug := slugRegex.ReplaceAllString(s, sep)
67+
if slug == "" {
68+
return ""
69+
}
70+
quoted := regexp.QuoteMeta(sep)
71+
sepRegex := regexp.MustCompile("(" + quoted + "){2,}")
72+
slug = sepRegex.ReplaceAllString(slug, sep)
73+
sepEnds := regexp.MustCompile("^" + quoted + "|" + quoted + "$")
74+
slug = sepEnds.ReplaceAllString(slug, "")
75+
return strings.ToLower(slug)
76+
}
77+
78+
// NewCookie is a helper method that returns a new http.Cookie object.
79+
// Duration is specified in seconds. If the duration is zero, the cookie is permanent.
80+
// This can be used in conjunction with ctx.SetCookie.
81+
func NewCookie(name string, value string, age int64) *http.Cookie {
82+
var utctime time.Time
83+
if age == 0 {
84+
// 2^31 - 1 seconds (roughly 2038)
85+
utctime = time.Unix(2147483647, 0)
86+
} else {
87+
utctime = time.Unix(time.Now().Unix()+age, 0)
88+
}
89+
return &http.Cookie{Name: name, Value: value, Expires: utctime}
90+
}

0 commit comments

Comments
 (0)