-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
132 lines (125 loc) · 3.07 KB
/
router.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"embed"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/devilcove/timetraced/database"
"github.com/devilcove/timetraced/models"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
)
//go:embed: images/favicon.ico
var icon embed.FS
func setupRouter() *gin.Engine {
//gin.SetMode(gin.ReleaseMode)
secret, ok := os.LookupEnv("SESSION_SECRET")
if !ok {
secret = "secret"
}
store := cookie.NewStore([]byte(secret))
session := sessions.Sessions("time", store)
router := gin.Default()
router.LoadHTMLGlob("html/*.html")
router.Static("images", "./images")
router.Static("assets", "./assets")
router.StaticFS("/favicon.ico", http.FS(icon))
//router.SetHTMLTemplate(template.Must(template.New("").Parse("html/*")))
_ = router.SetTrustedProxies(nil)
router.Use(gin.Recovery(), session)
users := router.Group("/users", auth)
{
users.GET("", getUsers)
users.GET("current", getUser)
users.POST("", addUser)
users.POST(":name", editUser)
users.DELETE(":name", deleteUser)
users.GET(":name", getUser)
}
router.GET("/login", displayLogin)
router.POST("/login", login)
router.GET("/logout", logout)
router.GET("/register", register)
router.GET("/", displayMain)
router.POST("/register", regUser)
projects := router.Group("/projects", auth)
{
projects.GET("", getProjects)
projects.GET("/add", displayProjectForm)
projects.POST("", addProject)
projects.GET("/:name", getProject)
projects.POST("/:name/start", start)
projects.POST("/stop", stop)
projects.GET("/status", displayStatus)
}
reports := router.Group("/reports", auth)
{
reports.GET("", report)
reports.POST("", getReport)
}
records := router.Group("records", auth)
{
records.GET("/:id", getRecord)
records.POST("/:id", editRecord)
}
configuration := router.Group("/config", auth)
{
configuration.GET("/", config)
configuration.POST("/", setConfig)
}
return router
}
func processError(c *gin.Context, status int, message string) {
pc, fn, line, _ := runtime.Caller(1)
source := fmt.Sprintf("%s[%s:%d]", runtime.FuncForPC(pc).Name(), filepath.Base(fn), line)
slog.Error(message, "status", status, "source", source)
c.HTML(status, "error", message)
c.Abort()
}
func auth(c *gin.Context) {
session := sessions.Default(c)
loggedIn := session.Get("loggedin")
if loggedIn == nil {
slog.Info("not logged in display login page")
page := models.GetPage()
page.NeedsLogin = true
c.HTML(http.StatusOK, "login", page)
c.Abort()
return
}
}
func checkDefaultUser() {
user := os.Getenv("USER")
pass := os.Getenv("PASS")
users, err := database.GetAllUsers()
if err != nil {
log.Fatal(err)
}
if len(users) > 1 {
slog.Debug("user exists")
return
}
if user == "" {
user = "admin"
}
if pass == "" {
pass = "password"
}
password, err := hashPassword(pass)
if err != nil {
slog.Error("hash error", "error", err)
}
_ = database.SaveUser(&models.User{
Username: user,
Password: password,
IsAdmin: true,
Updated: time.Now(),
})
slog.Info("default user created")
}