-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (89 loc) · 2.52 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"time"
"github.com/dominikbraun/timetrace/config"
"github.com/dominikbraun/timetrace/core"
"github.com/dominikbraun/timetrace/fs"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memstore"
"github.com/gin-gonic/gin"
)
var timetrace *core.Timetrace
//go:embed: images/favicon.ico
var icon embed.FS
//go:embed html/* images/*
var f embed.FS
func PrintDuration(d time.Duration) string {
return fmt.Sprint(d)
}
func main() {
config := config.Get()
file := fs.New(config)
timetrace = core.New(config, file)
if err := timetrace.EnsureDirectories(); err != nil {
log.Fatal("unable to create dirs", err)
}
router := SetupRouter()
router.Run(":8090")
}
func SetupRouter() *gin.Engine {
funcMap := template.FuncMap{
"printDuration": PrintDuration,
}
store := memstore.NewStore([]byte("secret"))
session := sessions.Sessions("timetrace", store)
options := sessions.Options{MaxAge: 7200}
fmt.Println("options\n", options)
router := gin.Default()
router.Use(session)
//router.LoadHTMLGlob("html/*")
templates := template.Must(template.New("").Funcs(funcMap).ParseFS(f, "html/*"))
router.SetHTMLTemplate(templates)
//router.StaticFile("favicon.ico", "./images/favicon.ico")
router.StaticFS("/favicon.ico", http.FS(icon))
//router.Static("images", "./images")
router.GET("/images/*filepath", func(c *gin.Context) {
c.FileFromFS(c.Request.URL.Path, http.FS(f))
})
//router.StaticFS("/images", http.FS(f))
router.POST("/newuser", NewUser)
router.POST("/login", ProcessLogin)
router.GET("/logout", Logout)
restricted := router.Group("/", AuthRequired)
{
restricted.GET("/", DisplayLanding)
restricted.POST("/", StartStop)
restricted.POST("/create_project", CreateProject)
restricted.POST("/delete_project", DeleteProject)
restricted.POST("/reports", GenerateReport)
restricted.POST("/edit", EditRecord)
}
return router
}
func AuthRequired(c *gin.Context) {
session := sessions.Default(c)
loggedIn := session.Get("loggedIn")
fmt.Println("loggedIn status: ", loggedIn)
if loggedIn != true {
home := os.Getenv("HOME")
_, err := os.Open(home + "/.timetrace/users.json")
if err != nil {
fmt.Println("error opening user file: ", err)
c.HTML(http.StatusOK, "New", gin.H{"message": err})
c.Abort()
return
}
message := session.Get("error")
fmt.Println("user exists --- message\n", message)
c.HTML(http.StatusOK, "Login", gin.H{"messge": message})
c.Abort()
return
}
fmt.Println("authorized - good to go")
}