Réorganisation des packages + renommage du projet en gitea-kan

This commit is contained in:
2019-11-28 12:13:01 +01:00
parent 18f2dbf592
commit 98f4288c8a
18 changed files with 21 additions and 29 deletions

View File

@ -1,7 +1,7 @@
package main
import (
"forge.cadoles.com/wpetit/gitea-kan/cmd/server/config"
"forge.cadoles.com/wpetit/gitea-kan/internal/config"
"github.com/gorilla/sessions"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/service"

View File

@ -6,8 +6,8 @@ import (
"net/http"
"os"
"forge.cadoles.com/wpetit/gitea-kan/cmd/server/route"
"forge.cadoles.com/wpetit/gitea-kan/config"
"forge.cadoles.com/wpetit/gitea-kan/internal/config"
"forge.cadoles.com/wpetit/gitea-kan/internal/route"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/pkg/errors"

View File

@ -1,31 +0,0 @@
package route
import (
"crypto/rand"
"net/http"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/template"
"gitlab.com/wpetit/goweb/template/html"
)
func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Data) template.Data {
ctn := container.Must(r.Context())
data, err := template.Extend(data,
html.WithFlashes(w, r, ctn),
)
if err != nil {
panic(errors.Wrap(err, "error while extending template data"))
}
return data
}
func generateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}

View File

@ -1,23 +0,0 @@
package route
import (
"net/http"
"forge.cadoles.com/wpetit/gitea-kan/config"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/session"
)
func handleLogout(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
conf := config.Must(ctn)
sess, err := session.Must(ctn).Get(w, r)
if err != nil {
panic(errors.Wrap(err, "could not retrieve session"))
}
if err := sess.Delete(w, r); err != nil {
panic(errors.Wrap(err, "could not delete session"))
}
http.Redirect(w, r, conf.Gitea.LogoutURL, http.StatusSeeOther)
}

View File

@ -1,46 +0,0 @@
package route
import (
"net/http"
"forge.cadoles.com/wpetit/gitea-kan/middleware"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/session"
"golang.org/x/oauth2"
)
func handleOAuth2Callback(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
sess, err := session.Must(ctn).Get(w, r)
if err != nil {
panic(errors.Wrap(err, "could not retrieve session"))
}
expectedState := sess.Get(middleware.SessionOAuth2State)
state := r.FormValue("state")
if state != expectedState {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
giteaOAuth2Config := middleware.GiteaOAuth2Config(ctn)
code := r.FormValue("code")
token, err := giteaOAuth2Config.Exchange(oauth2.NoContext, code)
if err != nil {
panic(errors.Wrap(err, "could not exchange oauth2 token"))
}
sess.Set(middleware.SessionOAuth2AccessToken, token.AccessToken)
sess.Set(middleware.SessionOAuth2State, "")
if err := sess.Save(w, r); err != nil {
panic(errors.Wrap(err, "could not save session"))
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}

View File

@ -1,41 +0,0 @@
package route
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"forge.cadoles.com/wpetit/gitea-kan/config"
"forge.cadoles.com/wpetit/gitea-kan/middleware"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/session"
)
func proxyAPIRequest(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
conf := config.Must(ctn)
apiBaseURL, err := url.Parse(conf.Gitea.APIBaseURL)
if err != nil {
panic(errors.Wrap(err, "could not parse api base url"))
}
sess, err := session.Must(ctn).Get(w, r)
if err != nil {
panic(errors.Wrap(err, "could not retrieve session"))
}
accessToken := sess.Get(middleware.SessionOAuth2AccessToken)
proxy := httputil.NewSingleHostReverseProxy(apiBaseURL)
proxy.Director = func(r *http.Request) {
r.Host = apiBaseURL.Host
r.URL.Scheme = apiBaseURL.Scheme
r.URL.Host = apiBaseURL.Host
r.Header.Add("Authorization", fmt.Sprintf("token %s", accessToken))
}
proxy.ServeHTTP(w, r)
}

View File

@ -1,33 +0,0 @@
package route
import (
"net/http"
"path"
"forge.cadoles.com/wpetit/gitea-kan/config"
"forge.cadoles.com/wpetit/gitea-kan/middleware"
"github.com/go-chi/chi"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/static"
)
func Mount(r *chi.Mux, config *config.Config) {
r.Group(func(r chi.Router) {
r.Get("/callback", handleOAuth2Callback)
// Authenticated routes
r.Group(func(r chi.Router) {
r.Use(middleware.Authenticate)
r.Get("/logout", handleLogout)
r.Get("/gitea/api/*", http.StripPrefix("/gitea", http.HandlerFunc(proxyAPIRequest)).ServeHTTP)
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", html5PushStateHandler))
})
})
}
func html5PushStateHandler(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
conf := config.Must(ctn)
indexFile := path.Join(conf.HTTP.PublicDir, "index.html")
http.ServeFile(w, r, indexFile)
}

View File

@ -1,17 +0,0 @@
{{define "base"}}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>{{block "title" . -}}{{- end}}</title>
<link rel="stylesheet" href="css/main.css">
{{- block "head_style" . -}}{{end}}
{{- block "head_script" . -}}{{end}}
</head>
<body>
{{- block "body" . -}}{{- end -}}
{{- block "body_script" . -}}{{end}}
</body>
</html>
{{end}}

View File

@ -1,8 +0,0 @@
{{define "title"}}Gitea Apps{{end}}
{{define "body"}}
<div id="gitea-apps" class="is-fullheight"></div>
{{end}}
{{define "body_scripts"}}
<script type="text/javascript" src="main.js"></script>
{{end}}
{{template "base" .}}