@@ -5,58 +5,65 @@ import (
5
5
"net/http"
6
6
"strings"
7
7
8
- "github.com/spaceuptech/space-cloud/modules/static "
8
+ "github.com/spaceuptech/space-cloud/utils/projects "
9
9
)
10
10
11
11
// HandleStaticRequest creates a static request endpoint
12
- func HandleStaticRequest (static * static. Module ) http.HandlerFunc {
12
+ func HandleStaticRequest (p * projects. Projects ) http.HandlerFunc {
13
13
return func (w http.ResponseWriter , r * http.Request ) {
14
14
url := r .URL .Path
15
15
host := strings .Split (r .Host , ":" )[0 ]
16
16
17
- route , ok := static .SelectRoute (host , url )
18
- if ! ok {
19
- http .Error (w , "Path not found" , http .StatusNotFound )
20
- return
21
- }
22
-
23
- path := strings .TrimPrefix (url , route .URLPrefix )
24
- if ! strings .HasPrefix (path , "/" ) {
25
- path = "/" + path
26
- }
27
- path = route .Path + path
28
-
29
- // Its a proxy request
30
- if route .Proxy != "" {
31
- addr := route .Proxy + path
32
- req , err := http .NewRequest (r .Method , addr , r .Body )
33
- if err != nil {
34
- http .Error (w , err .Error (), http .StatusNotFound )
35
- return
17
+ completed := p .Iter (func (project string , state * projects.ProjectState ) bool {
18
+ static := state .Static
19
+ route , ok := static .SelectRoute (host , url )
20
+ if ! ok {
21
+ return true
36
22
}
37
23
38
- // Set the http headers
39
- req .Header = make (http.Header )
40
- if contentType , p := r .Header ["Content-Type" ]; p {
41
- req .Header ["Content-Type" ] = contentType
24
+ path := strings .TrimPrefix (url , route .URLPrefix )
25
+ if ! strings .HasPrefix (path , "/" ) {
26
+ path = "/" + path
42
27
}
28
+ path = route .Path + path
29
+
30
+ // Its a proxy request
31
+ if route .Proxy != "" {
32
+ addr := route .Proxy + path
33
+ req , err := http .NewRequest (r .Method , addr , r .Body )
34
+ if err != nil {
35
+ http .Error (w , err .Error (), http .StatusNotFound )
36
+ return false
37
+ }
43
38
44
- // Make the http client request
45
- res , err := http .DefaultClient .Do (req )
46
- if err != nil {
47
- http .Error (w , err .Error (), http .StatusNotFound )
48
- return
39
+ // Set the http headers
40
+ req .Header = make (http.Header )
41
+ if contentType , p := r .Header ["Content-Type" ]; p {
42
+ req .Header ["Content-Type" ] = contentType
43
+ }
44
+
45
+ // Make the http client request
46
+ res , err := http .DefaultClient .Do (req )
47
+ if err != nil {
48
+ http .Error (w , err .Error (), http .StatusNotFound )
49
+ return false
50
+ }
51
+ defer res .Body .Close ()
52
+
53
+ reader := bufio .NewReader (res .Body )
54
+
55
+ w .Header ().Set ("Content-Type" , res .Header .Get ("Content-Type" ))
56
+ w .WriteHeader (res .StatusCode )
57
+ reader .WriteTo (w )
58
+ return false
49
59
}
50
- defer res .Body .Close ()
51
60
52
- reader := bufio .NewReader (res .Body )
61
+ http .ServeFile (w , r , path )
62
+ return false
63
+ })
53
64
54
- w .Header ().Set ("Content-Type" , res .Header .Get ("Content-Type" ))
55
- w .WriteHeader (res .StatusCode )
56
- reader .WriteTo (w )
57
- return
65
+ if ! completed {
66
+ http .Error (w , "Path not found" , http .StatusNotFound )
58
67
}
59
-
60
- http .ServeFile (w , r , path )
61
68
}
62
69
}
0 commit comments