88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"gitlab.com/skillz/bff/auth"
|
|
"gitlab.com/skillz/bff/storage"
|
|
)
|
|
|
|
/*
|
|
CookieName is the name of the cookie used to identify sessions.
|
|
*/
|
|
const CookieName = `BFFSESSID`
|
|
|
|
/*
|
|
DefaultDSN is the default MongoDB DSN.
|
|
*/
|
|
const DefaultDSN = `mongodb://localhost:27017`
|
|
|
|
/*
|
|
DefaultListenAddr is the default TCP listening address in canonical Go format.
|
|
*/
|
|
const DefaultListenAddr = `:8000`
|
|
|
|
/*
|
|
MountPath is the path below which the authboss paths are handled.
|
|
*/
|
|
const MountPath = `/auth`
|
|
|
|
/*
|
|
RootURL is the public absolute URL of the app. In production, will typically
|
|
be injected from the environment instead, to cover reverse proxying.
|
|
*/
|
|
const RootURL = `http://bff.localhost` + DefaultListenAddr + MountPath
|
|
|
|
func realMain(stdout, stderr io.Writer, flags *flag.FlagSet, args []string) {
|
|
// Load configuration from flags.
|
|
listenAddr := flag.String(`http`, DefaultListenAddr, `The listen address, like `+DefaultListenAddr)
|
|
dsn := flag.String(`mongo`, DefaultDSN, `The MongoDB server/cluster DSN, like `+DefaultDSN)
|
|
flags.Init(args[0], flag.ContinueOnError)
|
|
if err := flags.Parse(args[1:]); err != nil && err != flag.ErrHelp {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Connect to storage.
|
|
mongo, err := storage.New(context.Background(), *dsn)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Set up authentication.
|
|
ab, err := auth.NewAuthBoss(RootURL, CookieName, MountPath, mongo)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Set up routing.
|
|
r := mux.Router{}
|
|
r.HandleFunc(`/`, func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set(`Content-type`, `text/plain`)
|
|
fmt.Fprintln(w, `Root URL`)
|
|
})
|
|
ar := r.PathPrefix(MountPath).Subrouter()
|
|
ar.Handle(`/`, ab.Config.Core.Router)
|
|
|
|
// Start up.
|
|
if err = http.ListenAndServe(*listenAddr, &r); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
/*
|
|
Only use main to access stdlib globals, to avoid using them in normal code, to simplify testing.
|
|
*/
|
|
func main() {
|
|
log.SetOutput(os.Stderr)
|
|
realMain(os.Stdout, os.Stderr, // Streams
|
|
flag.CommandLine, os.Args, // Command line
|
|
)
|
|
}
|