1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
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")
oauthClientConfig, err := serviceConfig()
if err != nil {
log.Fatal("failed to load service creds")
}
renderer, err := tmpl.NewHTMLTmpl("templates")
if err != nil {
log.Fatal(err)
}
storagePath, err := os.Getwd()
if err != nil {
log.Fatal("couldn't find directory to write images to")
}
sessionManager := scs.New()
sessionManager.Lifetime = 24 * time.Hour
// Image
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))
}
|