You can build a simple restful project or a web application with it. If you dosen't want to use mysql db, you can implement your own Auth decorates and session.You can use your own DAO or whatever you like.
-
run cmd
mkdir demo && cd demo go get gopkg.in/alecthomas/kingpin.v2 go get github.com/yang-f/beauty
-
add $GOPATH/bin to your $PATH
-
run cmd beauty
usage: beauty [<flags>] <command> [<args> ...] A command-line tools of beauty. Flags: --help Show context-sensitive help (also try --help-long and --help-man). Commands: help [<command>...] Show help. demo Demo of web server. generate <name> Generate a new app.
-
test beauty
beauty demo
-
then
2017/05/04 16:21:05 start server on port :8080
-
visit 127.0.0.1:8080
{"description":"this is json"}
-
visit 127.0.0.1:8080/demo1
{"status":403,"description":"token not found.","code":"AUTH_FAILED"}
-
visit 127.0.0.1:8080/demo2
{"description":"this is json"}
-
visit 127.0.0.1:8080/demo3
{"status":403,"description":"token not found.","code":"AUTH_FAILED"}
-
Generate new app
beauty generate yourAppName
-
dir list
GOPATH/src/yourAppName ├── controllers │ ├── adminController.go │ └── controller_test.go ├── main.go ├── models ├── routers │ └── routes.go ├── tpl └── utils
-
about Route
type Route struct { Name string //Show route name in log file Method string //GET PUT POST DELETE ... Pattern string //Path HandlerFunc decorates.Handler //Controller or handler ContentType string //"application/json;charset=utf-8" or "text/html" etc... }
- demo
router.BRoutes = router.Routes{ router.Route{ "nothing", "GET", "/", controllers.Config, contenttype.JSON, },//normal route router.Route{ "authDemo", "GET", "/demo1", Handler(controllers.Config). Auth(), contenttype.JSON, },//route need auth router.Route{ "verifyDemo", "GET", "/demo2", Handler(controllers.Config). Verify(), contenttype.JSON, },//route need verify, such as sql injection router.Route{ "verifyAndAuthDemo", "GET", "/demo3", Handler(controllers.Config). Auth(). Verify(), contenttype.JSON, },//both }
-
token generate
tokenString, err := token.Generate(fmt.Sprintf("%v|%v", user_id, user_pass))
-
demo
package main import ( "net/http" "github.com/yang-f/beauty/consts/contenttype" "github.com/yang-f/beauty/utils/log" "github.com/yang-f/beauty/router" "github.com/yang-f/beauty/settings" "github.com/yang-f/beauty/controllers" . "github.com/yang-f/beauty/decorates" ) func main() { log.Printf("start server on port %s", settings.Listen) router.BRoutes = router.Routes{ router.Route{ "nothing", "GET", "/", controllers.Config, contenttype.JSON, }, router.Route{ "authDemo", "GET", "/demo1", Handler(controllers.Config). Auth(), contenttype.JSON, }, router.Route{ "verifyDemo", "GET", "/demo2", Handler(controllers.Config). Verify(), contenttype.JSON, }, router.Route{ "verifyAndAuthDemo", "GET", "/demo3", Handler(controllers.Config). Auth(). Verify(), contenttype.JSON, }, } settings.Listen = ":8080" settings.Domain = "yourdomain.com" settings.LogFile = "/your/path/yourname.log" settings.DefaultOrigin = "http://defaultorigin.com" settings.HmacSampleSecret = "whatever" router := router.NewRouter() log.Fatal(http.ListenAndServe(settings.Listen, router)) }
-
token
settings.HmacSampleSecret = "whatever" token, err := token.Generate(origin) origin, err := token.Valid(token)
-
db
db.Query(sql, params...)
-
cors
- static file server
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", decorates.CorsHeader2(http.FileServer(http.Dir("/your/static/path")))))
- api etc:
- default is cors
-
log
- use
settings.LogFile = "/you/log/path/beauty.log" log.Printf(msg, params...)
- auto archive
-
sessions
currentUser := sessions.CurrentUser(r *http.Request)
-
error handler
func XxxxController(w http.ResponseWriter, r *http.Request) *models.APPError { xxx,err := someOperation() if err != nil{ return &models.APPError {err, Message, Code, Status} } ... return nil }
-
utils
- Response
- Rand
- MD5
- Post
-
test
- go test -v -bench=".*"
- go run controllers/controller_test.go
- ...
- sql
create database yourdatabase; use yourdatabase; create table if not exists user ( user_id int primary key not null auto_increment, user_name varchar(64), user_pass varchar(64), user_mobile varchar(32), user_type enum('user', 'admin', 'test') not null, add_time timestamp not null default CURRENT_TIMESTAMP ); insert into user (user_name, user_pass) values('admin', 'admin');
- you need set a json file in '/srv/filestore/settings/latest.json' format like this
{ "mysql_host":"127.0.0.1:3306", "mysql_user":"root", "mysql_pass":"root", "mysql_database":"yourdatabase" }
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -m 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
- Cmd tools
- Improve document
- Role review