Add docs and set the baseurl
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2019-03-19 22:19:17 +01:00
parent ca146fe5a0
commit 2b9150a5a3
Signed by: peter
GPG Key ID: 374322D56E5209E8

View File

@ -17,10 +17,12 @@ import (
"p83.nl/go/ekster/pkg/server" "p83.nl/go/ekster/pkg/server"
) )
// Constants
const ( const (
ClientID string = "https://p83.nl/microsub-client" ClientID string = "https://p83.nl/microsub-client"
) )
// AppOptions are options for the app
type AppOptions struct { type AppOptions struct {
Port int Port int
AuthEnabled bool AuthEnabled bool
@ -46,6 +48,7 @@ func newPool(addr string) *redis.Pool {
} }
} }
// WithAuth adds authorization to a http.Handler
func WithAuth(handler http.Handler, b *memoryBackend) http.Handler { func WithAuth(handler http.Handler, b *memoryBackend) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions { if r.Method == http.MethodOptions {
@ -73,21 +76,23 @@ func WithAuth(handler http.Handler, b *memoryBackend) http.Handler {
}) })
} }
// App is the main app structure
type App struct { type App struct {
options AppOptions options AppOptions
backend *memoryBackend backend *memoryBackend
hubBackend *hubIncomingBackend hubBackend *hubIncomingBackend
} }
// Run runs the app
func (app *App) Run() { func (app *App) Run() {
app.backend.run() app.backend.run()
app.hubBackend.run() app.hubBackend.run()
log.Printf("Listening on port %d\n", app.options.Port) log.Printf("Listening on port %d\n", app.options.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", app.options.Port), nil)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", app.options.Port), nil))
} }
// NewApp initializes the App
func NewApp(options AppOptions) *App { func NewApp(options AppOptions) *App {
app := &App{ app := &App{
options: options, options: options,
@ -95,6 +100,7 @@ func NewApp(options AppOptions) *App {
app.backend = loadMemoryBackend() app.backend = loadMemoryBackend()
app.backend.AuthEnabled = options.AuthEnabled app.backend.AuthEnabled = options.AuthEnabled
app.backend.baseURL = options.BaseURL
app.hubBackend = &hubIncomingBackend{app.backend, options.BaseURL} app.hubBackend = &hubIncomingBackend{app.backend, options.BaseURL}