-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (59 loc) · 2.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"database/sql"
"fmt"
"net/http"
"os"
"github.com/a-h/templ"
"github.com/joelramilison/timespent/internal/database"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
func main() {
dbUrl := loadEnv()
db, err := sql.Open("postgres", dbUrl)
if err != nil {
panic(err)
}
cfg := &apiConfig{DB: database.New(db)}
mux := http.NewServeMux()
server := http.Server{Handler: mux}
mux.Handle("GET /login", templ.Handler(login(nil)))
mux.Handle("GET /register", templ.Handler(register()))
mux.HandleFunc("POST /users", cfg.registerUserHandler)
mux.HandleFunc("GET /{$}", cfg.middlewareAuth(cfg.appHandler))
mux.HandleFunc("POST /login", cfg.loginHandler)
mux.HandleFunc("GET /stopwatch", cfg.middlewareAuth(cfg.stopWatchHandler))
mux.HandleFunc("POST /sessions/start", cfg.middlewareAuth(cfg.startSessionHandler))
mux.Handle("POST /sessions/stop", cfg.middlewareAuth(cfg.stopSessionHandler))
mux.HandleFunc("POST /sessions/confirm-stop", cfg.middlewareAuth(cfg.confirmSessionStopHandler))
mux.Handle("POST /sessions/abort-stop", cfg.middlewareAuth(cfg.abortStopHandler))
mux.HandleFunc("POST /logout", cfg.middlewareAuth(cfg.logoutHandler))
mux.HandleFunc("POST /sessions/pause", cfg.middlewareAuth(cfg.pauseSessionHandler))
mux.HandleFunc("POST /sessions/resume", cfg.middlewareAuth(cfg.resumeSessionHandler))
mux.Handle("GET /css/", http.FileServer(http.Dir(".")))
mux.Handle("GET /fonts/", http.FileServer(http.Dir(".")))
mux.Handle("GET /assets/", http.FileServer(http.Dir(".")))
mux.HandleFunc("POST /activities-page", cfg.middlewareAuth(cfg.activitiesPageHandler))
mux.HandleFunc("POST /activities", cfg.middlewareAuth(cfg.createActivityHandler))
mux.HandleFunc("POST /activities/edit", cfg.middlewareAuth(cfg.activityEditMenuHandler))
mux.HandleFunc("POST /activities/delete/{id}", cfg.middlewareAuth(cfg.deleteActivityHandler))
mux.HandleFunc("POST /activities/confirm/{id}", cfg.middlewareAuth(cfg.confirmDeleteActivityHandler))
fmt.Println("Starting server ...")
err = server.ListenAndServe()
fmt.Printf("Server closed with error %v", err)
}
type apiConfig struct {
DB *database.Queries
}
func loadEnv() (string) {
err := godotenv.Load()
if err != nil {
panic(err)
}
dbUrl := os.Getenv("DB_CONNECTION_STRING")
if dbUrl == "" {
panic("empty dbUrl from environment variables")
}
return dbUrl
}