2017-10-09 14:26:46 +02:00
|
|
|
// file: main.go
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/_examples/mvc/overview/datasource"
|
|
|
|
"github.com/kataras/iris/v12/_examples/mvc/overview/repositories"
|
|
|
|
"github.com/kataras/iris/v12/_examples/mvc/overview/services"
|
|
|
|
"github.com/kataras/iris/v12/_examples/mvc/overview/web/controllers"
|
|
|
|
"github.com/kataras/iris/v12/_examples/mvc/overview/web/middleware"
|
2017-10-09 14:26:46 +02:00
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/mvc"
|
2017-10-09 14:26:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
2017-12-16 05:38:28 +01:00
|
|
|
app.Logger().SetLevel("debug")
|
2017-10-10 03:46:32 +02:00
|
|
|
|
2017-10-09 14:26:46 +02:00
|
|
|
// Load the template files.
|
2017-10-12 02:51:06 +02:00
|
|
|
app.RegisterView(iris.HTML("./web/views", ".html"))
|
2017-10-09 14:26:46 +02:00
|
|
|
|
2017-12-27 03:15:41 +01:00
|
|
|
// Serve our controllers.
|
|
|
|
mvc.New(app.Party("/hello")).Handle(new(controllers.HelloController))
|
2017-12-16 05:38:28 +01:00
|
|
|
// You can also split the code you write to configure an mvc.Application
|
2017-12-27 03:15:41 +01:00
|
|
|
// using the `mvc.Configure` method, as shown below.
|
|
|
|
mvc.Configure(app.Party("/movies"), movies)
|
2017-10-10 03:46:32 +02:00
|
|
|
|
2017-10-09 14:26:46 +02:00
|
|
|
// http://localhost:8080/hello
|
|
|
|
// http://localhost:8080/hello/iris
|
2017-10-12 02:51:06 +02:00
|
|
|
// http://localhost:8080/movies
|
2017-10-09 14:26:46 +02:00
|
|
|
// http://localhost:8080/movies/1
|
|
|
|
app.Run(
|
2017-12-16 05:38:28 +01:00
|
|
|
// Start the web server at localhost:8080
|
2017-10-09 14:26:46 +02:00
|
|
|
iris.Addr("localhost:8080"),
|
2017-12-16 05:38:28 +01:00
|
|
|
// skip err server closed when CTRL/CMD+C pressed:
|
2017-10-09 14:26:46 +02:00
|
|
|
iris.WithoutServerError(iris.ErrServerClosed),
|
2017-12-16 05:38:28 +01:00
|
|
|
// enables faster json serialization and more:
|
|
|
|
iris.WithOptimizations,
|
2017-10-09 14:26:46 +02:00
|
|
|
)
|
|
|
|
}
|
2017-12-16 05:38:28 +01:00
|
|
|
|
|
|
|
// note the mvc.Application, it's not iris.Application.
|
|
|
|
func movies(app *mvc.Application) {
|
|
|
|
// Add the basic authentication(admin:password) middleware
|
|
|
|
// for the /movies based requests.
|
|
|
|
app.Router.Use(middleware.BasicAuth)
|
|
|
|
|
|
|
|
// Create our movie repository with some (memory) data from the datasource.
|
|
|
|
repo := repositories.NewMovieRepository(datasource.Movies)
|
|
|
|
// Create our movie service, we will bind it to the movie app's dependencies.
|
|
|
|
movieService := services.NewMovieService(repo)
|
2017-12-27 03:15:41 +01:00
|
|
|
app.Register(movieService)
|
2017-12-16 05:38:28 +01:00
|
|
|
|
2017-12-27 03:15:41 +01:00
|
|
|
// serve our movies controller.
|
|
|
|
// Note that you can serve more than one controller
|
|
|
|
// you can also create child mvc apps using the `movies.Party(relativePath)` or `movies.Clone(app.Party(...))`
|
|
|
|
// if you want.
|
|
|
|
app.Handle(new(controllers.MovieController))
|
2017-12-16 05:38:28 +01:00
|
|
|
}
|