adding server handling
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
@@ -41,7 +42,12 @@ func GetOSbyID(w http.ResponseWriter, r *http.Request) {
|
||||
params := mux.Vars(r)
|
||||
db_conn := db.GetDatabaseConnection()
|
||||
defer db_conn.Close()
|
||||
os, err = db.GetOSbyID(db_conn, params["id"])
|
||||
id, err := strconv.ParseInt(params["id"], 10, 64)
|
||||
if err != nil {
|
||||
log.Println("Error converting ID", err)
|
||||
status = 500
|
||||
}
|
||||
os, err = db.GetOSbyID(db_conn, int64(id))
|
||||
if err != nil {
|
||||
log.Println("Error getting OS")
|
||||
status = 500
|
||||
|
||||
52
Http/server.go
Normal file
52
Http/server.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
db "infra-dashboard/Database"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func GetServersList(w http.ResponseWriter, r *http.Request) {
|
||||
var list []db.Server
|
||||
var err error
|
||||
|
||||
t := time.Now()
|
||||
status := 200
|
||||
|
||||
db_conn := db.GetDatabaseConnection()
|
||||
defer db_conn.Close()
|
||||
list, err = db.GetServersList(db_conn)
|
||||
if err != nil {
|
||||
log.Println("Error getting OS list")
|
||||
status = 500
|
||||
}
|
||||
logRequest(t, r, status)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(list)
|
||||
}
|
||||
|
||||
func GetServersbyID(w http.ResponseWriter, r *http.Request) {
|
||||
var server db.Server
|
||||
var err error
|
||||
|
||||
t := time.Now()
|
||||
status := 200
|
||||
|
||||
params := mux.Vars(r)
|
||||
id, err := strconv.ParseInt(params["id"], 10, 32)
|
||||
if err != nil {
|
||||
log.Println("Error converting ID", err)
|
||||
status = 500
|
||||
}
|
||||
db_conn := db.GetDatabaseConnection()
|
||||
defer db_conn.Close()
|
||||
server = db.GetServersbyID(db_conn, int64(id))
|
||||
logRequest(t, r, status)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(server)
|
||||
}
|
||||
Reference in New Issue
Block a user