-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (57 loc) · 1.72 KB
/
Copy pathmain.go
File metadata and controls
70 lines (57 loc) · 1.72 KB
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
package main
import (
"embed"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/dawgdevv/tick/internal/db"
"github.com/dawgdevv/tick/internal/handlers"
)
//go:embed internal/webdist/* internal/webdist/assets/*
var webFiles embed.FS
var (
addr = flag.String("addr", ":8080", "address to listen on")
dbPath = flag.String("db", "tick.db", "path to SQLite database")
)
func main() {
flag.Parse()
if err := os.MkdirAll(filepath.Dir(*dbPath), 0755); err != nil {
log.Fatalf("Failed to create db directory: %v", err)
}
database, err := db.Open(*dbPath)
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
defer database.Close()
if err := database.Migrate(); err != nil {
log.Fatalf("Failed to migrate database: %v", err)
}
mux := http.NewServeMux()
dist, err := fs.Sub(webFiles, "internal/webdist")
if err != nil {
log.Fatal(err)
}
mux.Handle("/", http.FileServer(http.FS(dist)))
api := handlers.NewAPI(database)
mux.HandleFunc("/api/tasks", api.Tasks)
mux.HandleFunc("/api/tasks/timer", api.TaskTimer)
mux.HandleFunc("/api/subtasks", api.Subtasks)
mux.HandleFunc("/api/subtask", api.Subtask)
mux.HandleFunc("/api/scratchpad", api.Scratchpad)
mux.HandleFunc("/api/quicklinks", api.Quicklinks)
mux.HandleFunc("/api/quicklinks/export", api.QuicklinksExport)
mux.HandleFunc("/api/quicklinks/import", api.QuicklinksImport)
mux.HandleFunc("/api/time", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"time":"%s","date":"%s"}`,
time.Now().Format("15:04:05"),
time.Now().Format("02/01/2006"))
})
log.Printf("Tick running at http://localhost%s", *addr)
log.Fatal(http.ListenAndServe(*addr, mux))
}