-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_linux.go
More file actions
94 lines (77 loc) · 2.21 KB
/
main_linux.go
File metadata and controls
94 lines (77 loc) · 2.21 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"embed"
"fmt"
"log"
"mime"
"net/http"
"os"
"time"
"github.com/gluek/timetracker/internal/database"
"github.com/gluek/timetracker/internal/handlers"
"github.com/gluek/timetracker/internal/routes"
"github.com/spf13/viper"
)
//go:embed internal/assets/css/input.css
//go:embed internal/assets/favicon.ico
//go:embed internal/assets/js/htmx.min.js
//go:embed internal/assets/js/echarts.min.js
var content embed.FS
//internal/assets/js/echarts.js
func main() {
// Init session
viperInit()
var err error
if viper.GetBool("logfile") {
logfile, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("could not open logfile: %v", err)
}
defer logfile.Close()
log.SetOutput(logfile)
}
handlers.HandlerInit()
database.Connect()
defer database.Close()
mux := http.NewServeMux()
mux.HandleFunc("/", handlers.HomePage)
routes.RegisterRecordRoutes(mux)
routes.RegisterProjectRoutes(mux)
routes.RegisterOtherRoutes(mux)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(content))))
err = mime.AddExtensionType(".js", "application/javascript")
if err != nil {
log.Printf("error add mime: %v", err)
}
server := &http.Server{
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 2 * time.Second,
Handler: mux,
Addr: fmt.Sprintf("localhost:%d", viper.GetInt("port")),
}
log.Printf("Running in DEBUG Mode")
log.Printf("Listening on http://localhost:%d\n", viper.GetInt("port"))
if err := server.ListenAndServe(); err != nil {
log.Printf("error listening: %v", err)
}
}
func viperInit() {
viper.SetDefault("port", 34115)
viper.SetDefault("worktime_per_week", "39h0m0s")
viper.SetDefault("offset_overtime", "0h0m0s")
viper.SetDefault("logfile", false)
viper.SetDefault("decimal_separator", ",")
viper.SetConfigName("timetrackerconf")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
viper.SafeWriteConfig()
log.Println("Config file not found, creating...")
} else {
log.Println(err)
}
}
}