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
fece5adc
Commit
fece5adc
authored
Jul 09, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add example for api application
parent
7bfb4126
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
136 additions
and
0 deletions
+136
-0
app.conf
example/beeapi/conf/app.conf
+5
-0
default.go
example/beeapi/controllers/default.go
+59
-0
main.go
example/beeapi/main.go
+20
-0
object.go
example/beeapi/models/object.go
+52
-0
No files found.
example/beeapi/conf/app.conf
0 → 100644
View file @
fece5adc
appname
=
beeapi
httpport
=
8080
runmode
=
dev
autorender
=
false
copyrequestbody
=
true
example/beeapi/controllers/default.go
0 → 100644
View file @
fece5adc
package
controllers
import
(
"encoding/json"
"github.com/astaxie/beego"
"github.com/astaxie/beego/example/beeapi/models"
)
type
ResponseInfo
struct
{
}
type
ObejctController
struct
{
beego
.
Controller
}
func
(
this
*
ObejctController
)
Post
()
{
var
ob
models
.
Object
json
.
Unmarshal
(
this
.
Ctx
.
RequestBody
,
&
ob
)
objectid
:=
models
.
AddOne
(
ob
)
this
.
Data
[
"json"
]
=
"{
\"
ObjectId
\"
:
\"
"
+
objectid
+
"
\"
}"
this
.
ServeJson
()
}
func
(
this
*
ObejctController
)
Get
()
{
objectId
:=
this
.
Ctx
.
Params
[
":objectId"
]
if
objectId
!=
""
{
ob
,
err
:=
models
.
GetOne
(
objectId
)
if
err
!=
nil
{
this
.
Data
[
"json"
]
=
err
}
else
{
this
.
Data
[
"json"
]
=
ob
}
}
else
{
obs
:=
models
.
GetAll
()
this
.
Data
[
"json"
]
=
obs
}
this
.
ServeJson
()
}
func
(
this
*
ObejctController
)
Put
()
{
objectId
:=
this
.
Ctx
.
Params
[
":objectId"
]
var
ob
models
.
Object
json
.
Unmarshal
(
this
.
Ctx
.
RequestBody
,
&
ob
)
err
:=
models
.
Update
(
objectId
,
ob
.
Score
)
if
err
!=
nil
{
this
.
Data
[
"json"
]
=
err
}
else
{
this
.
Data
[
"json"
]
=
"update success!"
}
this
.
ServeJson
()
}
func
(
this
*
ObejctController
)
Delete
()
{
objectId
:=
this
.
Ctx
.
Params
[
":objectId"
]
models
.
Delete
(
objectId
)
this
.
Data
[
"json"
]
=
"delete success!"
this
.
ServeJson
()
}
example/beeapi/main.go
0 → 100644
View file @
fece5adc
package
main
import
(
"github.com/astaxie/beego"
"github.com/astaxie/beego/example/beeapi/controllers"
)
// Objects
// URL HTTP Verb Functionality
// /object POST Creating Objects
// /object/<objectId> GET Retrieving Objects
// /object/<objectId> PUT Updating Objects
// /object GET Queries
// /object/<objectId> DELETE Deleting Objects
func
main
()
{
beego
.
RESTRouter
(
"/object"
,
&
controllers
.
ObejctController
{})
beego
.
Run
()
}
example/beeapi/models/object.go
0 → 100644
View file @
fece5adc
package
models
import
(
"errors"
"strconv"
"time"
)
var
(
Objects
map
[
string
]
*
Object
)
type
Object
struct
{
ObjectId
string
Score
int64
PlayerName
string
}
func
init
()
{
Objects
=
make
(
map
[
string
]
*
Object
)
Objects
[
"hjkhsbnmn123"
]
=
&
Object
{
"hjkhsbnmn123"
,
100
,
"astaxie"
}
Objects
[
"mjjkxsxsaa23"
]
=
&
Object
{
"mjjkxsxsaa23"
,
101
,
"someone"
}
}
func
AddOne
(
object
Object
)
(
ObjectId
string
)
{
object
.
ObjectId
=
"astaxie"
+
strconv
.
FormatInt
(
time
.
Now
()
.
UnixNano
(),
10
)
Objects
[
object
.
ObjectId
]
=
&
object
return
object
.
ObjectId
}
func
GetOne
(
ObjectId
string
)
(
object
*
Object
,
err
error
)
{
if
v
,
ok
:=
Objects
[
ObjectId
];
ok
{
return
v
,
nil
}
return
nil
,
errors
.
New
(
"ObjectId Not Exist"
)
}
func
GetAll
()
map
[
string
]
*
Object
{
return
Objects
}
func
Update
(
ObjectId
string
,
Score
int64
)
(
err
error
)
{
if
v
,
ok
:=
Objects
[
ObjectId
];
ok
{
v
.
Score
=
Score
return
nil
}
return
errors
.
New
(
"ObjectId Not Exist"
)
}
func
Delete
(
ObjectId
string
)
{
delete
(
Objects
,
ObjectId
)
}
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