This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathroot.go
99 lines (90 loc) · 3.5 KB
/
root.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
package cmd
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
nlog "github.com/nuveo/log"
"github.com/prest/adapters/postgres"
"github.com/prest/config"
"github.com/prest/config/router"
"github.com/prest/controllers"
"github.com/prest/middlewares"
"github.com/spf13/cobra"
"github.com/urfave/negroni"
// postgres driver for migrate
_ "gopkg.in/mattes/migrate.v1/driver/postgres"
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "prest",
Short: "Serve a RESTful API from any PostgreSQL database",
Long: `Serve a RESTful API from any PostgreSQL database, start HTTP server`,
Run: func(cmd *cobra.Command, args []string) {
if config.PrestConf.Adapter == nil {
nlog.Warningln("adapter is not set. Using the default (postgres)")
postgres.Load()
}
startServer()
},
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
migrateCmd.AddCommand(createCmd)
migrateCmd.AddCommand(downCmd)
migrateCmd.AddCommand(gotoCmd)
migrateCmd.AddCommand(mversionCmd)
migrateCmd.AddCommand(nextCmd)
migrateCmd.AddCommand(redoCmd)
migrateCmd.AddCommand(upCmd)
migrateCmd.AddCommand(resetCmd)
RootCmd.AddCommand(versionCmd)
RootCmd.AddCommand(migrateCmd)
migrateCmd.PersistentFlags().StringVar(&urlConn, "url", driverURL(), "Database driver url")
migrateCmd.PersistentFlags().StringVar(&path, "path", config.PrestConf.MigrationsPath, "Migrations directory")
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
// MakeHandler reagister all routes
func MakeHandler() http.Handler {
n := middlewares.GetApp()
r := router.Get()
r.HandleFunc("/databases", controllers.GetDatabases).Methods("GET")
r.HandleFunc("/schemas", controllers.GetSchemas).Methods("GET")
r.HandleFunc("/tables", controllers.GetTables).Methods("GET")
r.HandleFunc("/_QUERIES/{queriesLocation}/{script}", controllers.ExecuteFromScripts)
r.HandleFunc("/{database}/{schema}", controllers.GetTablesByDatabaseAndSchema).Methods("GET")
crudRoutes := mux.NewRouter().PathPrefix("/").Subrouter().StrictSlash(true)
crudRoutes.HandleFunc("/{database}/{schema}/{table}", controllers.SelectFromTables).Methods("GET")
crudRoutes.HandleFunc("/{database}/{schema}/{table}", controllers.InsertInTables).Methods("POST")
crudRoutes.HandleFunc("/batch/{database}/{schema}/{table}", controllers.BatchInsertInTables).Methods("POST")
crudRoutes.HandleFunc("/{database}/{schema}/{table}", controllers.DeleteFromTable).Methods("DELETE")
crudRoutes.HandleFunc("/{database}/{schema}/{table}", controllers.UpdateTable).Methods("PUT", "PATCH")
r.PathPrefix("/").Handler(negroni.New(
middlewares.AccessControl(),
negroni.Wrap(crudRoutes),
))
n.UseHandler(r)
return n
}
func startServer() {
http.Handle(config.PrestConf.ContextPath, MakeHandler())
l := log.New(os.Stdout, "[prest] ", 0)
if !config.PrestConf.AccessConf.Restrict {
nlog.Warningln("You are running pREST in public mode.")
}
if config.PrestConf.Debug {
nlog.DebugMode = config.PrestConf.Debug
nlog.Warningln("You are running pREST in debug mode.")
}
addr := fmt.Sprintf("%s:%d", config.PrestConf.HTTPHost, config.PrestConf.HTTPPort)
l.Printf("listening on %s and serving on %s", addr, config.PrestConf.ContextPath)
if config.PrestConf.HTTPSMode {
l.Fatal(http.ListenAndServeTLS(addr, config.PrestConf.HTTPSCert, config.PrestConf.HTTPSKey, nil))
}
l.Fatal(http.ListenAndServe(addr, nil))
}