68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"go.jolheiser.com/gistea/router/session"
|
|
"go.jolheiser.com/gistea/static"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/gorilla/sessions"
|
|
"github.com/markbates/goth"
|
|
"github.com/markbates/goth/gothic"
|
|
"github.com/markbates/goth/providers/gitea"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func New(app App) *chi.Mux {
|
|
app.GiteaURL = strings.TrimRight(app.GiteaURL, "/")
|
|
app.Domain = strings.TrimRight(app.Domain, "/")
|
|
callbackURL := fmt.Sprintf("%s/_/auth/callback", app.Domain)
|
|
|
|
goth.UseProviders(
|
|
gitea.NewCustomisedURL(app.GiteaClientKey, app.GiteaClientSecret, callbackURL,
|
|
fmt.Sprintf("%s/login/oauth/authorize", app.GiteaURL),
|
|
fmt.Sprintf("%s/login/oauth/access_token", app.GiteaURL),
|
|
fmt.Sprintf("%s/api/v1/user", app.GiteaURL),
|
|
),
|
|
)
|
|
gothStore := sessions.NewCookieStore([]byte(app.GiteaClientSecret))
|
|
gothStore.Options.HttpOnly = true
|
|
gothic.Store = gothStore
|
|
gothic.GetProviderName = func(_ *http.Request) (string, error) {
|
|
return "gitea", nil
|
|
}
|
|
|
|
store := session.NewStore(app.SessionSecret, app.GiteaURL)
|
|
app.store = store
|
|
|
|
r := chi.NewMux()
|
|
r.Use(middleware.Logger)
|
|
r.Use(middleware.Recoverer)
|
|
|
|
middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{
|
|
Logger: &log.Logger,
|
|
NoColor: true,
|
|
})
|
|
|
|
r.Route("/_", func(r chi.Router) {
|
|
r.Mount("/css", static.CSS)
|
|
r.Mount("/js", static.JS)
|
|
|
|
r.Route("/auth", func(r chi.Router) {
|
|
r.Get("/login", app.login)
|
|
r.Get("/logout", app.logout)
|
|
r.Get("/callback", app.loginCallback)
|
|
})
|
|
})
|
|
|
|
r.Route("/", func(r chi.Router) {
|
|
r.Get("/", app.newGist)
|
|
})
|
|
|
|
return r
|
|
}
|