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

package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/sessions"
"forge.cadoles.com/wpetit/goweb/session/gorilla"
"forge.cadoles.com/wpetit/goweb/template/html"
"github.com/go-chi/chi/middleware"
"forge.cadoles.com/wpetit/goweb/service/session"
"forge.cadoles.com/wpetit/goweb/service/template"
"forge.cadoles.com/Pyxis/orion/cmd/server/jsonrpc"
containerMiddleware "forge.cadoles.com/wpetit/goweb/middleware/container"
"forge.cadoles.com/wpetit/goweb/service"
"github.com/go-chi/chi"
)
var (
conf = newDefaultConfig()
)
func main() {
if err := overwriteFromEnv(conf); err != nil {
log.Fatal(err)
}
// Create service container
container := service.NewContainer()
container.Provide(jsonrpc.ServiceName, getJSONRPCServiceProvider())
container.Provide(template.ServiceName, html.ServiceProvider(conf.TemplateDir))
container.Provide(session.ServiceName, getSessionServiceProvider())
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(containerMiddleware.ServiceContainer(container))
configureRoutes(r)
hostStr := fmt.Sprintf("%s:%s", conf.HTTPHost, conf.HTTPPort)
log.Printf("listening on http://%s", hostStr)
if err := http.ListenAndServe(hostStr, r); err != nil {
log.Fatal(err)
}
}
func getJSONRPCServiceProvider() service.Provider {
jsonrpc := jsonrpc.NewServer()
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
}
}