auth+cli
This commit is contained in:
parent
314cbd37c7
commit
07522043b4
16 changed files with 870 additions and 38 deletions
|
|
@ -12,11 +12,19 @@ import (
|
|||
func (a *App) routes() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/", http.FileServer(http.Dir("./web")))
|
||||
mux.HandleFunc("/api/me", a.withAuth(a.handleMe))
|
||||
mux.HandleFunc("/api/workspaces", a.withAuth(a.handleWorkspaces))
|
||||
mux.HandleFunc("/api/projects", a.withAuth(a.handleProjects))
|
||||
mux.HandleFunc("/api/projects/", a.withAuth(a.handleProjectSubroutes))
|
||||
mux.HandleFunc("/api/caddy/config", a.withAuth(a.handleCaddyConfig))
|
||||
mux.HandleFunc("/web-api/me", a.withAuth(a.handleMe))
|
||||
mux.HandleFunc("/web-api/workspaces", a.withAuth(a.handleWorkspaces))
|
||||
mux.HandleFunc("/web-api/projects", a.withAuth(a.handleProjects))
|
||||
mux.HandleFunc("/web-api/projects/", a.withAuth(a.handleProjectSubroutes))
|
||||
mux.HandleFunc("/web-api/caddy/config", a.withAuth(a.handleCaddyConfig))
|
||||
mux.HandleFunc("/web-api/tokens/webhook", a.withAuth(a.handleWebhookToken))
|
||||
|
||||
mux.HandleFunc("/api/me", a.withAPIUser(a.handleMe))
|
||||
mux.HandleFunc("/api/workspaces", a.withAPIUser(a.handleWorkspaces))
|
||||
mux.HandleFunc("/api/projects", a.withAPIUser(a.handleProjects))
|
||||
mux.HandleFunc("/api/projects/", a.withAPIUser(a.handleProjectSubroutes))
|
||||
mux.HandleFunc("/api/caddy/config", a.withAPIUser(a.handleCaddyConfig))
|
||||
mux.HandleFunc("/api/tokens/webhook", a.withAPIUser(a.handleWebhookToken))
|
||||
mux.HandleFunc("/api/webhooks/deploy/", a.handleDeployWebhook)
|
||||
return loggingMiddleware(mux)
|
||||
}
|
||||
|
|
@ -111,6 +119,9 @@ func (a *App) handleProjects(w http.ResponseWriter, r *http.Request, user User)
|
|||
|
||||
func (a *App) handleProjectSubroutes(w http.ResponseWriter, r *http.Request, user User) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/projects/")
|
||||
if path == r.URL.Path {
|
||||
path = strings.TrimPrefix(r.URL.Path, "/web-api/projects/")
|
||||
}
|
||||
parts := strings.Split(path, "/")
|
||||
if len(parts) < 1 || parts[0] == "" {
|
||||
http.Error(w, "bad path", http.StatusBadRequest)
|
||||
|
|
@ -297,3 +308,24 @@ func (a *App) handleCaddyConfig(w http.ResponseWriter, r *http.Request, _ User)
|
|||
"content": mustJSONPretty(cfg),
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) handleWebhookToken(w http.ResponseWriter, r *http.Request, user User) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
tok, err := a.ensureUserWebhookToken(r.Context(), user.Username)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, tok)
|
||||
case http.MethodPost:
|
||||
tok, err := a.rotateUserWebhookToken(r.Context(), user.Username)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, tok)
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue