From 5dd8de09de434578a96af687996530dd1746f8f4 Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 6 Aug 2023 22:54:08 +0000 Subject: [PATCH] family is home --- snippetbox/cmd/web/main.go | 25 ++++++++++++++++++++- snippetbox/cmd/web/routes.go | 2 +- snippetbox/go.mod | 2 ++ snippetbox/go.sum | 2 ++ snippetbox/internal/models/snippets.go | 30 ++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 snippetbox/go.sum create mode 100644 snippetbox/internal/models/snippets.go diff --git a/snippetbox/cmd/web/main.go b/snippetbox/cmd/web/main.go index bfa4870..818448b 100644 --- a/snippetbox/cmd/web/main.go +++ b/snippetbox/cmd/web/main.go @@ -1,25 +1,37 @@ package main import ( + "database/sql" "flag" "log" "net/http" "os" + + "gitea.wagshome.duckdns.org/nathan/golearn/internal/models" + _ "github.com/go-sql-driver/mysql" ) type application struct { errorLog *log.Logger infoLog *log.Logger + snippets *models.SnippetModel } func main() { addr := flag.String("addr", ":4000", "HTTP network address") + dsn := flag.String("dsn", "web:pass@/snippetbox?parseTime=true", "MySQL data source name") flag.Parse() infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime) errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile) + db, err := openDB(*dsn) + if err != nil { + errorLog.Fatal(err) + } + defer db.Close() app := &application{ errorLog: errorLog, infoLog: infoLog, + snippets: &models.SnippetModel{DB: db}, } srv := &http.Server{ @@ -28,6 +40,17 @@ func main() { Handler: app.routes(), } infoLog.Printf("Starting server on %s", *addr) - err := srv.ListenAndServe() + err = srv.ListenAndServe() errorLog.Fatal(err) } + +func openDB(dsn string) (*sql.DB, error) { + db, err := sql.Open("mysql", dsn) + if err != nil { + return nil, err + } + if err = db.Ping(); err != nil { + return nil, err + } + return db, nil +} diff --git a/snippetbox/cmd/web/routes.go b/snippetbox/cmd/web/routes.go index aa89d39..440708a 100644 --- a/snippetbox/cmd/web/routes.go +++ b/snippetbox/cmd/web/routes.go @@ -2,7 +2,7 @@ package main import "net/http" -func (app *application) routes() *http.ServerMux { +func (app *application) routes() *http.ServeMux { mux := http.NewServeMux() fileServer := http.FileServer(http.Dir("./ui/static/")) mux.Handle("/static", http.StripPrefix("/static", fileServer)) diff --git a/snippetbox/go.mod b/snippetbox/go.mod index 56729d7..2540843 100644 --- a/snippetbox/go.mod +++ b/snippetbox/go.mod @@ -1,3 +1,5 @@ module gitea.wagshome.duckdns.org/nathan/golearn go 1.20 + +require github.com/go-sql-driver/mysql v1.7.1 // indirect diff --git a/snippetbox/go.sum b/snippetbox/go.sum new file mode 100644 index 0000000..fd7ae07 --- /dev/null +++ b/snippetbox/go.sum @@ -0,0 +1,2 @@ +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= diff --git a/snippetbox/internal/models/snippets.go b/snippetbox/internal/models/snippets.go new file mode 100644 index 0000000..1f1ca71 --- /dev/null +++ b/snippetbox/internal/models/snippets.go @@ -0,0 +1,30 @@ +package models + +import ( + "database/sql" + "time" +) + +type Snippet struct { + ID int + Title string + Content string + Created time.Time + Expires time.Time +} + +type SnippetModel struct { + DB *sql.DB +} + +func (m *SnippetModel) Insert(title string, content string, expires int) (int, error) { + return 0, nil +} + +func (m *SnippetModel) Get(id int) (*Snippet, error) { + return nil, nil +} + +func (m *SnippetModel) Latest() ([]*Snippet, error) { + return nil, nil +}