everything you need to create an atproto appview
at main 61 lines 1.3 kB view raw
1package server 2 3import ( 4 "net/http" 5 "time" 6 7 "github.com/go-chi/chi/v5" 8 "github.com/go-chi/chi/v5/middleware" 9) 10 11func (s *Server) Router() http.Handler { 12 r := chi.NewRouter() 13 14 // middleware 15 r.Use(middleware.RequestID) 16 r.Use(middleware.RealIP) 17 r.Use(s.loggingMiddleware) 18 r.Use(middleware.Recoverer) 19 r.Use(middleware.Timeout(60 * time.Second)) 20 21 // basic routes 22 r.Get("/", s.Home) 23 r.Get("/health", s.Health) 24 r.Get("/robots.txt", s.RobotsTxt) 25 26 // oauth routes 27 r.Get("/login", s.Login) 28 r.Post("/login", s.Login) 29 r.Post("/logout", s.Logout) 30 r.Post("/account/switch", s.SwitchAccount) 31 r.Post("/account/{did}", s.RemoveAccount) 32 r.Delete("/account/{did}", s.RemoveAccount) 33 34 r.Get("/oauth/client-metadata.json", s.OAuthClientMetadata) 35 r.Get("/oauth/jwks.json", s.OAuthJWKS) 36 r.Get("/oauth/callback", s.OAuthCallback) 37 38 // xrpc routes 39 r.Mount("/xrpc", s.XRPCRouter()) 40 41 return r 42} 43 44func (s *Server) loggingMiddleware(next http.Handler) http.Handler { 45 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 46 start := time.Now() 47 ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor) 48 49 defer func() { 50 s.logger.Info("request", 51 "method", r.Method, 52 "path", r.URL.Path, 53 "status", ww.Status(), 54 "bytes", ww.BytesWritten(), 55 "duration", time.Since(start), 56 ) 57 }() 58 59 next.ServeHTTP(ww, r) 60 }) 61}