|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "crypto/subtle" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "os/signal" |
| 12 | + "strings" |
| 13 | + "syscall" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/go-co-op/gocron-ui/server" |
| 17 | + "github.com/go-co-op/gocron/v2" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + usernameEnv = "GOCRON_UI_USERNAME" |
| 22 | + passwordEnv = "GOCRON_UI_PASSWORD" |
| 23 | +) |
| 24 | + |
| 25 | +var jobs = []struct { |
| 26 | + name string |
| 27 | + definition gocron.JobDefinition |
| 28 | + task gocron.Task |
| 29 | + options []gocron.JobOption |
| 30 | +}{ |
| 31 | + { |
| 32 | + "simple-10s-interval", gocron.DurationJob(10 * time.Second), gocron.NewTask(func() { log.Println("Running 10-second interval job") }), []gocron.JobOption{gocron.WithName("simple-10s-interval"), gocron.WithTags("interval", "simple")}, |
| 33 | + }, |
| 34 | + { |
| 35 | + "simple-5s-interval", gocron.DurationJob(5 * time.Second), gocron.NewTask(func() { log.Println("Running 5-second interval job") }), []gocron.JobOption{gocron.WithName("simple-5s-interval"), gocron.WithTags("interval", "simple")}, |
| 36 | + }, |
| 37 | + { |
| 38 | + "simple-20s-interval", gocron.DurationJob(20 * time.Second), gocron.NewTask(func() { log.Println("Running 20-second interval job") }), []gocron.JobOption{gocron.WithName("simple-20s-interval"), gocron.WithTags("interval", "simple")}, |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +func main() { |
| 43 | + username, usernameOK := os.LookupEnv(usernameEnv) |
| 44 | + password, passwordOK := os.LookupEnv(passwordEnv) |
| 45 | + |
| 46 | + if (!usernameOK || !passwordOK) || username == "" || password == "" { |
| 47 | + log.Fatalf("Environment variables %s and %s must be set for basic authentication", usernameEnv, passwordEnv) |
| 48 | + } |
| 49 | + |
| 50 | + port := flag.Int("port", 8080, "Port to run the server on") |
| 51 | + title := flag.String("title", "GoCron Scheduler", "Custom title for the UI") |
| 52 | + flag.Parse() |
| 53 | + |
| 54 | + // create the gocron scheduler |
| 55 | + scheduler, err := gocron.NewScheduler() |
| 56 | + if err != nil { |
| 57 | + log.Fatalf("Failed to create scheduler: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + // add jobs to the scheduler |
| 61 | + for _, job := range jobs { |
| 62 | + if _, err := scheduler.NewJob(job.definition, job.task, job.options...); err != nil { |
| 63 | + log.Printf("Error creating job: %v", err) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // start the scheduler |
| 68 | + scheduler.Start() |
| 69 | + log.Println("Scheduler started with", len(scheduler.Jobs()), "jobs") |
| 70 | + |
| 71 | + // create and start the API server with custom title |
| 72 | + srv := server.NewServer(scheduler, *port, server.WithTitle(*title)) |
| 73 | + |
| 74 | + // start server in a goroutine |
| 75 | + go func() { |
| 76 | + addr := fmt.Sprintf(":%d", *port) |
| 77 | + log.Println("\n" + strings.Repeat("=", 70)) |
| 78 | + log.Printf("GoCron UI Server Started with Basic Authentication") |
| 79 | + log.Println(strings.Repeat("=", 70)) |
| 80 | + log.Printf("Web UI: http://localhost%s", addr) |
| 81 | + log.Printf("API: http://localhost%s/api", addr) |
| 82 | + log.Printf("WebSocket: ws://localhost%s/ws", addr) |
| 83 | + log.Printf("Total Jobs: %d", len(scheduler.Jobs())) |
| 84 | + log.Println(strings.Repeat("=", 70) + "\n") |
| 85 | + |
| 86 | + if err := http.ListenAndServe(addr, basicAuthMiddleware(srv.Router, username, password)); err != nil { |
| 87 | + log.Fatalf("Server failed to start: %v", err) |
| 88 | + } |
| 89 | + }() |
| 90 | + |
| 91 | + // wait for interrupt signal to gracefully shutdown |
| 92 | + quit := make(chan os.Signal, 1) |
| 93 | + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) |
| 94 | + <-quit |
| 95 | + |
| 96 | + log.Println("\nShutting down server...") |
| 97 | + |
| 98 | + // shutdown scheduler |
| 99 | + if err := scheduler.Shutdown(); err != nil { |
| 100 | + log.Printf("Error shutting down scheduler: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + log.Println("Server stopped gracefully") |
| 104 | +} |
| 105 | + |
| 106 | +// https://www.alexedwards.net/blog/basic-authentication-in-go |
| 107 | +func basicAuthMiddleware(next http.Handler, expectedUsername, expectedPassword string) http.HandlerFunc { |
| 108 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 109 | + username, password, ok := r.BasicAuth() |
| 110 | + if ok { |
| 111 | + usernameHash := sha256.Sum256([]byte(username)) |
| 112 | + passwordHash := sha256.Sum256([]byte(password)) |
| 113 | + |
| 114 | + expectedUsernameHash := sha256.Sum256([]byte(expectedUsername)) |
| 115 | + expectedPasswordHash := sha256.Sum256([]byte(expectedPassword)) |
| 116 | + |
| 117 | + usernameMatch := (subtle.ConstantTimeCompare(usernameHash[:], expectedUsernameHash[:]) == 1) |
| 118 | + passwordMatch := (subtle.ConstantTimeCompare(passwordHash[:], expectedPasswordHash[:]) == 1) |
| 119 | + |
| 120 | + if usernameMatch && passwordMatch { |
| 121 | + next.ServeHTTP(w, r) |
| 122 | + return |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`) |
| 127 | + http.Error(w, "Unauthorized", http.StatusUnauthorized) |
| 128 | + }) |
| 129 | +} |
0 commit comments