118 lines
4.7 KiB
Go
118 lines
4.7 KiB
Go
// app.go
|
|
|
|
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
// App : holds the router and db configuration
|
|
type App struct {
|
|
Router *mux.Router
|
|
DB *sql.DB
|
|
}
|
|
|
|
// Initialize : Initialize the app
|
|
func (a *App) Initialize(user, password, dbname string) {
|
|
connectionString :=
|
|
fmt.Sprintf("%s:%s@/%s?parseTime=true", user, password, dbname)
|
|
|
|
var err error
|
|
a.DB, err = sql.Open("mysql", connectionString)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
a.Router = mux.NewRouter()
|
|
a.initializeRoutes()
|
|
}
|
|
|
|
func respondWithError(w http.ResponseWriter, code int, message string) {
|
|
respondWithJSON(w, code, map[string]string{"error": message})
|
|
}
|
|
|
|
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
|
|
response, _ := json.Marshal(payload)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(code)
|
|
w.Write(response)
|
|
}
|
|
|
|
// Run : Run the app
|
|
func (a *App) Run(addr string) {
|
|
log.Fatal(http.ListenAndServe(":8000", a.Router))
|
|
}
|
|
|
|
// initializeRoutes : initialize all routes
|
|
func (a *App) initializeRoutes() {
|
|
|
|
a.Router.HandleFunc("/api/v1/configs", a.getConfigs).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/config", a.createConfig).Methods("POST")
|
|
a.Router.HandleFunc("/api/v1/config/{id:[0-9]+}", a.getConfig).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/config/{id:[0-9]+}", a.updateConfig).Methods("PUT")
|
|
a.Router.HandleFunc("/api/v1/config/{id:[0-9]+}", a.deleteConfig).Methods("DELETE")
|
|
|
|
a.Router.HandleFunc("/api/v1/dataTypes", a.getDataTypes).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/dataType", a.createDataType).Methods("POST")
|
|
a.Router.HandleFunc("/api/v1/dataType/{id:[0-9]+}", a.getDataType).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/dataType/{id:[0-9]+}", a.updateDataType).Methods("PUT")
|
|
a.Router.HandleFunc("/api/v1/dataType/{id:[0-9]+}", a.deleteDataType).Methods("DELETE")
|
|
|
|
a.Router.HandleFunc("/api/v1/deviceTypes", a.getDeviceTypes).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/deviceType", a.createDeviceType).Methods("POST")
|
|
a.Router.HandleFunc("/api/v1/deviceType/{id:[0-9]+}", a.getDeviceType).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/deviceType/{id:[0-9]+}", a.updateDeviceType).Methods("PUT")
|
|
a.Router.HandleFunc("/api/v1/deviceType/{id:[0-9]+}", a.deleteDeviceType).Methods("DELETE")
|
|
|
|
a.Router.HandleFunc("/api/v1/devices", a.getDevices).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/device", a.createDevice).Methods("POST")
|
|
a.Router.HandleFunc("/api/v1/device/{id:[0-9]+}", a.getDevice).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/device/{id:[0-9]+}", a.updateDevice).Methods("PUT")
|
|
a.Router.HandleFunc("/api/v1/device/{id:[0-9]+}", a.deleteDevice).Methods("DELETE")
|
|
|
|
a.Router.HandleFunc("/api/v1/files", a.getFiles).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/file", a.createFile).Methods("POST")
|
|
a.Router.HandleFunc("/api/v1/file/{id:[0-9]+}", a.getFile).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/file/{id:[0-9]+}", a.updateFile).Methods("PUT")
|
|
a.Router.HandleFunc("/api/v1/file/{id:[0-9]+}", a.deleteFile).Methods("DELETE")
|
|
|
|
a.Router.HandleFunc("/api/v1/tagClasses", a.getTagClasses).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/tagClass", a.createTagClass).Methods("POST")
|
|
a.Router.HandleFunc("/api/v1/tagClass/{id:[0-9]+}", a.getTagClass).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/tagClass/{id:[0-9]+}", a.updateTagClass).Methods("PUT")
|
|
a.Router.HandleFunc("/api/v1/tagClass/{id:[0-9]+}", a.deleteTagClass).Methods("DELETE")
|
|
|
|
a.Router.HandleFunc("/api/v1/tags", a.getTags).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/tag", a.createTag).Methods("POST")
|
|
a.Router.HandleFunc("/api/v1/tag/{id:[0-9]+}", a.getTag).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/tag/{id:[0-9]+}", a.updateTag).Methods("PUT")
|
|
a.Router.HandleFunc("/api/v1/tag/{id:[0-9]+}", a.deleteTag).Methods("DELETE")
|
|
|
|
a.Router.HandleFunc("/api/v1/tagValues", a.getTagValues).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/tagValues/latest", a.getLatestTagValues).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/tagValue", a.createTagValue).Methods("POST")
|
|
a.Router.HandleFunc("/api/v1/tagValue/{id:[0-9]+}", a.getTagValue).Methods("GET")
|
|
a.Router.HandleFunc("/api/v1/tagValue/{id:[0-9]+}", a.updateTagValue).Methods("PUT")
|
|
a.Router.HandleFunc("/api/v1/tagValue/{id:[0-9]+}", a.deleteTagValue).Methods("DELETE")
|
|
|
|
//Serve public files
|
|
var dir string
|
|
flag.StringVar(&dir, "dir", "./public", "the directory to serve files from. Defaults to the current dir")
|
|
flag.Parse()
|
|
a.Router.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir(dir))))
|
|
|
|
// Serve Root HTML Page
|
|
a.Router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, "./html/index.html")
|
|
})
|
|
}
|