-
Notifications
You must be signed in to change notification settings - Fork 2
HTTP Server and Entrypoint #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aec2358
http server and entrypoint
hayesZach 46cc4a7
close db connections
hayesZach 815bcf0
mitigate slowloris attack
hayesZach 2f60630
return error instead of panicking
hayesZach 5d01c79
Merge branch 'feature/middleware' into feature/server
hayesZach 43c865b
Merge branch 'feature/middleware' into feature/server
hayesZach 094c2c4
check for empty token
hayesZach ba65721
Revert "check for empty token"
hayesZach 8e00040
Merge branch 'feature/middleware' into feature/server
hayesZach 9b2360a
Merge branch 'master' into feature/server
hayesZach 4766bd4
add factory
hayesZach 0d0b20f
use correct type
hayesZach fd581b0
update factory and add tests
hayesZach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package repository | ||
|
|
||
| type Factory interface { | ||
| Key() KeyRepository | ||
| Marketplace() MarketplaceRepository | ||
| AdminAudit() AdminAuditRepository | ||
| Reversal() ReversalRepository | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package testutil | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "reverse-watch/domain/repository" | ||
| ) | ||
|
|
||
| type factory struct { | ||
| key repository.KeyRepository | ||
| marketplace repository.MarketplaceRepository | ||
| adminAudit repository.AdminAuditRepository | ||
| reversal repository.ReversalRepository | ||
| } | ||
|
|
||
| func NewTestFactory(t *testing.T) *factory { | ||
| t.Helper() | ||
| return &factory{} | ||
| } | ||
|
|
||
| func (f *factory) Key() repository.KeyRepository { | ||
| return f.key | ||
| } | ||
|
|
||
| func (f *factory) Marketplace() repository.MarketplaceRepository { | ||
| return f.marketplace | ||
| } | ||
|
|
||
| func (f *factory) AdminAudit() repository.AdminAuditRepository { | ||
| return f.adminAudit | ||
| } | ||
|
|
||
| func (f *factory) Reversal() repository.ReversalRepository { | ||
| return f.reversal | ||
| } | ||
|
|
||
| func (f *factory) WithKey(key repository.KeyRepository) *factory { | ||
| f.key = key | ||
| return f | ||
| } | ||
|
|
||
| func (f *factory) WithMarketplace(marketplace repository.MarketplaceRepository) *factory { | ||
| f.marketplace = marketplace | ||
| return f | ||
| } | ||
|
|
||
| func (f *factory) WithAdminAudit(adminAudit repository.AdminAuditRepository) *factory { | ||
| f.adminAudit = adminAudit | ||
| return f | ||
| } | ||
|
|
||
| func (f *factory) WithReversal(reversal repository.ReversalRepository) *factory { | ||
| f.reversal = reversal | ||
| return f | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "reverse-watch/config" | ||
| "reverse-watch/domain/models" | ||
| "reverse-watch/logging" | ||
| "reverse-watch/server" | ||
| ) | ||
|
|
||
| func main() { | ||
| logging.Initialize() | ||
| cfg := config.Load() | ||
| models.InitSnowflakeGenerator(0, 0) | ||
|
|
||
| logging.Log.Info("Starting Reverse Watch") | ||
|
|
||
| done := make(chan os.Signal, 1) | ||
| signal.Notify(done, os.Interrupt, syscall.SIGTERM) | ||
|
|
||
| srv, err := server.New(cfg) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| httpSrv := &http.Server{ | ||
| Addr: fmt.Sprintf("0.0.0.0:%s", cfg.HTTP.Port), | ||
| Handler: srv, | ||
| ReadHeaderTimeout: 5 * time.Second, | ||
| ReadTimeout: 10 * time.Second, | ||
| WriteTimeout: 10 * time.Second, | ||
| IdleTimeout: 120 * time.Second, | ||
| } | ||
|
|
||
| go func() { | ||
| logging.Log.Infof("Starting HTTP Server on %v", httpSrv.Addr) | ||
| if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
| panic(err) | ||
| } | ||
| }() | ||
|
|
||
| <-done | ||
|
|
||
| logging.Log.Info("Shutting down server connections gracefully") | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| if err := httpSrv.Shutdown(ctx); err != nil { | ||
| panic(err) | ||
| } | ||
hayesZach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Close db connections | ||
| if err := srv.Close(); err != nil { | ||
| panic(err) | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| "reverse-watch/domain/repository" | ||
| ) | ||
|
|
||
| const FactoryContextKey ContextKey = "factory" | ||
|
|
||
| func FactoryMiddleware(factory repository.Factory) func(http.Handler) http.Handler { | ||
| return func(next http.Handler) http.Handler { | ||
| fn := func(w http.ResponseWriter, r *http.Request) { | ||
| ctx := context.WithValue(r.Context(), FactoryContextKey, factory) | ||
| next.ServeHTTP(w, r.WithContext(ctx)) | ||
| } | ||
| return http.HandlerFunc(fn) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "reverse-watch/internal/testutil" | ||
| ) | ||
|
|
||
| func TestFactoryMiddleware(t *testing.T) { | ||
| var capturedRequest *http.Request | ||
| fn := func(w http.ResponseWriter, r *http.Request) { | ||
| capturedRequest = r | ||
| w.WriteHeader(http.StatusOK) | ||
| } | ||
| next := http.HandlerFunc(fn) | ||
|
|
||
| factory := testutil.NewTestFactory(t) | ||
| factoryMiddleware := FactoryMiddleware(factory) | ||
| handler := factoryMiddleware(next) | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| handler.ServeHTTP(w, r) | ||
|
|
||
| if w.Code != http.StatusOK { | ||
| t.Errorf("got status code %d, wanted %d", w.Code, http.StatusOK) | ||
| } | ||
|
|
||
| if capturedRequest == nil { | ||
| t.Fatalf("captured request is nil") | ||
| } | ||
|
|
||
| capturedFactory := capturedRequest.Context().Value(FactoryContextKey) | ||
| if capturedFactory == nil { | ||
| t.Fatalf("captured factory is nil") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package factory | ||
|
|
||
| import "reverse-watch/domain/repository" | ||
|
|
||
| type factory struct { | ||
| private repository.PrivateRepository | ||
| public repository.PublicRepository | ||
| } | ||
|
|
||
| func NewFactory(private repository.PrivateRepository, public repository.PublicRepository) repository.Factory { | ||
| return &factory{ | ||
| private: private, | ||
| public: public, | ||
| } | ||
| } | ||
|
|
||
| func (f *factory) Key() repository.KeyRepository { | ||
| return f.private.Key() | ||
| } | ||
|
|
||
| func (f *factory) Marketplace() repository.MarketplaceRepository { | ||
| return f.private.Marketplace() | ||
| } | ||
|
|
||
| func (f *factory) AdminAudit() repository.AdminAuditRepository { | ||
| return f.private.AdminAudit() | ||
| } | ||
|
|
||
| func (f *factory) Reversal() repository.ReversalRepository { | ||
| return f.public.Reversal() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "reverse-watch/config" | ||
| "reverse-watch/domain/repository" | ||
| "reverse-watch/logging" | ||
| rwmiddleware "reverse-watch/middleware" | ||
| "reverse-watch/repository/factory" | ||
| "reverse-watch/repository/private" | ||
| "reverse-watch/repository/public" | ||
| "reverse-watch/secret" | ||
|
|
||
| "github.com/go-chi/chi/v5" | ||
| "github.com/go-chi/chi/v5/middleware" | ||
| ) | ||
|
|
||
| type Server struct { | ||
| r chi.Router | ||
|
|
||
| privateRepo repository.PrivateRepository | ||
| publicRepo repository.PublicRepository | ||
hayesZach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func New(cfg config.Config) (*Server, error) { | ||
| keygen := secret.NewKeyGenerator(cfg.Environment) | ||
| privateRepo, err := private.NewPrivateRepository(cfg, keygen) | ||
| if err != nil { | ||
| logging.Log.Errorf("failed to create private repository: %v", err) | ||
| return nil, fmt.Errorf("failed to create private repository: %v", err) | ||
| } | ||
| publicRepo, err := public.NewPublicRepository(cfg) | ||
| if err != nil { | ||
| privateRepo.Close() | ||
| logging.Log.Errorf("failed to create public repository: %v", err) | ||
| return nil, fmt.Errorf("failed to create public repository: %v", err) | ||
| } | ||
hayesZach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| r := chi.NewRouter() | ||
|
|
||
| r.Use(middleware.Recoverer) | ||
| r.Use(middleware.RequestID) | ||
| r.Use(middleware.RealIP) | ||
| r.Use(middleware.Logger) | ||
|
|
||
| f := factory.NewFactory(privateRepo, publicRepo) | ||
| r.Use(rwmiddleware.FactoryMiddleware(f)) | ||
|
|
||
| // TODO(zach): Define routes | ||
|
|
||
| return &Server{ | ||
| r: r, | ||
| privateRepo: privateRepo, | ||
| publicRepo: publicRepo, | ||
| }, nil | ||
| } | ||
|
|
||
| func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
| s.r.ServeHTTP(w, r) | ||
| } | ||
|
|
||
| func (s *Server) Close() error { | ||
| var errs []error | ||
| if err := s.privateRepo.Close(); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| if err := s.publicRepo.Close(); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| return errors.Join(errs...) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.