This repository has been archived on 2024-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
orion/cmd/server/main.go

89 lines
1.9 KiB
Go
Raw Normal View History

2018-09-17 14:17:54 +02:00
package main
import (
"fmt"
"log"
"net/http"
2018-12-06 22:12:32 +01:00
"github.com/gorilla/sessions"
"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
"forge.cadoles.com/wpetit/goweb/service/session"
"forge.cadoles.com/wpetit/goweb/service/template"
"forge.cadoles.com/Pyxis/orion/cmd/server/jsonrpc"
2019-02-12 10:17:06 +01:00
containerMiddleware "forge.cadoles.com/wpetit/goweb/middleware/container"
2018-12-06 22:12:32 +01:00
"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())
2019-02-12 10:17:06 +01:00
container.Provide(template.ServiceName, html.ServiceProvider(conf.TemplateDir))
2018-12-06 22:12:32 +01:00
container.Provide(session.ServiceName, getSessionServiceProvider())
2018-09-17 14:17:54 +02:00
2019-02-12 10:17:06 +01:00
r := chi.NewRouter()
2018-09-17 14:17:54 +02:00
2019-02-12 10:17:06 +01:00
r.Use(middleware.Recoverer)
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(containerMiddleware.ServiceContainer(container))
2018-12-06 22:12:32 +01:00
2019-02-12 10:17:06 +01:00
configureRoutes(r)
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)
2019-02-12 10:17:06 +01:00
if err := http.ListenAndServe(hostStr, r); err != nil {
2018-09-17 14:17:54 +02:00
log.Fatal(err)
}
}
2018-12-06 22:12:32 +01:00
func getJSONRPCServiceProvider() service.Provider {
2019-02-12 10:17:06 +01:00
jsonrpc := jsonrpc.NewServer()
2018-12-06 22:12:32 +01:00
return func(c *service.Container) (interface{}, error) {
return jsonrpc, 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
}
}