-
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.
Merge pull request #1 from jacobmichels/clean-layout
Clean layout
- Loading branch information
Showing
20 changed files
with
774 additions
and
635 deletions.
There are no files selected for viewing
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,60 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/jacobmichels/Course-Sense-Go/config" | ||
"github.com/jacobmichels/Course-Sense-Go/firestore" | ||
"github.com/jacobmichels/Course-Sense-Go/notifier" | ||
"github.com/jacobmichels/Course-Sense-Go/register" | ||
"github.com/jacobmichels/Course-Sense-Go/server" | ||
"github.com/jacobmichels/Course-Sense-Go/trigger" | ||
"github.com/jacobmichels/Course-Sense-Go/webadvisor" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
sig := make(chan os.Signal, 1) | ||
signal.Notify(sig, os.Interrupt) | ||
go func() { | ||
<-sig | ||
log.Println("received interrupt, shutting down") | ||
cancel() | ||
}() | ||
|
||
cfg, err := config.ReadConfig() | ||
if err != nil { | ||
log.Panicf("failed to get config: %s", err) | ||
} | ||
|
||
webadvisorService, err := webadvisor.NewWebAdvisorSectionService() | ||
if err != nil { | ||
log.Panicf("failed to create WebAdvisorSectionService: %s", err) | ||
} | ||
|
||
firestoreService, err := firestore.NewFirestoreWatcherService(ctx, cfg.Firestore.ProjectID, cfg.Firestore.SectionCollectionID, cfg.Firestore.WatcherCollectionID, cfg.Firestore.CredentialsFilePath) | ||
if err != nil { | ||
log.Panicf("failed to create FirestoreWatcherService: %s", err) | ||
} | ||
|
||
emailNotifier := notifier.NewEmail(cfg.Smtp.Host, cfg.Smtp.Username, cfg.Smtp.Password, cfg.Smtp.From, cfg.Smtp.Port) | ||
|
||
register := register.NewRegister(webadvisorService, firestoreService) | ||
trigger := trigger.NewTrigger(webadvisorService, firestoreService, emailNotifier) | ||
|
||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8080" | ||
} | ||
|
||
srv := server.NewServer(fmt.Sprintf(":%s", port), register, trigger) | ||
if err = srv.Start(ctx); err != nil { | ||
log.Panicf("Server failure: %s", err) | ||
} | ||
} |
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,90 @@ | ||
package coursesense | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Domain types are defined in this file | ||
|
||
type Course struct { | ||
Department string `json:"department"` | ||
Code int `json:"code"` | ||
} | ||
|
||
func (c Course) Valid() error { | ||
if c.Department == "" { | ||
return errors.New("Department cannot be empty") | ||
} | ||
if c.Code <= 999 && c.Code >= 9999 { | ||
return errors.New("Course code must be 4 digits") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type Section struct { | ||
Course Course `json:"course"` | ||
Code string `json:"code"` | ||
Term string `json:"term"` | ||
} | ||
|
||
func (s Section) Valid() error { | ||
if s.Code == "" { | ||
return errors.New("Section code cannot be empty") | ||
} | ||
if s.Term == "" { | ||
return errors.New("Term cannot be empty") | ||
} | ||
return s.Course.Valid() | ||
} | ||
|
||
func (s Section) String() string { | ||
return fmt.Sprintf("%s*%d*%s*%s", s.Course.Department, s.Course.Code, s.Code, s.Term) | ||
} | ||
|
||
// Service that gets information on course sections | ||
type SectionService interface { | ||
Exists(context.Context, Section) (bool, error) | ||
GetAvailableSeats(context.Context, Section) (uint, error) | ||
} | ||
|
||
// A user registered for notifications on a Section | ||
type Watcher struct { | ||
Email string `json:"email"` | ||
Phone string `json:"phone"` | ||
} | ||
|
||
func (w Watcher) Valid() error { | ||
if w.Email == "" && w.Phone == "" { | ||
return errors.New("At least one contact method needs to be present") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (w Watcher) String() string { | ||
return fmt.Sprintf("%s:%s", w.Email, w.Phone) | ||
} | ||
|
||
// Service that manages Watchers | ||
type WatcherService interface { | ||
AddWatcher(context.Context, Section, Watcher) error | ||
GetWatchedSections(context.Context) ([]Section, error) | ||
GetWatchers(context.Context, Section) ([]Watcher, error) | ||
RemoveWatchers(context.Context, Section) error | ||
} | ||
|
||
// A type that sends can send notifications to Watchers | ||
type Notifier interface { | ||
Notify(context.Context, Section, ...Watcher) error | ||
} | ||
|
||
type TriggerService interface { | ||
Trigger(context.Context) error | ||
} | ||
|
||
type RegistrationService interface { | ||
Register(context.Context, Section, Watcher) error | ||
} |
Oops, something went wrong.