@@ -21,29 +21,32 @@ import (
2121//go:embed static/*
2222var staticFiles embed.FS
2323
24+ // Server is the main server struct which contains the scheduler, router, webSocket clients, webSocket mutex, upgrader, and config
2425type Server struct {
2526 Scheduler gocron.Scheduler
2627 Router http.Handler
2728 wsClients map [* websocket.Conn ]bool
2829 wsMutex sync.RWMutex
2930 upgrader websocket.Upgrader
30- config ServerConfig
31+ config Config
3132}
3233
33- type ServerConfig struct {
34+ // Config is the server configuration in which user can set the title of the UI
35+ type Config struct {
3436 Title string `json:"title"`
3537}
3638
37- func NewServer (scheduler gocron.Scheduler , port int , opts ... ServerOption ) * Server {
39+ // NewServer creates a new server instance
40+ func NewServer (scheduler gocron.Scheduler , _ int , opts ... Option ) * Server {
3841 s := & Server {
3942 Scheduler : scheduler ,
4043 wsClients : make (map [* websocket.Conn ]bool ),
4144 upgrader : websocket.Upgrader {
42- CheckOrigin : func (r * http.Request ) bool {
45+ CheckOrigin : func (_ * http.Request ) bool {
4346 return true // allow all origins for development
4447 },
4548 },
46- config : ServerConfig {
49+ config : Config {
4750 Title : "GoCron UI" , // default title
4851 },
4952 }
@@ -92,22 +95,22 @@ func NewServer(scheduler gocron.Scheduler, port int, opts ...ServerOption) *Serv
9295 return s
9396}
9497
95- // serverOption is a functional option for configuring the server
96- type ServerOption func (* Server )
98+ // Option is a functional option for configuring the server
99+ type Option func (* Server )
97100
98101// WithTitle sets a custom title for the UI
99- func WithTitle (title string ) ServerOption {
102+ func WithTitle (title string ) Option {
100103 return func (s * Server ) {
101104 s .config .Title = title
102105 }
103106}
104107
105- // get server configuration
106- func (s * Server ) GetConfig (w http.ResponseWriter , r * http.Request ) {
108+ // GetConfig gets server configuration
109+ func (s * Server ) GetConfig (w http.ResponseWriter , _ * http.Request ) {
107110 respondJSON (w , http .StatusOK , s .config )
108111}
109112
110- // webSocket handler
113+ // HandleWebSocket is a webSocket handler
111114func (s * Server ) HandleWebSocket (w http.ResponseWriter , r * http.Request ) {
112115 conn , err := s .upgrader .Upgrade (w , r , nil )
113116 if err != nil {
@@ -144,7 +147,7 @@ func (s *Server) HandleWebSocket(w http.ResponseWriter, r *http.Request) {
144147 }
145148}
146149
147- // broadcast job updates to all connected webSocket clients
150+ // broadcastJobUpdates broadcasts job updates to all connected webSocket clients
148151func (s * Server ) broadcastJobUpdates () {
149152 ticker := time .NewTicker (1 * time .Second )
150153 defer ticker .Stop ()
@@ -180,13 +183,13 @@ func (s *Server) broadcastJobUpdates() {
180183 }
181184}
182185
183- // get all jobs
184- func (s * Server ) GetJobs (w http.ResponseWriter , r * http.Request ) {
186+ // GetJobs gets all jobs
187+ func (s * Server ) GetJobs (w http.ResponseWriter , _ * http.Request ) {
185188 jobs := s .getJobsData ()
186189 respondJSON (w , http .StatusOK , jobs )
187190}
188191
189- // get single job
192+ // GetJob gets a single job
190193func (s * Server ) GetJob (w http.ResponseWriter , r * http.Request ) {
191194 vars := mux .Vars (r )
192195 idStr := vars ["id" ]
@@ -209,7 +212,7 @@ func (s *Server) GetJob(w http.ResponseWriter, r *http.Request) {
209212 respondError (w , http .StatusNotFound , "Job not found" )
210213}
211214
212- // create a new job
215+ // CreateJob creates a new job
213216func (s * Server ) CreateJob (w http.ResponseWriter , r * http.Request ) {
214217 var req CreateJobRequest
215218 if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
@@ -286,7 +289,7 @@ func (s *Server) CreateJob(w http.ResponseWriter, r *http.Request) {
286289 respondJSON (w , http .StatusCreated , jobData )
287290}
288291
289- // delete a job
292+ // DeleteJob deletes a job
290293func (s * Server ) DeleteJob (w http.ResponseWriter , r * http.Request ) {
291294 vars := mux .Vars (r )
292295 idStr := vars ["id" ]
@@ -305,7 +308,7 @@ func (s *Server) DeleteJob(w http.ResponseWriter, r *http.Request) {
305308 respondJSON (w , http .StatusOK , map [string ]string {"message" : "Job deleted successfully" })
306309}
307310
308- // run a job immediately
311+ // RunJob runs a job immediately
309312func (s * Server ) RunJob (w http.ResponseWriter , r * http.Request ) {
310313 vars := mux .Vars (r )
311314 idStr := vars ["id" ]
@@ -331,17 +334,17 @@ func (s *Server) RunJob(w http.ResponseWriter, r *http.Request) {
331334 respondError (w , http .StatusNotFound , "Job not found" )
332335}
333336
334- // stop the scheduler
335- func (s * Server ) StopScheduler (w http.ResponseWriter , r * http.Request ) {
337+ // StopScheduler stops the scheduler
338+ func (s * Server ) StopScheduler (w http.ResponseWriter , _ * http.Request ) {
336339 if err := s .Scheduler .StopJobs (); err != nil {
337340 respondError (w , http .StatusInternalServerError , err .Error ())
338341 return
339342 }
340343 respondJSON (w , http .StatusOK , map [string ]string {"message" : "Scheduler stopped" })
341344}
342345
343- // start the scheduler
344- func (s * Server ) StartScheduler (w http.ResponseWriter , r * http.Request ) {
346+ // StartScheduler starts the scheduler
347+ func (s * Server ) StartScheduler (w http.ResponseWriter , _ * http.Request ) {
345348 s .Scheduler .Start ()
346349 respondJSON (w , http .StatusOK , map [string ]string {"message" : "Scheduler started" })
347350}
@@ -440,16 +443,17 @@ func (s *Server) inferSchedule(job gocron.Job, nextRuns []time.Time) (string, st
440443 if interval < time .Minute {
441444 seconds := int (interval .Seconds ())
442445 return fmt .Sprintf ("Every %d seconds" , seconds ), fmt .Sprintf ("Duration: %ds" , seconds )
443- } else if interval < time .Hour {
446+ }
447+ if interval < time .Hour {
444448 minutes := int (interval .Minutes ())
445449 return fmt .Sprintf ("Every %d minutes" , minutes ), fmt .Sprintf ("Duration: %dm" , minutes )
446- } else if interval < 24 * time .Hour {
450+ }
451+ if interval < 24 * time .Hour {
447452 hours := int (interval .Hours ())
448453 return fmt .Sprintf ("Every %d hours" , hours ), fmt .Sprintf ("Duration: %dh" , hours )
449- } else {
450- days := int (interval .Hours () / 24 )
451- return fmt .Sprintf ("Every %d days" , days ), fmt .Sprintf ("Duration: %dd" , days )
452454 }
455+ days := int (interval .Hours () / 24 )
456+ return fmt .Sprintf ("Every %d days" , days ), fmt .Sprintf ("Duration: %dd" , days )
453457 }
454458
455459 return "Scheduled" , "Custom schedule"
@@ -482,11 +486,13 @@ func parseTime(timeStr string) (gocron.AtTime, error) {
482486 return gocron .NewAtTime (uint (t .Hour ()), uint (t .Minute ()), uint (t .Second ())), nil
483487}
484488
485- // respond with JSON
489+ // respondJSON responds with JSON
486490func respondJSON (w http.ResponseWriter , status int , data interface {}) {
487491 w .Header ().Set ("Content-Type" , "application/json" )
488492 w .WriteHeader (status )
489- json .NewEncoder (w ).Encode (data )
493+ if err := json .NewEncoder (w ).Encode (data ); err != nil {
494+ log .Printf ("Error encoding JSON response: %v" , err )
495+ }
490496}
491497
492498func respondError (w http.ResponseWriter , status int , message string ) {
0 commit comments