go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/chirp/pkg/controller/index.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package controller 9 10 import ( 11 "net/http" 12 "net/url" 13 14 "go.charczuk.com/sdk/apputil" 15 "go.charczuk.com/sdk/web" 16 17 "go.charczuk.com/projects/chirp/pkg/config" 18 "go.charczuk.com/projects/chirp/pkg/model" 19 "go.charczuk.com/projects/chirp/pkg/static" 20 ) 21 22 type Index struct { 23 apputil.BaseController 24 Model model.Manager 25 Config config.Config 26 27 NextProxyURL string 28 } 29 30 func (i Index) Register(app *web.App) { 31 if i.NextProxyURL != "" { 32 nextProxy := i.indexProxied() 33 app.Get("/", nextProxy) 34 app.NotFoundHandler = app.ActionHandler(nextProxy) 35 } else { 36 indexStatic := web.SessionAware(i.indexStatic) 37 app.Get("/", indexStatic) 38 app.NotFoundHandler = app.ActionHandler(indexStatic) 39 } 40 41 app.Get("/marketing", i.marketing) 42 } 43 44 func (i Index) indexProxied() web.Action { 45 nextProxyURL, _ := url.Parse(i.NextProxyURL) 46 nextProxy := web.ProxyAction(nextProxyURL) 47 return web.SessionAware(func(ctx web.Context) web.Result { 48 if ctx.Session() == nil { 49 return web.RedirectWithMethod(http.MethodGet, "/marketing") 50 } 51 return nextProxy(ctx) 52 }) 53 } 54 55 func (i Index) indexStatic(ctx web.Context) web.Result { 56 if ctx.Session() == nil { 57 return web.RedirectWithMethod(http.MethodGet, "/marketing") 58 } 59 return web.Static(ctx, http.FS(static.Next), "_nextjs/_dist/index.html") 60 } 61 62 func (i Index) marketing(ctx web.Context) web.Result { 63 return ctx.Views().View("marketing", nil) 64 }