-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (56 loc) · 1.57 KB
/
main.go
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
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/apex/gateway/v2"
"github.com/apex/log"
jsonhandler "github.com/apex/log/handlers/json"
"github.com/apex/log/handlers/text"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type Record struct {
ID string `dynamodbav:"id" json:"id"`
Created time.Time `dynamodbav:"created,unixtime" json:"created"`
Expires time.Time `dynamodbav:"expires,unixtime" json:"expires"`
Latitude float64 `dynamodbav:"latitude" json:"latitude"`
Longitude float64 `dynamodbav:"longitude" json:"longitude"`
Title string `dynamodbav:"title" json:"title"`
Audio string `dynamodbav:"audio" json:"audio"`
}
type server struct {
router *http.ServeMux
db *dynamodb.Client
store *s3.Client
}
// Routes are defined here
func newServer(local bool) *server {
s := &server{router: &http.ServeMux{}}
if local {
log.SetHandler(text.Default)
log.Info("local mode")
s.db = dynamoLocal()
} else {
log.SetHandler(jsonhandler.Default)
log.Info("cloud mode")
s.db = dynamoCloud()
}
s.store = s3Cloud()
s.router.Handle("/", s.index())
s.router.Handle("/add", s.add())
return s
}
func main() {
_, awsDetected := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME")
log.WithField("awsDetected", awsDetected).Info("starting up")
s := newServer(!awsDetected)
var err error
if awsDetected {
err = gateway.ListenAndServe("", s.router)
} else {
err = http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PORT")), s.router)
}
log.WithError(err).Fatal("error listening")
}