2019-02-21 04:38:45 +00:00
|
|
|
package main
|
|
|
|
|
|
2019-02-22 03:18:08 +00:00
|
|
|
import (
|
|
|
|
|
"internal-tools-server/api"
|
2019-02-27 16:35:03 +00:00
|
|
|
"internal-tools-server/models"
|
2019-02-26 17:06:21 +00:00
|
|
|
"internal-tools-server/storage"
|
2019-02-22 03:18:08 +00:00
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
|
)
|
2019-02-21 04:38:45 +00:00
|
|
|
|
2019-02-27 16:35:03 +00:00
|
|
|
const baseURL = "/api"
|
|
|
|
|
const apiVersion = "/v1"
|
|
|
|
|
|
2019-02-21 04:38:45 +00:00
|
|
|
func main() {
|
2019-02-26 17:06:21 +00:00
|
|
|
// Initialize the database
|
|
|
|
|
var err error
|
|
|
|
|
storage.StorageEngine, err = storage.CreateDatastore("postgres")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln("Exception while creating datastore")
|
|
|
|
|
}
|
2019-02-22 03:18:08 +00:00
|
|
|
|
2019-02-27 16:35:03 +00:00
|
|
|
runMigrations()
|
|
|
|
|
|
2019-02-22 03:18:08 +00:00
|
|
|
router := httprouter.New()
|
|
|
|
|
|
2019-02-26 17:06:21 +00:00
|
|
|
// Account CRUD Endpoints
|
|
|
|
|
|
|
|
|
|
// Component CRUD Endpoints
|
2019-02-27 16:35:03 +00:00
|
|
|
router.GET(baseURL+apiVersion+"/components", api.GetComponents)
|
|
|
|
|
router.POST(baseURL+apiVersion+"/components", api.CreateComponents)
|
|
|
|
|
router.PUT(baseURL+apiVersion+"/components", api.UpdateComponent)
|
2019-02-26 17:06:21 +00:00
|
|
|
|
|
|
|
|
// Page CRUD Endpoints
|
|
|
|
|
|
|
|
|
|
// Query CRUD Endpoints
|
2019-02-22 03:18:08 +00:00
|
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(":8000", router))
|
|
|
|
|
}
|
2019-02-27 16:35:03 +00:00
|
|
|
|
|
|
|
|
func runMigrations() {
|
|
|
|
|
log.Println("Going to run migrations")
|
|
|
|
|
storage.StorageEngine.GetDatastore().AutoMigrate(
|
|
|
|
|
&models.Component{},
|
|
|
|
|
&models.Account{},
|
|
|
|
|
&models.User{},
|
|
|
|
|
&models.Role{},
|
|
|
|
|
&models.Page{},
|
|
|
|
|
)
|
|
|
|
|
log.Println("Successfully run all migrations")
|
|
|
|
|
}
|