diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-18 11:06:32 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-18 11:06:32 +0100 |
commit | 20560419614e22fbb58567cdb0b88b54caf679f4 (patch) | |
tree | 163df44f07907d865b363db3bc198287ebd3d25f /cmd/web/main.go | |
parent | 435742cede199e3c85b5e2eb5a42ccbee4906a05 (diff) |
Adds code from ded-go-core - no database
D2S test app code removed (nginx error).
Adds all go code from `ded-go-core` using the basic Gov.UK UI for a test
page for DED.
Diffstat (limited to '')
-rw-r--r-- | cmd/web/main.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/cmd/web/main.go b/cmd/web/main.go new file mode 100644 index 0000000..5f4bfdf --- /dev/null +++ b/cmd/web/main.go @@ -0,0 +1,82 @@ +package main + +import ( + "database/sql" + "flag" + "html/template" + "log/slog" + "net/http" + "os" + + // _ "github.com/go-sql-driver/mysql" + "github.com/defencedigital/ded-web/internal/models" + // _ "github.com/lib/pq" +) + +type application struct { + logger *slog.Logger + operations *models.OperationModel + organisations *models.OrganisationModel + persons *models.PersonModel + engagementStrategies *models.EngagementStrategyModel + templateCache map[string]*template.Template +} + +func main() { + addr := flag.String("addr", ":4000", "HTTP network port") + // This uses "ded-db" in the connection string; this should be the name of the container running postgres. + // If not running this app inside a container, use "localhost". + //dsn := flag.String("dsn", "postgresql://postgres:secret@ded-db?sslmode=disable", "PostgreSQL data source name") + flag.Parse() + + logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) + + // Database connection + //db, err := openDB(*dsn) + //if err != nil { + // logger.Error(err.Error()) + // os.Exit(1) + //} + // + //defer db.Close() + + // initialise the new template cache... + templateCache, err := newTemplateCache() + if err != nil { + logger.Error(err.Error()) + os.Exit(1) + } + + app := &application{ + logger: logger, + //operations: &models.OperationModel{DB: db}, + //organisations: &models.OrganisationModel{DB: db}, + //persons: &models.PersonModel{DB: db}, + //engagementStrategies: &models.EngagementStrategyModel{DB: db}, + templateCache: templateCache, + } + + // mux := http.NewServeMux() + // mux.HandleFunc("/", home) + // log.Print("starting server on :4000") + logger.Info("starting server", "addr", *addr) + err = http.ListenAndServe(*addr, app.routes()) + logger.Error(err.Error()) + os.Exit(1) +} + +// openDB wraps sql.Open() and returns a sql.DB connection pool +// for a given DSN. +func openDB(dsn string) (*sql.DB, error) { + db, err := sql.Open("postgres", dsn) + if err != nil { + return nil, err + } + + err = db.Ping() + if err != nil { + db.Close() + return nil, err + } + return db, nil +} |