Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
446daaea
Commit
446daaea
authored
Mar 26, 2012
by
xiemengjun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modify
parent
3e887137
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
317 additions
and
3 deletions
+317
-3
beego.go
beego/beego.go
+24
-3
controller.go
beego/core/controller.go
+20
-0
router.go
beego/core/router.go
+145
-0
server.go
beego/server/server.go
+15
-0
blog.go
examples/blog/blog.go
+96
-0
app.yaml
examples/blog/configs/app.yaml
+3
-0
hello.go
examples/hello/hello.go
+14
-0
No files found.
beego/beego.go
View file @
446daaea
package
beego
import
"
fmt
"
import
"
./core
"
func
main
(){
fmt
.
Printf
(
"hello world"
)
type
C
struct
{
core
.
Content
}
type
M
struct
{
core
.
Model
}
type
D
struct
{
core
.
Config
}
type
U
struct
{
core
.
URL
}
type
A
struct
{
core
.
Controller
}
type
V
struct
{
core
.
View
}
\ No newline at end of file
beego/core/controller.go
View file @
446daaea
package
beego
// Interface for controller types that handle requests
type
Controller
interface
{
// When implemented, handles the request
HandleRequest
(
c
*
Context
)
}
// The ControllerFunc type is an adapter to allow the use of
// ordinary functions as goweb handlers. If f is a function
// with the appropriate signature, ControllerFunc(f) is a
// Controller object that calls f.
type
ControllerFunc
func
(
*
Context
)
// HandleRequest calls f(c).
func
(
f
ControllerFunc
)
HandleRequest
(
c
*
Context
)
{
f
(
c
)
}
\ No newline at end of file
beego/core/router.go
View file @
446daaea
package
beego
import
(
"regexp"
"strings"
)
/*
Route
*/
// Represents a single route mapping
type
Route
struct
{
pattern
string
parameterKeys
ParameterKeyMap
extension
string
Path
string
Controller
Controller
MatcherFuncs
[]
RouteMatcherFunc
}
func
(
r
*
Route
)
String
()
string
{
return
"{Route:'"
+
r
.
Path
+
"'}"
}
// Makes a new route from the given path
func
makeRouteFromPath
(
path
string
)
*
Route
{
// get the path segments
segments
:=
getPathSegments
(
path
)
regexSegments
:=
make
([]
string
,
len
(
segments
))
// prepare the parameter key map
var
paramKeys
ParameterKeyMap
=
make
(
ParameterKeyMap
)
var
extension
string
// pull out any dynamic segments
for
index
,
_
:=
range
segments
{
if
isDynamicSegment
(
segments
[
index
])
{
// e.g. {id}
paramKeys
[
strings
.
Trim
(
segments
[
index
],
"{}"
)]
=
index
regexSegments
[
index
]
=
ROUTE_REGEX_PLACEHOLDER
}
else
if
isExtensionSegment
(
segments
[
index
])
{
// e.g. .json
extension
=
segments
[
index
]
// trim off the last space (we don't need it)
regexSegments
=
regexSegments
[
0
:
len
(
regexSegments
)
-
1
]
}
else
{
// e.g. "groups"
regexSegments
[
index
]
=
segments
[
index
]
}
}
patternString
:=
"/"
+
strings
.
Join
(
regexSegments
,
"/"
)
// return a new route
var
route
*
Route
=
new
(
Route
)
route
.
pattern
=
patternString
route
.
extension
=
extension
route
.
parameterKeys
=
paramKeys
route
.
Path
=
path
route
.
Controller
=
nil
return
route
}
// Gets the parameter values for the route from the specified path
func
(
route
*
Route
)
getParameterValueMap
(
path
string
)
ParameterValueMap
{
return
getParameterValueMap
(
route
.
parameterKeys
,
path
)
}
// Checks whether a path matches a route or not
func
(
route
*
Route
)
DoesMatchPath
(
path
string
)
bool
{
match
,
error
:=
regexp
.
MatchString
(
route
.
pattern
,
path
)
if
error
==
nil
{
if
match
{
if
len
(
route
.
extension
)
>
0
{
// make sure the extensions match too
return
strings
.
HasSuffix
(
strings
.
ToLower
(
path
),
strings
.
ToLower
(
route
.
extension
))
}
else
{
return
match
}
}
else
{
return
false
}
}
// error :-(
return
false
}
// Checks whether the context for this request matches the route
func
(
route
*
Route
)
DoesMatchContext
(
c
*
Context
)
bool
{
// by default, we match
var
match
bool
=
true
if
len
(
route
.
MatcherFuncs
)
>
0
{
// there are some matcher functions, so don't automatically
// match by default - let the matchers decide
match
=
false
// loop through the matcher functions
for
_
,
f
:=
range
route
.
MatcherFuncs
{
// modify 'match' based on the result of the matcher function
switch
f
(
c
)
{
case
NoMatch
:
match
=
false
case
Match
:
match
=
true
}
}
}
// return the result
return
match
}
\ No newline at end of file
beego/server/server.go
0 → 100644
View file @
446daaea
package
server
import
(
"bufio"
"bytes"
"errors"
"io"
"log"
"net"
"net/url"
"runtime/debug"
"strconv"
"strings"
)
\ No newline at end of file
examples/blog/blog.go
View file @
446daaea
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package
guestbook
import
(
"io"
"net/http"
"text/template"
"time"
"appengine"
"appengine/datastore"
"appengine/user"
)
type
Greeting
struct
{
Author
string
Content
string
Date
time
.
Time
}
func
serve404
(
w
http
.
ResponseWriter
)
{
w
.
WriteHeader
(
http
.
StatusNotFound
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/plain; charset=utf-8"
)
io
.
WriteString
(
w
,
"Not Found"
)
}
func
serveError
(
c
appengine
.
Context
,
w
http
.
ResponseWriter
,
err
error
)
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/plain; charset=utf-8"
)
io
.
WriteString
(
w
,
"Internal Server Error"
)
c
.
Errorf
(
"%v"
,
err
)
}
var
mainPage
=
template
.
Must
(
template
.
New
(
"guestbook"
)
.
Parse
(
`<html><body>
{{range .}}
{{with .Author}}<b>{{.|html}}</b>{{else}}An anonymous person{{end}}
on <em>{{.Date.Format "3:04pm, Mon 2 Jan"}}</em>
wrote <blockquote>{{.Content|html}}</blockquote>
{{end}}
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form></body></html>
`
))
func
handleMainPage
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Method
!=
"GET"
||
r
.
URL
.
Path
!=
"/"
{
serve404
(
w
)
return
}
c
:=
appengine
.
NewContext
(
r
)
q
:=
datastore
.
NewQuery
(
"Greeting"
)
.
Order
(
"-Date"
)
.
Limit
(
10
)
var
gg
[]
*
Greeting
_
,
err
:=
q
.
GetAll
(
c
,
&
gg
)
if
err
!=
nil
{
serveError
(
c
,
w
,
err
)
return
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
if
err
:=
mainPage
.
Execute
(
w
,
gg
);
err
!=
nil
{
c
.
Errorf
(
"%v"
,
err
)
}
}
func
handleSign
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Method
!=
"POST"
{
serve404
(
w
)
return
}
c
:=
appengine
.
NewContext
(
r
)
if
err
:=
r
.
ParseForm
();
err
!=
nil
{
serveError
(
c
,
w
,
err
)
return
}
g
:=
&
Greeting
{
Content
:
r
.
FormValue
(
"content"
),
Date
:
time
.
Now
(),
}
if
u
:=
user
.
Current
(
c
);
u
!=
nil
{
g
.
Author
=
u
.
String
()
}
if
_
,
err
:=
datastore
.
Put
(
c
,
datastore
.
NewIncompleteKey
(
c
,
"Greeting"
,
nil
),
g
);
err
!=
nil
{
serveError
(
c
,
w
,
err
)
return
}
http
.
Redirect
(
w
,
r
,
"/"
,
http
.
StatusFound
)
}
func
init
()
{
http
.
HandleFunc
(
"/"
,
handleMainPage
)
http
.
HandleFunc
(
"/sign"
,
handleSign
)
}
examples/blog/configs/app.yaml
0 → 100644
View file @
446daaea
application
:
guestbook-go
handlers
:
-
url
:
/.*
examples/hello/hello.go
View file @
446daaea
package
helloworld
import
(
"fmt"
"net/http"
)
func
init
()
{
http
.
HandleFunc
(
"/"
,
handle
)
}
func
handle
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprint
(
w
,
"<html><body>Hello, World! 세상아 안녕!</body></html>"
)
}
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