-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (39 loc) · 1.04 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/vvmk/shineserver/models"
"github.com/gorilla/handlers"
)
const port = ":8001"
type Env struct {
db models.Datastore
}
var env Env
var ssr_jwt_key []byte
func main() {
// set log flags
log.SetFlags(log.LstdFlags | log.Lshortfile)
ssr_jwt_key = []byte(os.Getenv("SSRJWT"))
// init db
dbU := os.Getenv("POSTGRES_USER")
dbP := os.Getenv("POSTGRES_PASSWORD")
dbD := os.Getenv("POSTGRES_DB")
dbH := os.Getenv("POSTGRES_HOST")
connStr := fmt.Sprintf("user=%s password=%s dbname=%s host=%s sslmode=disable", dbU, dbP, dbD, dbH)
db, err := models.NewDB(connStr)
if err != nil {
panic(err)
}
// inject
env = Env{db}
// serve
router := NewRouter()
// cors
headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE"})
origins := handlers.AllowedOrigins([]string{"*"})
log.Fatal(http.ListenAndServe(port, handlers.CORS(headers, methods, origins)(router)))
}