[READ-ONLY] Mirror of https://github.com/andrioid/s3preview. Preview serving microservice for s3. Generates and uploads any missing thumbnails. Redirects to existing.
1package main
2
3import (
4 "fmt"
5 "github.com/BurntSushi/toml"
6 "github.com/codegangsta/cli"
7 "github.com/gorilla/mux"
8 "net/http"
9 "os"
10)
11
12type PreviewOptions struct {
13 Width int
14 Height int
15 Method string
16}
17
18type Config struct {
19 Previews map[string]PreviewOptions
20 Asset_Bucket string
21 Asset_Prefix string
22 Preview_Bucket string
23 Preview_Prefix string
24 StorageDomain string
25 ListenPort int
26 ListenPortSSL int
27 Seed bool
28 AWS_Key string
29 AWS_Secret string
30}
31
32var configuration Config
33
34func init() {
35}
36
37func main() {
38
39 app := cli.NewApp()
40 app.Name = "s3preview"
41 app.Authors = []cli.Author{
42 cli.Author{
43 Name: "Andri Óskarsson",
44 Email: "andri80@gmail.com",
45 },
46 }
47 app.Usage = "Previews (or thumbnails) for AWS S3 objects"
48 app.Version = "0.1.0"
49 app.HideVersion = true
50
51 app.Flags = []cli.Flag{
52 cli.StringFlag{
53 Name: "aws-key",
54 Usage: "AWS Access Key. Needs to have S3 Access.",
55 EnvVar: "AWS_ACCESS_KEY_ID",
56 },
57 cli.StringFlag{
58 Name: "aws-secret",
59 Usage: "AWS Secret Key",
60 EnvVar: "AWS_SECRET_ACCESS_KEY",
61 },
62 cli.IntFlag{
63 Name: "listen-http, l",
64 Usage: "HTTP listen port",
65 Value: 80,
66 EnvVar: "HTTP_PORT",
67 },
68 cli.StringFlag{
69 Name: "asset-bucket",
70 Usage: "Bucket to retrieve originals from",
71 EnvVar: "ASSET_BUCKET",
72 },
73 cli.StringFlag{
74 Name: "asset-prefix",
75 Usage: "Prefix for Asset Bucket. E.g. /originals",
76 EnvVar: "ASSET_PREFIX",
77 },
78 cli.StringFlag{
79 Name: "preview-bucket",
80 Usage: "Bucket to store previews on. Needs to be public for redirects to work.",
81 EnvVar: "PREVIEW_BUCKET",
82 },
83 cli.StringFlag{
84 Name: "preview-prefix",
85 Usage: "Prefix for Preview Bucket. E.g. /s3preview",
86 Value: "/s3preview",
87 EnvVar: "ASSET_PREFIX",
88 },
89 cli.BoolFlag{
90 Name: "generate",
91 Usage: "Will generate missing previews during startup.",
92 EnvVar: "GENERATE",
93 },
94 }
95
96 // TODO: Finish CLI'fying the program
97
98 app.Action = func(c *cli.Context) {
99 Configure(c)
100 PopulatePreviewCache()
101 if c.Bool("generate") == true {
102 GenerateMissing()
103 }
104
105 r := mux.NewRouter()
106
107 http.Handle("/", r)
108 registerHandlers(r)
109 //fmt.Println(configuration.Previews)
110 // fmt.Println(configuration.Previews["small"])
111 fmt.Printf("I'm listening (port %d)...\n", configuration.ListenPort)
112 http.ListenAndServe(fmt.Sprintf(":%d", configuration.ListenPort), nil)
113 }
114
115 app.Run(os.Args)
116}
117
118func Configure(c *cli.Context) {
119 if _, err := toml.DecodeFile("config.toml", &configuration); err != nil {
120 panic(err)
121 }
122 if c.String("aws-key") != "" {
123 configuration.AWS_Key = c.String("aws-key")
124 }
125 if c.String("aws-secret") != "" {
126 configuration.AWS_Secret = c.String("aws-secret")
127 }
128
129 if configuration.AWS_Key == "" || configuration.AWS_Secret == "" {
130 fmt.Println("AWS Configuration Required.")
131 os.Exit(1)
132 }
133}