Skip to content

Commit ff77fee

Browse files
committed
move back crons and private/public fields to the app config
1 parent 2739b19 commit ff77fee

File tree

7 files changed

+89
-78
lines changed

7 files changed

+89
-78
lines changed

app/app.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ var (
2020
)
2121

2222
type AppConfig struct {
23-
Entrypoint string `json:"entrypoint,omitempty"`
24-
Root string `json:"root,omitempty"`
23+
Entrypoint string `json:"entrypoint,omitempty"`
24+
Root string `json:"root,omitempty"`
25+
Crons []CronJob `json:"cron"`
26+
Private bool `json:"private"`
27+
PrivateRoutes []string `json:"privateRoutes"`
28+
PublicRoutes []string `json:"publicRoutes"`
2529
}
2630

2731
type CronJob struct {
28-
Schedule string `json:"schedule"`
29-
Args []string `json:"args"`
32+
Description string `json:"description"`
33+
Schedule string `json:"schedule"`
34+
Args []string `json:"args"`
3035
}
3136

3237
type App struct {

cmd/crons.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ func NewCmdCrons() *cobra.Command {
4747
Short: "List cron jobs",
4848
RunE: func(cmd *cobra.Command, args []string) error {
4949
var crons []CronItem
50-
for _, appname := range k.MapKeys("apps") {
50+
apps, err := app.ListApps(k.String("dir"))
51+
if err != nil {
52+
return fmt.Errorf("failed to list apps: %w", err)
53+
}
54+
55+
for _, appname := range apps {
5156
if len(args) > 0 && appname != args[0] {
5257
continue
5358
} else if len(args) == 0 && !flags.all {
@@ -69,11 +74,16 @@ func NewCmdCrons() *cobra.Command {
6974
}
7075
}
7176

72-
for _, job := range k.Slices(fmt.Sprintf("apps.%s.crons", appname)) {
77+
a, err := app.LoadApp(appname, k.String("dir"), k.String("domain"), k.Bool(fmt.Sprintf("apps.%s.admin", appname)))
78+
if err != nil {
79+
return fmt.Errorf("failed to load app %s: %w", appname, err)
80+
}
81+
82+
for _, job := range a.Config.Crons {
7383
crons = append(crons, CronItem{
7484
App: appname,
75-
Args: job.Strings("args"),
76-
Schedule: job.String("schedule"),
85+
Args: job.Args,
86+
Schedule: job.Schedule,
7787
})
7888
}
7989
}

cmd/up.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,21 @@ func (me *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
694694
return
695695
}
696696

697+
wk, err := me.GetWorker(appname, k.String("dir"), k.String("domain"))
698+
if err != nil {
699+
if errors.Is(err, app.ErrAppNotFound) {
700+
w.WriteHeader(http.StatusNotFound)
701+
w.Write([]byte(fmt.Sprintf("No app found for host %s", r.Host)))
702+
return
703+
}
704+
705+
w.WriteHeader(http.StatusInternalServerError)
706+
fmt.Fprintf(w, "failed to get worker: %v", err)
707+
return
708+
}
709+
697710
claims, err := me.extractClaims(r)
698-
if err != nil && isRoutePrivate(appname, r.URL.Path) {
711+
if err != nil && isRoutePrivate(wk.App, r.URL.Path) {
699712
if me.oidcIssuerUrl == nil {
700713
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
701714
return
@@ -775,7 +788,7 @@ func (me *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
775788
}
776789
}
777790

778-
if isRoutePrivate(appname, r.URL.Path) && !isAuthorized(appname, claims.Email, claims.Group) {
791+
if isRoutePrivate(wk.App, r.URL.Path) && !isAuthorized(appname, claims.Email, claims.Group) {
779792
if claims.Email == "" {
780793
http.Redirect(w, r, fmt.Sprintf("https://%s/_smallweb/signin", r.Host), http.StatusTemporaryRedirect)
781794
return
@@ -790,32 +803,19 @@ func (me *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
790803
r.Header.Set("Remote-Group", claims.Group)
791804
r.Header.Set("Remote-Name", claims.Name)
792805

793-
wk, err := me.GetWorker(appname, k.String("dir"), k.String("domain"))
794-
if err != nil {
795-
if errors.Is(err, app.ErrAppNotFound) {
796-
w.WriteHeader(http.StatusNotFound)
797-
w.Write([]byte(fmt.Sprintf("No app found for host %s", r.Host)))
798-
return
799-
}
800-
801-
w.WriteHeader(http.StatusInternalServerError)
802-
fmt.Fprintf(w, "failed to get worker: %v", err)
803-
return
804-
}
805-
806806
wk.ServeHTTP(w, r)
807807
}
808808

809-
func isRoutePrivate(appname string, route string) bool {
810-
isPrivate := k.Bool(fmt.Sprintf("apps.%s.private", appname))
809+
func isRoutePrivate(a app.App, route string) bool {
810+
isPrivate := a.Config.Private
811811

812-
for _, publicRoute := range k.Strings(fmt.Sprintf("apps.%s.publicRoutes", appname)) {
812+
for _, publicRoute := range a.Config.PublicRoutes {
813813
if ok, _ := doublestar.Match(publicRoute, route); ok {
814814
isPrivate = false
815815
}
816816
}
817817

818-
for _, privateRoute := range k.Strings(fmt.Sprintf("apps.%s.privateRoutes", appname)) {
818+
for _, privateRoute := range a.Config.PrivateRoutes {
819819
if ok, _ := doublestar.Match(privateRoute, route); ok {
820820
isPrivate = true
821821
}

example/.smallweb/config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"apps": {
99
"ls": {
1010
"admin": true,
11-
"private": true,
1211
"additionalDomains": [
1312
"custom-domain.localhost"
1413
]

example/ls/smallweb.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"private": true,
3-
"publicRoutes": [
4-
"/public/*"
5-
]
2+
"private": true
63
}

schemas/config.schema.json

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -65,52 +65,6 @@
6565
"description": "Give the app admin privileges",
6666
"type": "boolean"
6767
},
68-
"private": {
69-
"description": "Protect all routes behind authentication",
70-
"type": "boolean"
71-
},
72-
"privateRoutes": {
73-
"description": "Make specific routes private",
74-
"type": "array",
75-
"items": {
76-
"type": "string"
77-
}
78-
},
79-
"publicRoutes": {
80-
"description": "Make specific routes public",
81-
"type": "array",
82-
"items": {
83-
"type": "string"
84-
}
85-
},
86-
"crons": {
87-
"description": "Cron jobs",
88-
"type": "array",
89-
"items": {
90-
"type": "object",
91-
"required": [
92-
"schedule",
93-
"args"
94-
],
95-
"properties": {
96-
"schedule": {
97-
"description": "Cron schedule",
98-
"type": "string"
99-
},
100-
"description": {
101-
"type": "string",
102-
"description": "An optional description for the task"
103-
},
104-
"args": {
105-
"description": "Cron arguments",
106-
"type": "array",
107-
"items": {
108-
"type": "string"
109-
}
110-
}
111-
}
112-
}
113-
},
11468
"additionalDomains": {
11569
"description": "Additional app domains",
11670
"type": "array",

schemas/manifest.schema.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,52 @@
1010
"description": "The root directory of the project",
1111
"type": "string"
1212
},
13+
"private": {
14+
"description": "Protect all routes behind authentication",
15+
"type": "boolean"
16+
},
17+
"privateRoutes": {
18+
"description": "Make specific routes private",
19+
"type": "array",
20+
"items": {
21+
"type": "string"
22+
}
23+
},
24+
"publicRoutes": {
25+
"description": "Make specific routes public",
26+
"type": "array",
27+
"items": {
28+
"type": "string"
29+
}
30+
},
31+
"crons": {
32+
"description": "Cron jobs",
33+
"type": "array",
34+
"items": {
35+
"type": "object",
36+
"required": [
37+
"schedule",
38+
"args"
39+
],
40+
"properties": {
41+
"schedule": {
42+
"description": "Cron schedule",
43+
"type": "string"
44+
},
45+
"description": {
46+
"type": "string",
47+
"description": "An optional description for the task"
48+
},
49+
"args": {
50+
"description": "Cron arguments",
51+
"type": "array",
52+
"items": {
53+
"type": "string"
54+
}
55+
}
56+
}
57+
}
58+
},
1359
"labels": {
1460
"description": "Labels for the project",
1561
"type": "object",

0 commit comments

Comments
 (0)