diff options
| author | Max Resnick <max@ofmax.li> | 2020-08-14 23:13:41 -0700 |
|---|---|---|
| committer | Max Resnick <max@ofmax.li> | 2020-11-08 07:57:13 -0800 |
| commit | 689a57ec4a444f8233fe2e5ec7ceb0903218218d (patch) | |
| tree | 1bcfe6786c38b4ae11997d5d97dc3c5fba747b97 /cmd/web/main.go | |
| parent | 77c2e6aca2dc0f851f55e30a0f49c9ee7c2c952e (diff) | |
| download | iserv-689a57ec4a444f8233fe2e5ec7ceb0903218218d.tar.gz | |
Diffstat (limited to '')
| -rw-r--r-- | cmd/web/main.go | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/cmd/web/main.go b/cmd/web/main.go index f215d1e..093dad6 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -1,22 +1,46 @@ package main import ( + "io/ioutil" "log" "net/http" "os" "path" + "time" + "github.com/alexedwards/scs/v2" "github.com/go-chi/chi" + "github.com/go-chi/chi/middleware" + "golang.org/x/oauth2" + "golang.org/x/oauth2/google" + "git.ofmax.li/iserv/internal/auth" "git.ofmax.li/iserv/internal/db/redis" "git.ofmax.li/iserv/internal/fs" "git.ofmax.li/iserv/internal/image" "go.ofmax.li/tmpl" ) +func serviceConfig() (*oauth2.Config, error) { + b, err := ioutil.ReadFile("credentials.json") + if err != nil { + log.Fatalf("Unable to read client secret file: %v", err) + } + scopes := []string{ + "https://www.googleapis.com/auth/userinfo.email", + "openid", + } + config, err := google.ConfigFromJSON(b, scopes...) + return config, err +} + func main() { connPool := redis.CreatePool("localhost:6379") - db := redis.NewRedisImageRepo(connPool) + + oauthClientConfig, err := serviceConfig() + if err != nil { + log.Fatal("failed to load service creds") + } renderer, err := tmpl.NewHTMLTmpl("templates") if err != nil { @@ -27,20 +51,36 @@ func main() { if err != nil { log.Fatal("couldn't find directory to write images to") } + sessionManager := scs.New() + sessionManager.Lifetime = 24 * time.Hour // Image - imageService := image.NewService(db, storagePath, renderer) + imgdb := redis.NewRedisImageRepo(connPool) + imageService := image.NewService(imgdb, storagePath, renderer) imageHandler := image.NewHandler(imageService) imageFile := fs.NewHandler(storagePath) + // Auth + authdb := redis.NewRedisAuthRepo(connPool) + authService := auth.NewService(authdb) + authHandler := auth.NewHandler(authService, sessionManager, oauthClientConfig) + // Static Files staticFiles := fs.NewHandler(path.Join(storagePath, "static")) + r := chi.NewRouter() + r.Use(middleware.Logger) + r.Use(sessionManager.LoadAndSave) + + r.Get("/a/g", authHandler.OauthCallback) + r.Get("/l", authHandler.Login) r.Get("/i/{fileName}", imageHandler.GetImage) r.Post("/u", imageHandler.PostImage) r.Get("/f/*", imageFile) + r.Get("/static/*", staticFiles) + log.Print("starting imageserv") log.Fatal(http.ListenAndServe(":8080", r)) } |