adonis/main.go

57 lines
1.2 KiB
Go

package main
import (
"html/template"
"log"
"net/http"
"os"
db "git.keegandeppe.com/kdeppe/adonis/internal/db"
)
type Todo struct {
Title string
Done bool
}
type TodoPageData struct {
PageTitle string
Todos []Todo
}
func main() {
base := template.Must(template.ParseFiles("static/index.html"))
tmpl := template.Must(template.ParseFiles("static/layout.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
base.Execute(w, nil)
})
http.HandleFunc("/htmx.min.js", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/js/htmx.min.js")
})
http.HandleFunc("/gettasks", func(w http.ResponseWriter, r *http.Request) {
data := TodoPageData{
PageTitle: "My TODO list",
Todos: []Todo{
{Title: "test", Done: false},
},
}
tmpl.Execute(w, data)
})
// setup database connection based on env vars
username := os.Getenv("POSTGRES_USER")
password := os.Getenv("POSTGRES_PASSWORD")
database := os.Getenv("POSTGRES_DB")
hostname := os.Getenv("POSTGRES_HOSTNAME")
go db.NewDatabase(username, password, hostname, database)
// attach webserver
log.Fatal(http.ListenAndServe(":80", nil))
// crypto.Test()
// os.Exit(0)
}