-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api-server): Decouple server and command
With this commit, the server and the command are decoupled. The command now rests in the conventional /cmd/<command-name> location. The new server architecture opens up the possibility of better testing, too, with the use of the `amizone.ClientFactoryInterface` for the authentication handler layer.
- Loading branch information
Showing
12 changed files
with
121 additions
and
59 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 @@ | ||
package main |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package handlers | ||
|
||
import ( | ||
"github.com/ditsuke/go-amizone/amizone" | ||
"github.com/go-logr/logr" | ||
"net/http" | ||
) | ||
|
||
// Cfg is a handler configuration struct for the API. | ||
// It currently provides the handlers access to the logger configured by the main application. | ||
type Cfg struct { | ||
L logr.Logger | ||
A amizone.ClientFactoryInterface | ||
} | ||
|
||
func NewCfg(l logr.Logger, a amizone.ClientFactoryInterface) *Cfg { | ||
return &Cfg{L: l, A: a} | ||
} | ||
|
||
func (a *Cfg) AttendanceHandler(rw http.ResponseWriter, r *http.Request) { | ||
authenticatedHandlerWrapper(a, a.authenticatedAttendanceHandler)(rw, r) | ||
} | ||
|
||
func (a *Cfg) ClassScheduleHandler(rw http.ResponseWriter, r *http.Request) { | ||
authenticatedHandlerWrapper(a, a.authenticatedClassScheduleHandler)(rw, r) | ||
} | ||
|
||
func (a *Cfg) ExamScheduleHandler(rw http.ResponseWriter, r *http.Request) { | ||
authenticatedHandlerWrapper(a, a.authenticatedExamScheduleHandler)(rw, r) | ||
} |
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"github.com/ditsuke/go-amizone/amizone" | ||
"github.com/ditsuke/go-amizone/server/handlers" | ||
"github.com/go-logr/logr" | ||
"net/http" | ||
) | ||
|
||
type AmizoneClientFactory func(amizone.Credentials) amizone.ClientInterface | ||
|
||
type Config struct { | ||
Logger logr.Logger | ||
BindAddr string | ||
} | ||
|
||
func NewConfig() *Config { | ||
return &Config{ | ||
BindAddr: "127.0.0.1:8081", | ||
} | ||
} | ||
|
||
type ApiServer struct { | ||
Config *Config | ||
Router *http.ServeMux | ||
httpServer *http.Server | ||
} | ||
|
||
func New(config *Config) *ApiServer { | ||
return &ApiServer{ | ||
Config: config, | ||
Router: http.NewServeMux(), | ||
} | ||
} | ||
|
||
func (s *ApiServer) Run() error { | ||
if s.httpServer == nil { | ||
s.Config.Logger.V(1).Info("Initializing server...") | ||
s.httpServer = &http.Server{ | ||
Addr: s.Config.BindAddr, | ||
Handler: s.Router, | ||
} | ||
} | ||
|
||
s.Config.Logger.Info("Starting server", "bind_addr", s.Config.BindAddr) | ||
s.configureRouter() | ||
return s.httpServer.ListenAndServe() | ||
} | ||
|
||
func (s *ApiServer) Stop(ctx context.Context) error { | ||
return s.httpServer.Shutdown(ctx) | ||
} | ||
|
||
func (s *ApiServer) configureRouter() { | ||
handlerCfg := handlers.NewCfg(s.Config.Logger, func(cred amizone.Credentials, httpClient *http.Client) (amizone.ClientInterface, error) { | ||
return amizone.NewClient(cred, httpClient) | ||
}) | ||
|
||
s.Router.HandleFunc("/attendance", handlerCfg.AttendanceHandler) | ||
s.Router.HandleFunc("/class_schedule", handlerCfg.ClassScheduleHandler) | ||
s.Router.HandleFunc("/exam_schedule", handlerCfg.ExamScheduleHandler) | ||
} |