aboutsummaryrefslogtreecommitdiff
path: root/cmd/web
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/web')
-rw-r--r--cmd/web/main.go44
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))
}