go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/server/main.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 main 9 10 import ( 11 "context" 12 "flag" 13 "fmt" 14 "net" 15 "os" 16 17 "go.charczuk.com/sdk/apputil" 18 "go.charczuk.com/sdk/db" 19 "go.charczuk.com/sdk/db/dbutil" 20 "go.charczuk.com/sdk/errutil" 21 "go.charczuk.com/sdk/graceful" 22 "go.charczuk.com/sdk/logutil" 23 "go.charczuk.com/sdk/oauth" 24 "go.charczuk.com/sdk/slant" 25 "go.charczuk.com/sdk/web" 26 27 "go.charczuk.com/projects/nodes/pkg/config" 28 "go.charczuk.com/projects/nodes/pkg/controller" 29 "go.charczuk.com/projects/nodes/pkg/dbmodel" 30 ) 31 32 var entrypoint = apputil.DBEntryPoint[config.Config]{ 33 Start: func(ctx context.Context, cfg config.Config, dbc *db.Connection) error { 34 slant.Print(os.Stdout, "nodes-srv") 35 36 log := logutil.GetLogger(ctx) 37 app := web.New() 38 app.BaseContext = func(_ net.Listener) context.Context { 39 return logutil.WithLogger(context.Background(), log) 40 } 41 app.RegisterConfig(cfg.Web) 42 app.RegisterLoggerListeners(log) 43 44 app.PanicAction = func(r web.Context, err interface{}) web.Result { 45 return web.JSON().InternalError(errutil.New(err)) 46 } 47 48 if flagNextProxyURL != "" { 49 logutil.Infof(log, "using next proxy url: %v", flagNextProxyURL) 50 } else { 51 logutil.Infof(log, "using production next client assets") 52 } 53 54 oauthMgr, err := oauth.New( 55 context.Background(), 56 oauth.OptConfig(cfg.OAuth), 57 ) 58 if err != nil { 59 if cfg.Config.Meta.ServiceEnv == "prod" { 60 return err 61 } 62 log.Printf("oauth initialization failed: %+v", err) 63 } 64 appModelMgr := apputil.NewModelManager(dbc) 65 66 temporalClient, _ := cfg.Temporal.Client() 67 // if err != nil { 68 // return err 69 // } 70 71 app.RegisterControllers( 72 controller.Index{ 73 NextProxyURL: flagNextProxyURL, 74 }, 75 apputil.Auth{ 76 Config: cfg.Config, 77 DB: appModelMgr, 78 OAuth: oauthMgr, 79 AuthedRedirectPath: "/", 80 }, 81 controller.Static{ 82 NextProxyURL: flagNextProxyURL, 83 }, 84 controller.API{ 85 Temporal: temporalClient, 86 DB: &dbmodel.Manager{ 87 BaseManager: dbutil.NewBaseManager(dbc), 88 }, 89 }, 90 ) 91 if err := app.Initialize(); err != nil { 92 err = fmt.Errorf("app initialization failure: %w", err) 93 return err 94 } 95 return graceful.StartForShutdown(ctx, 96 app, 97 ) 98 }, 99 } 100 101 var flagNextProxyURL string 102 103 func init() { 104 flag.StringVar(&flagNextProxyURL, "next-proxy-url", "", "The url of the local development next instance to proxy requests to") 105 entrypoint.Init() 106 } 107 108 func main() { 109 entrypoint.Main() 110 }