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

View file

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