app.go 733 Bytes
package main

import (
	"fmt"
	"net/http"

	"github.com/gorilla/mux"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

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")

	return app
}

func (app *App) Serve() {
	http.ListenAndServe(fmt.Sprintf(":%d", app.Config.Port), app.router)
}