zdravko/cmd/server/main.go

40 lines
804 B
Go
Raw Normal View History

package main
import (
"log"
"net/http"
2024-02-11 09:15:00 +00:00
"os"
"github.com/gorilla/mux"
"code.tjo.space/mentos1386/zdravko/internal"
"code.tjo.space/mentos1386/zdravko/internal/pages"
2024-02-11 10:56:21 +00:00
"code.tjo.space/mentos1386/zdravko/internal/static"
)
func main() {
2024-02-11 09:15:00 +00:00
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
r := mux.NewRouter()
2024-02-11 10:56:21 +00:00
db, err := internal.ConnectToDatabase()
if err != nil {
log.Fatal(err)
}
log.Println("Connected to database")
page := pages.NewPageHandler(db)
// Server static files
2024-02-11 10:56:21 +00:00
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static.Static))))
2024-02-11 10:56:21 +00:00
r.HandleFunc("/", page.Index).Methods("GET")
r.HandleFunc("/settings", page.Settings).Methods("GET")
2024-02-11 09:15:00 +00:00
log.Println("Server started on", port)
log.Fatal(http.ListenAndServe(":"+port, r))
}