IRIS Web Application
import "gitee.com/go-wares/framework-iris/framework"
Workspace
Follow is an example application workspace structure.
/data/sketch
├─ app
│ ├─ annotations
│ │ ├─ annotation_controllers.go
│ │ ├─ annotation_crontabs.go
│ │ ├─ annotation_handlers.go
│ │ └─ annotation_middlewares.go
│ ├─ controllers
│ │ └─ controller.go
│ ├─ crontabs
│ │ └─ example_crontab.go
│ ├─ handlers
│ │ └─ example_handler.go
│ ├─ logics
│ │ ├─ home
│ │ │ └─ logic.go
│ │ └─ user
│ │ ├─ logic.go
│ │ ├─ request.go
│ │ └─ response.go
│ └─ middlewares
│ └─ example_middleware.go
│
├─ config
│ ├─ app.yaml
│ ├─ db.yaml
│ ├─ iris.yaml
│ ├─ log.yaml
│ └─ redis.yaml
│
└─ main.go
Example
import (
"gitee.com/go-wares/framework-iris/framework"
"gitee.com/go-wares/log"
"sketch/app/annotations"
)
func main(){
defer log.Close()
c := framework.New()
// Annotations can be generated by `go run main.go gen:annotation`
c.Application.AddAnnotationMiddlewares(annotations.AnnotationMiddlewares)
c.Application.AddAnnotationControllers(annotations.AnnotationControllers...)
c.Application.AddAnnotationHandlers(annotations.AnnotationHandlers...)
c.Application.AddAnnotationCrontabs(annotations.AnnotationCrontab...)
// Run service.
c.Run()
}
Run
go run main.go start
Build binary
go build \
-trimpath \
-gcflags "-trimpath=$GOPATH" \
-ldflags "-s -w" \
-o sketch
Build Annotation
You can run follow script to generate docs.
go run main.go gen:annotation
Controller
Scan source code files from app/controllers
directory then
generated to app/annotations/annotation_controllers.go
.
Crontab
Scan source code files from app/crontabs
directory then
generated to app/annotations/annotation_crontabs.go
.
Accepts
@Crontab
("0 */1 * * * * *
") - required,
@CrontabGlobalUnique
(bool
) - optional, default: false
@CrontabNodeUnique
(bool
) - optional, default: true
@CrontabRunOnStartup
(bool
) - optional, default: false
Example
// ExampleCrontab
// example crontab processor.
//
// @Crontab("0 */1 * * * * *")
// @CrontabGlobalUnique(true)
// @CrontabNodeUnique(true)
// @CrontabRunOnStartup(true)
type ExampleCrontab struct{}
// Required methods.
func (o *ExampleCrontab) OnRun(ctx context.Context) (err error) {return}
// Optional methods.
func (o *ExampleCrontab) OnBefore(ctx context.Context) (err error) {return}
func (o *ExampleCrontab) OnFailed(ctx context.Context, err error) {}
func (o *ExampleCrontab) OnFinish(ctx context.Context) {}
func (o *ExampleCrontab) OnSucceed(ctx context.Context) {}
Handler
Middleware
Scan source code files from app/middlewares
directory then
generated to app/annotations/annotation_middlewares.go
.
Accepts
Example 1
with same name in app/middlewares/example.go
without annotation like follows.
// Example
// is an example middleware.
func Example(i iris.Context){
i.Next()
}
// PostLogin
// send login request with POST method.
//
// @Middleware("Example")
func (o *Controller) PostLogin(i iris.Context){
...
}
Example 2
with custom name in app/middlewares/example.go
with @Name
modify like follows.
// Example
// is an example middleware.
//
// @Name("custom")
func Example(i iris.Context){
i.Next()
}
// PostLogin
// send login request with POST method.
//
// @Middleware("custom")
func (o *Controller) PostLogin(i iris.Context){
...
}