app.go
869 Bytes
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
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Prop int
const (
PropUserNo Prop = iota
)
type App struct {
Config Config
db *sqlx.DB
router *mux.Router
}
func NewApp(config Config) *App {
app := new(App)
app.Config = config
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s", config.Database.User, config.Database.Password, config.Database.Host, config.Database.Name)
app.db = sqlx.MustOpen("mysql", dsn)
app.router = mux.NewRouter()
app.router.HandleFunc("/users", app.PostUsers).Methods("POST")
app.router.HandleFunc("/users/tokens", app.PostTokens).Methods("POST")
app.router.Handle("/extractions", app.WithAuth(app.PostExtractions)).Methods("Post")
return app
}
func (app *App) Serve() {
http.ListenAndServe(fmt.Sprintf(":%d", app.Config.Port), app.router)
}