Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/admin/cron.go (59 lines, 1.2 KiB) -rw-r--r-- file download

594ec41 Jakob Wakeling 2023-12-29 15:18:46
0
// Copyright (C) 2023, Jakob Wakeling
594ec41 Jakob Wakeling 2023-12-29 15:18:46
1
// All rights reserved.
594ec41 Jakob Wakeling 2023-12-29 15:18:46
2
594ec41 Jakob Wakeling 2023-12-29 15:18:46
3
package admin
594ec41 Jakob Wakeling 2023-12-29 15:18:46
4
594ec41 Jakob Wakeling 2023-12-29 15:18:46
5
import (
594ec41 Jakob Wakeling 2023-12-29 15:18:46
6
	"fmt"
594ec41 Jakob Wakeling 2023-12-29 15:18:46
7
	"log"
594ec41 Jakob Wakeling 2023-12-29 15:18:46
8
	"net/http"
594ec41 Jakob Wakeling 2023-12-29 15:18:46
9
	"time"
594ec41 Jakob Wakeling 2023-12-29 15:18:46
10
594ec41 Jakob Wakeling 2023-12-29 15:18:46
11
	"github.com/Jamozed/Goit/src/goit"
594ec41 Jakob Wakeling 2023-12-29 15:18:46
12
	"github.com/Jamozed/Goit/src/util"
594ec41 Jakob Wakeling 2023-12-29 15:18:46
13
)
594ec41 Jakob Wakeling 2023-12-29 15:18:46
14
594ec41 Jakob Wakeling 2023-12-29 15:18:46
15
func HandleCron(w http.ResponseWriter, r *http.Request) {
594ec41 Jakob Wakeling 2023-12-29 15:18:46
16
	auth, user, err := goit.Auth(w, r, true)
594ec41 Jakob Wakeling 2023-12-29 15:18:46
17
	if err != nil {
594ec41 Jakob Wakeling 2023-12-29 15:18:46
18
		log.Println("[/admin/cron]", err.Error())
594ec41 Jakob Wakeling 2023-12-29 15:18:46
19
		goit.HttpError(w, http.StatusInternalServerError)
594ec41 Jakob Wakeling 2023-12-29 15:18:46
20
		return
594ec41 Jakob Wakeling 2023-12-29 15:18:46
21
	}
594ec41 Jakob Wakeling 2023-12-29 15:18:46
22
594ec41 Jakob Wakeling 2023-12-29 15:18:46
23
	if !auth || !user.IsAdmin {
594ec41 Jakob Wakeling 2023-12-29 15:18:46
24
		goit.HttpError(w, http.StatusNotFound)
594ec41 Jakob Wakeling 2023-12-29 15:18:46
25
		return
594ec41 Jakob Wakeling 2023-12-29 15:18:46
26
	}
594ec41 Jakob Wakeling 2023-12-29 15:18:46
27
594ec41 Jakob Wakeling 2023-12-29 15:18:46
28
	type row struct{ Id, Repo, Schedule, Next, Last string }
594ec41 Jakob Wakeling 2023-12-29 15:18:46
29
	data := struct {
594ec41 Jakob Wakeling 2023-12-29 15:18:46
30
		Title string
594ec41 Jakob Wakeling 2023-12-29 15:18:46
31
		Jobs  []row
594ec41 Jakob Wakeling 2023-12-29 15:18:46
32
	}{Title: "Admin - Cron"}
594ec41 Jakob Wakeling 2023-12-29 15:18:46
33
594ec41 Jakob Wakeling 2023-12-29 15:18:46
34
	for _, job := range goit.Cron.Jobs() {
594ec41 Jakob Wakeling 2023-12-29 15:18:46
35
		repo := &goit.Repo{}
594ec41 Jakob Wakeling 2023-12-29 15:18:46
36
594ec41 Jakob Wakeling 2023-12-29 15:18:46
37
		if job.Rid != -1 {
594ec41 Jakob Wakeling 2023-12-29 15:18:46
38
			if r, err := goit.GetRepo(job.Rid); err != nil {
594ec41 Jakob Wakeling 2023-12-29 15:18:46
39
				log.Println("[/admin/cron]", err.Error())
594ec41 Jakob Wakeling 2023-12-29 15:18:46
40
			} else if r != nil {
594ec41 Jakob Wakeling 2023-12-29 15:18:46
41
				repo = r
594ec41 Jakob Wakeling 2023-12-29 15:18:46
42
			}
594ec41 Jakob Wakeling 2023-12-29 15:18:46
43
		}
594ec41 Jakob Wakeling 2023-12-29 15:18:46
44
594ec41 Jakob Wakeling 2023-12-29 15:18:46
45
		data.Jobs = append(data.Jobs, row{
594ec41 Jakob Wakeling 2023-12-29 15:18:46
46
			Id:       fmt.Sprint(job.Id),
594ec41 Jakob Wakeling 2023-12-29 15:18:46
47
			Repo:     repo.Name,
594ec41 Jakob Wakeling 2023-12-29 15:18:46
48
			Schedule: job.Schedule.String(),
594ec41 Jakob Wakeling 2023-12-29 15:18:46
49
			Next:     job.Next.String(),
594ec41 Jakob Wakeling 2023-12-29 15:18:46
50
			Last:     util.If(job.Last == time.Time{}, "never", job.Last.String()),
594ec41 Jakob Wakeling 2023-12-29 15:18:46
51
		})
594ec41 Jakob Wakeling 2023-12-29 15:18:46
52
	}
594ec41 Jakob Wakeling 2023-12-29 15:18:46
53
594ec41 Jakob Wakeling 2023-12-29 15:18:46
54
	if err := goit.Tmpl.ExecuteTemplate(w, "admin/cron", data); err != nil {
594ec41 Jakob Wakeling 2023-12-29 15:18:46
55
		log.Println("[/admin/cron]", err.Error())
594ec41 Jakob Wakeling 2023-12-29 15:18:46
56
	}
594ec41 Jakob Wakeling 2023-12-29 15:18:46
57
}
58