-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
141 lines (116 loc) · 3.07 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"time"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
flag "github.com/spf13/pflag"
"github.com/tdewolff/auth"
"github.com/tdewolff/go-vue-template/api"
)
const urlScheme = "http"
func main() {
config := loadConfig()
db := initDatabase(config)
s := &http.Server{
Addr: config.Port,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
MaxHeaderBytes: 1 << 16,
}
// Setup providers and endpoints for OAuth
userStore := NewUserStore(db.DB)
authHandler := auth.New(userStore)
authHandler.SetCORS(config.DevURL)
for name, provider := range config.Providers {
callbackURL, err := url.Parse(config.URL)
if config.DevURL != "" {
callbackURL, err = url.Parse(config.DevURL)
}
if err != nil {
panic(err)
}
callbackURL.Path = path.Join(callbackURL.Path, provider.URI)
log.Println("API callback:", name, callbackURL.String())
authHandler.AddProvider(name, provider.ID, provider.Secret, callbackURL.String(), provider.Scopes)
}
http.HandleFunc("/auth/list", authHandler.Auth)
http.HandleFunc("/auth/token", authHandler.Token)
// Endpoints for the API and Vue client
vueHandler := http.FileServer(Vue("web/dist/"))
apiHandler := api.New(db)
apiHandler.SetCORS(config.DevURL)
http.Handle("/api/", authHandler.Middleware(apiHandler))
http.Handle("/", vueHandler)
log.Println("Listening on", config.Port, "at", config.URL)
log.Fatal(s.ListenAndServe())
}
type ConfigProvider struct {
ID string `json:"id"`
Secret string `json:"secret"`
URI string `json:"uri"`
Scopes []string `json:"scopes"`
}
type Config struct {
Name string
Port string
URL string
DevURL string
Database string
Scheme string
Providers map[string]ConfigProvider
}
func loadConfig() *Config {
configFilename := ""
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] [input]\n\nOptions:\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nInput:\n Files or directories, leave blank to use stdin\n")
}
flag.StringVarP(&configFilename, "config", "", "config.json", "Config filename")
flag.Parse()
configFile, err := os.Open(configFilename)
if err != nil {
panic(err)
}
defer configFile.Close()
config := &Config{
Port: ":3000",
URL: "http://localhost:8080",
Database: "sqlite.db",
Scheme: "scheme.sql",
}
if err := json.NewDecoder(configFile).Decode(config); err != nil {
panic("parsing config: " + err.Error())
}
return config
}
func initDatabase(config *Config) *sqlx.DB {
db := sqlx.MustConnect("sqlite3", config.Database)
schemeFile, err := os.Open(config.Scheme)
if err != nil {
panic(err)
}
defer schemeFile.Close()
scheme, err := ioutil.ReadAll(schemeFile)
if err != nil {
panic(err)
}
db.MustExec(string(scheme))
return db
}
////////////////
type Vue string
func (v Vue) Open(name string) (http.File, error) {
if ext := path.Ext(name); name != "/" && (ext == "" || ext == ".html") {
name = "index.html"
}
return http.Dir(v).Open(name)
}