family is home

main
nathan 1 year ago
parent 5181aabd14
commit 5dd8de09de

@ -1,25 +1,37 @@
package main package main
import ( import (
"database/sql"
"flag" "flag"
"log" "log"
"net/http" "net/http"
"os" "os"
"gitea.wagshome.duckdns.org/nathan/golearn/internal/models"
_ "github.com/go-sql-driver/mysql"
) )
type application struct { type application struct {
errorLog *log.Logger errorLog *log.Logger
infoLog *log.Logger infoLog *log.Logger
snippets *models.SnippetModel
} }
func main() { func main() {
addr := flag.String("addr", ":4000", "HTTP network address") addr := flag.String("addr", ":4000", "HTTP network address")
dsn := flag.String("dsn", "web:pass@/snippetbox?parseTime=true", "MySQL data source name")
flag.Parse() flag.Parse()
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime) infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile) 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{ app := &application{
errorLog: errorLog, errorLog: errorLog,
infoLog: infoLog, infoLog: infoLog,
snippets: &models.SnippetModel{DB: db},
} }
srv := &http.Server{ srv := &http.Server{
@ -28,6 +40,17 @@ func main() {
Handler: app.routes(), Handler: app.routes(),
} }
infoLog.Printf("Starting server on %s", *addr) infoLog.Printf("Starting server on %s", *addr)
err := srv.ListenAndServe() err = srv.ListenAndServe()
errorLog.Fatal(err) 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
}

@ -2,7 +2,7 @@ package main
import "net/http" import "net/http"
func (app *application) routes() *http.ServerMux { func (app *application) routes() *http.ServeMux {
mux := http.NewServeMux() mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir("./ui/static/")) fileServer := http.FileServer(http.Dir("./ui/static/"))
mux.Handle("/static", http.StripPrefix("/static", fileServer)) mux.Handle("/static", http.StripPrefix("/static", fileServer))

@ -1,3 +1,5 @@
module gitea.wagshome.duckdns.org/nathan/golearn module gitea.wagshome.duckdns.org/nathan/golearn
go 1.20 go 1.20
require github.com/go-sql-driver/mysql v1.7.1 // indirect

@ -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=

@ -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
}
Loading…
Cancel
Save