rename private handler

This commit is contained in:
Ruidy 2024-09-09 22:01:43 +02:00
parent 6fbac3ec39
commit 26c8996f0f
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 23 additions and 30 deletions

View file

@ -4,20 +4,18 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/labstack/gommon/log"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"golang.org/x/oauth2/google" "golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3" "google.golang.org/api/calendar/v3"
"google.golang.org/api/option" "google.golang.org/api/option"
) )
const tokFile = "token.json"
type Service struct { type Service struct {
calIds map[string]struct{} calIds map[string]struct{}
*calendar.Service *calendar.Service
@ -74,7 +72,7 @@ func (s *Service) Create(calendarId, name, description string, from, to time.Tim
Summary: description, Summary: description,
}).Do() }).Do()
log.Println(err) log.Print(err)
log.Printf("%+v: %s", ne.Summary, ne.Id) log.Printf("%+v: %s", ne.Summary, ne.Id)
return err return err
} }
@ -91,7 +89,7 @@ func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
tok, err := tokenFromEnv() tok, err := tokenFromEnv()
if err != nil { if err != nil {
tok = getTokenFromWeb(config) tok = getTokenFromWeb(config)
saveToken(tokFile, tok) saveToken("token.json", tok)
} }
return config.Client(ctx, tok) return config.Client(ctx, tok)
} }
@ -137,7 +135,7 @@ func tokenFromEnv() (*oauth2.Token, error) {
// Saves a token to a file path. // Saves a token to a file path.
func saveToken(path string, token *oauth2.Token) { func saveToken(path string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %s\n", path) log.Printf("Saving credential file to: %s\n", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil { if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err) log.Fatalf("Unable to cache oauth token: %v", err)

View file

@ -1,20 +1,15 @@
package server package server
import ( import (
"net/http"
_ "net/http/pprof"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
) )
func (s Server) MountHandlers() { func (s Server) MountHandlers() {
// public s.Router.GET("/healthz", handleHealthCheck())
s.Router.GET("/health", handleHealthCheck())
s.Router.GET("/debug/pprof/*", echo.WrapHandler(http.DefaultServeMux))
s.Router.GET("/", handleLoginPage()) s.Router.GET("/", handleLoginPage())
s.Router.POST("/", handleLogin(s.as)) s.Router.POST("/", handleLogin(s.as))
// api
api := s.Router.Group("/api") api := s.Router.Group("/api")
api.Use(middleware.KeyAuthWithConfig(middleware.KeyAuthConfig{ api.Use(middleware.KeyAuthWithConfig(middleware.KeyAuthConfig{
KeyLookup: "header:api-key", KeyLookup: "header:api-key",
@ -23,21 +18,21 @@ func (s Server) MountHandlers() {
}, },
})) }))
api.POST("/sync", handleSync(s.bs)) api.POST("/sync", handleSync(s.bs))
// admin
g := s.Router.Group("") private := s.Router.Group("")
g.Use(MakeAuthMiddleware()) private.Use(MakeAuthMiddleware())
g.GET("/bookings", handleBookingListPage(s.bs, s.hc)) private.GET("/bookings", handleBookingListPage(s.bs, s.hc))
g.GET("/bookings/new", handleBookingCreatePage(s.hc)) private.GET("/bookings/new", handleBookingCreatePage(s.hc))
g.POST("/bookings/new", handleBookingCreate(s.bs)) private.POST("/bookings/new", handleBookingCreate(s.bs))
g.GET("/bookings/:id", handleBookingPage(s.bs, s.hc)) private.GET("/bookings/:id", handleBookingPage(s.bs, s.hc))
g.PUT("/bookings/:id", handleBookingUpdate(s.bs, s.hc)) private.PUT("/bookings/:id", handleBookingUpdate(s.bs, s.hc))
g.PATCH("/bookings/:id/cancel", handleBookingCancel(s.bs)) private.PATCH("/bookings/:id/cancel", handleBookingCancel(s.bs))
g.POST("/bookings/:id/items", handleCreateItem(s.bs, s.cs, s.hc)) private.POST("/bookings/:id/items", handleCreateItem(s.bs, s.cs, s.hc))
g.POST("/items/:id", handleItemPay(s.bs)) private.GET("/bookings/pdf/:id", handlePdfCreateInvoice(s.bs, s.ps, s.hc))
g.PUT("/items/:id", handleItemUpdate(s.bs)) private.POST("/items/:id", handleItemPay(s.bs))
g.GET("/items/:id", handleLineItemForm(s.bs)) private.PUT("/items/:id", handleItemUpdate(s.bs))
g.GET("/bookings/pdf/:id", handlePdfCreateInvoice(s.bs, s.ps, s.hc)) private.GET("/items/:id", handleLineItemForm(s.bs))
g.GET("/reports", handleReportsPage()) private.GET("/reports", handleReportsPage())
g.GET("/reports/do", handleReportCompute(s.bs, s.hc)) private.GET("/reports/do", handleReportCompute(s.bs, s.hc))
g.GET("/reports/pdf", handlePdfCreateReport(s.bs, s.ps)) private.GET("/reports/pdf", handlePdfCreateReport(s.bs, s.ps))
} }