2018-09-17 14:17:54 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2018-12-06 22:12:32 +01:00
|
|
|
"net/rpc"
|
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
|
|
|
|
"forge.cadoles.com/wpetit/goweb/static"
|
|
|
|
|
|
|
|
"forge.cadoles.com/wpetit/goweb/session/gorilla"
|
|
|
|
"forge.cadoles.com/wpetit/goweb/template/html"
|
2018-09-17 14:17:54 +02:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/middleware"
|
2018-12-06 22:12:32 +01:00
|
|
|
|
|
|
|
goweb "forge.cadoles.com/wpetit/goweb/middleware"
|
|
|
|
"forge.cadoles.com/wpetit/goweb/service/session"
|
|
|
|
"forge.cadoles.com/wpetit/goweb/service/template"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Pyxis/orion/cmd/server/jsonrpc"
|
|
|
|
"forge.cadoles.com/wpetit/goweb/service"
|
|
|
|
"github.com/go-chi/chi"
|
2018-09-17 14:17:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-12-06 22:12:32 +01:00
|
|
|
conf = newDefaultConfig()
|
2018-09-17 14:17:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
if err := overwriteFromEnv(conf); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-12-06 22:12:32 +01:00
|
|
|
// Create service container
|
|
|
|
container := service.NewContainer()
|
|
|
|
container.Provide(jsonrpc.ServiceName, getJSONRPCServiceProvider())
|
|
|
|
container.Provide(template.ServiceName, getTemplateServiceProvider(conf.TemplateDir))
|
|
|
|
container.Provide(session.ServiceName, getSessionServiceProvider())
|
2018-09-17 14:17:54 +02:00
|
|
|
|
2018-12-06 22:12:32 +01:00
|
|
|
router := chi.NewRouter()
|
2018-09-17 14:17:54 +02:00
|
|
|
|
2018-12-06 22:12:32 +01:00
|
|
|
router.Use(middleware.Recoverer)
|
|
|
|
router.Use(middleware.RequestID)
|
|
|
|
router.Use(middleware.Logger)
|
|
|
|
router.Use(goweb.ServiceContainer(container))
|
|
|
|
|
|
|
|
// Define routes
|
|
|
|
router.Get("/", serveHomepage)
|
|
|
|
router.Post("/rpc", handleJSONRPC)
|
|
|
|
router.Get("/**", static.Dir(conf.AssetDir, "", router.NotFoundHandler()))
|
2018-09-24 08:49:21 +02:00
|
|
|
|
2018-09-17 14:17:54 +02:00
|
|
|
hostStr := fmt.Sprintf("%s:%s", conf.HTTPHost, conf.HTTPPort)
|
|
|
|
|
2018-12-06 22:12:32 +01:00
|
|
|
log.Printf("listening on http://%s", hostStr)
|
|
|
|
if err := http.ListenAndServe(hostStr, router); err != nil {
|
2018-09-17 14:17:54 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-24 08:49:21 +02:00
|
|
|
|
2018-12-06 22:12:32 +01:00
|
|
|
func getJSONRPCServiceProvider() service.Provider {
|
|
|
|
jsonrpc := rpc.NewServer()
|
|
|
|
return func(c *service.Container) (interface{}, error) {
|
|
|
|
return jsonrpc, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTemplateServiceProvider(templateDir string) service.Provider {
|
|
|
|
|
|
|
|
// Create templateService at application startup
|
|
|
|
templateService := html.NewTemplateService()
|
|
|
|
|
|
|
|
// Load templates and keep error in cache
|
|
|
|
err := templateService.LoadTemplates(templateDir)
|
|
|
|
|
|
|
|
return func(c *service.Container) (interface{}, error) {
|
|
|
|
|
|
|
|
// If an error occured during the templates loading
|
|
|
|
// return the error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return templateService, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSessionServiceProvider() service.Provider {
|
|
|
|
|
|
|
|
// Create a new encrypted/authenticated cookie store for the session
|
|
|
|
cookieStore, err := gorilla.CreateCookieSessionStore(32, 64)
|
|
|
|
|
|
|
|
var sessionService session.Service
|
|
|
|
if err == nil {
|
|
|
|
sessionService = gorilla.NewSessionService(
|
|
|
|
"orion",
|
|
|
|
cookieStore,
|
|
|
|
&sessions.Options{
|
|
|
|
HttpOnly: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(c *service.Container) (interface{}, error) {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sessionService, nil
|
|
|
|
}
|
|
|
|
|
2018-09-24 08:49:21 +02:00
|
|
|
}
|