Skip to content

Commit 0ebab47

Browse files
author
Tom Morelly
committed
feat(opts): add WithAPIDisabled and WithWebSocketDisabled Options
1 parent 17d296f commit 0ebab47

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ server.NewServer(scheduler gocron.Scheduler, port int, opts ...ServerOption) *Se
174174
**Parameters:**
175175
- `scheduler` - Your configured gocron scheduler instance
176176
- `port` - HTTP port to listen on
177-
- `opts` - Optional configuration settings
178-
179-
### Configuration Options
177+
- `opts` - Optional configuration settings (available configuration Options are listed [here](https://pkg.go.dev/github.com/go-co-op/[email protected]/server#Option))
180178

181179
#### Custom Title
182180

server/server.go

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ type Server struct {
3333

3434
// Config is the server configuration in which user can set the title of the UI
3535
type Config struct {
36-
Title string `json:"title"`
36+
Title string `json:"title"`
37+
APIEnabled bool `json:"api_enabled"`
38+
WebSocketEnabled bool `json:"websocket_enabled"`
39+
}
40+
41+
func defaultConfig() Config {
42+
return Config{
43+
Title: "GoCron UI",
44+
APIEnabled: true,
45+
WebSocketEnabled: true,
46+
}
3747
}
3848

3949
// NewServer creates a new server instance
@@ -46,9 +56,7 @@ func NewServer(scheduler gocron.Scheduler, _ int, opts ...Option) *Server {
4656
return true // allow all origins for development
4757
},
4858
},
49-
config: Config{
50-
Title: "GoCron UI", // default title
51-
},
59+
config: defaultConfig(),
5260
}
5361

5462
// apply options
@@ -58,19 +66,23 @@ func NewServer(scheduler gocron.Scheduler, _ int, opts ...Option) *Server {
5866

5967
router := mux.NewRouter()
6068

61-
// api routes
62-
api := router.PathPrefix("/api").Subrouter()
63-
api.HandleFunc("/config", s.GetConfig).Methods("GET")
64-
api.HandleFunc("/jobs", s.GetJobs).Methods("GET")
65-
api.HandleFunc("/jobs", s.CreateJob).Methods("POST")
66-
api.HandleFunc("/jobs/{id}", s.GetJob).Methods("GET")
67-
api.HandleFunc("/jobs/{id}", s.DeleteJob).Methods("DELETE")
68-
api.HandleFunc("/jobs/{id}/run", s.RunJob).Methods("POST")
69-
api.HandleFunc("/scheduler/stop", s.StopScheduler).Methods("POST")
70-
api.HandleFunc("/scheduler/start", s.StartScheduler).Methods("POST")
69+
if s.config.APIEnabled {
70+
// api routes
71+
api := router.PathPrefix("/api").Subrouter()
72+
api.HandleFunc("/config", s.GetConfig).Methods("GET")
73+
api.HandleFunc("/jobs", s.GetJobs).Methods("GET")
74+
api.HandleFunc("/jobs", s.CreateJob).Methods("POST")
75+
api.HandleFunc("/jobs/{id}", s.GetJob).Methods("GET")
76+
api.HandleFunc("/jobs/{id}", s.DeleteJob).Methods("DELETE")
77+
api.HandleFunc("/jobs/{id}/run", s.RunJob).Methods("POST")
78+
api.HandleFunc("/scheduler/stop", s.StopScheduler).Methods("POST")
79+
api.HandleFunc("/scheduler/start", s.StartScheduler).Methods("POST")
80+
}
7181

72-
// webSocket route
73-
router.HandleFunc("/ws", s.HandleWebSocket)
82+
if s.config.WebSocketEnabled {
83+
// webSocket route
84+
router.HandleFunc("/ws", s.HandleWebSocket)
85+
}
7486

7587
// serve embedded static files (frontend)
7688
staticFS, err := fs.Sub(staticFiles, "static")
@@ -105,6 +117,20 @@ func WithTitle(title string) Option {
105117
}
106118
}
107119

120+
// WithAPIDisabled disables API
121+
func WithAPIDisabled() Option {
122+
return func(s *Server) {
123+
s.config.APIEnabled = false
124+
}
125+
}
126+
127+
// WithWebSocketDisabled disables webSocket
128+
func WithWebSocketDisabled() Option {
129+
return func(s *Server) {
130+
s.config.WebSocketEnabled = false
131+
}
132+
}
133+
108134
// GetConfig gets server configuration
109135
func (s *Server) GetConfig(w http.ResponseWriter, _ *http.Request) {
110136
respondJSON(w, http.StatusOK, s.config)

0 commit comments

Comments
 (0)