-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Bootstrap http server for API (#18)
* refactor: Change ID to be a string * refactor: Reorganise code layout * feat: Server implements ServerInterfaces * feat: Add server start to ServerCommand * fix: linting issue
- Loading branch information
1 parent
f4656ca
commit fd07428
Showing
16 changed files
with
183 additions
and
36 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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,5 @@ | ||
package: server | ||
generate: | ||
std-http-server: true | ||
models: true | ||
output: server/server.gen.go |
This file contains 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,25 @@ | ||
package ctx | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
// SigtermCacellationContext returns a new context that is canceled when an | ||
// interrupt signal (SIGTERM or SIGINT) is received. | ||
// Additionaly invokes the provided onCancel function before canceling the context. | ||
func SigtermCacellationContext(ctx context.Context, onCancel func()) context.Context { | ||
ctx, cancel := context.WithCancel(ctx) | ||
|
||
interruptCh := make(chan os.Signal, 1) | ||
signal.Notify(interruptCh, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
|
||
go func() { | ||
<-interruptCh | ||
onCancel() | ||
cancel() | ||
}() | ||
return ctx | ||
} |
This file contains 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,3 @@ | ||
package main | ||
|
||
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=codegen-server.yaml openapi.yaml |
This file contains 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 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 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 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,23 @@ | ||
package server | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type Option func(*Server) error | ||
|
||
// WithPort is an option that sets the port the server listens on. | ||
func WithPort(port string) func(*Server) error { | ||
return func(s *Server) error { | ||
if port == "" { | ||
return errors.New("port cannot be empty") | ||
} | ||
|
||
if s.server != nil { | ||
s.server.Addr = fmt.Sprintf(":%s", port) | ||
} | ||
|
||
return nil | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,81 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
ShutdownGracePeriod = 10 * time.Second | ||
DefaultPort = 8080 | ||
DefaultReadTimeout = 5 * time.Second | ||
DefaultWriteTimeout = 10 * time.Second | ||
) | ||
|
||
type Server struct { | ||
logger *slog.Logger | ||
server *http.Server | ||
} | ||
|
||
var _ ServerInterface = (*Server)(nil) | ||
|
||
func New( | ||
logger *slog.Logger, | ||
opts ...Option, | ||
) (*Server, error) { | ||
server := &Server{ | ||
logger: logger, | ||
} | ||
|
||
server.server = &http.Server{ | ||
Handler: HandlerFromMux(server, http.NewServeMux()), | ||
Addr: fmt.Sprintf(":%v", DefaultPort), | ||
ReadTimeout: DefaultReadTimeout, | ||
WriteTimeout: DefaultWriteTimeout, | ||
} | ||
|
||
for _, opt := range opts { | ||
if err := opt(server); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return server, nil | ||
} | ||
|
||
// ListenAndServe starts the server. | ||
func (s *Server) ListenAndServer() error { | ||
s.logger.Info("server is listening on :8080") | ||
return s.server.ListenAndServe() | ||
} | ||
|
||
// Shutdown gracefully shuts down the underlying server without interrupting any active connections. | ||
func (s *Server) Shutdown() { | ||
ctx, cancel := context.WithTimeout(context.Background(), ShutdownGracePeriod) | ||
defer cancel() | ||
|
||
if err := s.server.Shutdown(ctx); err != nil { | ||
s.logger.Error("could not gracefully shutdown the server:", "err", err) | ||
return | ||
} | ||
|
||
s.logger.Info("server stopped") | ||
} | ||
|
||
func (s *Server) ItemsDelete(w http.ResponseWriter, _ *http.Request) { | ||
// TODO. | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} | ||
|
||
func (s *Server) ItemsList(w http.ResponseWriter, _ *http.Request) { | ||
// TODO. | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} | ||
|
||
func (s *Server) ItemsCreate(w http.ResponseWriter, _ *http.Request) { | ||
// TODO. | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} |
File renamed without changes.
This file contains 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
File renamed without changes.
File renamed without changes.