-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
197 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"k8s.io/client-go/dynamic" | ||
"github.com/gimlet-io/capacitor/pkg/flux" | ||
) | ||
|
||
func fluxStateHandler(w http.ResponseWriter, r *http.Request) { | ||
dynamicClient, _ := r.Context().Value("dynamicClient").(*dynamic.DynamicClient) | ||
|
||
fluxState, err := flux.GetFluxState(dynamicClient) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
fluxStateBytes, err := json.Marshal(fluxState) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write(fluxStateBytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gimlet-io/capacitor/pkg/streaming" | ||
"github.com/go-chi/chi" | ||
"github.com/go-chi/chi/middleware" | ||
"k8s.io/client-go/dynamic" | ||
) | ||
|
||
func SetupRouter( | ||
dynamicClient *dynamic.DynamicClient, | ||
clientHub *streaming.ClientHub, | ||
) *chi.Mux { | ||
r := chi.NewRouter() | ||
r.Use(middleware.WithValue("dynamicClient", dynamicClient)) | ||
|
||
r.Get("/health", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
r.Get("/api/fluxState", fluxStateHandler) | ||
r.Get("/ws/", func(w http.ResponseWriter, r *http.Request) { | ||
streaming.ServeWs(clientHub, w, r) | ||
}) | ||
|
||
filesDir := http.Dir("./web/build") | ||
fileServer(r, "/", filesDir) | ||
|
||
return r | ||
} | ||
|
||
// static files from a http.FileSystem | ||
func fileServer(r chi.Router, path string, root http.FileSystem) { | ||
if strings.ContainsAny(path, "{}*") { | ||
//TODO: serve all React routes https://github.com/go-chi/chi/issues/403 | ||
panic("FileServer does not permit any URL parameters.") | ||
} | ||
|
||
if path != "/" && path[len(path)-1] != '/' { | ||
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP) | ||
path += "/" | ||
} | ||
path += "*" | ||
|
||
r.Get(path, func(w http.ResponseWriter, r *http.Request) { | ||
ctx := chi.RouteContext(r.Context()) | ||
pathPrefix := strings.TrimSuffix(ctx.RoutePattern(), "/*") | ||
fs := http.StripPrefix(pathPrefix, http.FileServer(root)) | ||
fs.ServeHTTP(w, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package controllers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/gimlet-io/capacitor/pkg/flux" | ||
"github.com/gimlet-io/capacitor/pkg/streaming" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
) | ||
|
||
var gitRepositoryResource = schema.GroupVersionResource{ | ||
Group: "source.toolkit.fluxcd.io", | ||
Version: "v1", | ||
Resource: "gitrepositories", | ||
} | ||
|
||
func GitRepositoryController( | ||
dynamicClient *dynamic.DynamicClient, | ||
clientHub *streaming.ClientHub, | ||
) *Controller { | ||
return NewDynamicController( | ||
"gitrepositories.source.toolkit.fluxcd.io", | ||
dynamicClient, | ||
gitRepositoryResource, | ||
func(informerEvent Event, objectMeta metav1.ObjectMeta, obj interface{}) error { | ||
switch informerEvent.EventType { | ||
case "create": | ||
fallthrough | ||
case "update": | ||
fallthrough | ||
case "delete": | ||
fmt.Printf("Changes in %s\n", objectMeta.Name) | ||
fluxState, err := flux.GetFluxState(dynamicClient) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
fluxStateBytes, err := json.Marshal(fluxState) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
clientHub.Broadcast <- fluxStateBytes | ||
} | ||
return nil | ||
}) | ||
} |
Oops, something went wrong.