Adding createServer handling
This commit is contained in:
@@ -2,7 +2,9 @@ package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gopkg.in/guregu/null.v4"
|
||||
db "infra-dashboard/Database"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -75,3 +77,30 @@ func GetServersbyOS(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(servers)
|
||||
}
|
||||
|
||||
func CreateServer(w http.ResponseWriter, r *http.Request) {
|
||||
var server db.Server
|
||||
var err error
|
||||
|
||||
t := time.Now()
|
||||
status := 204
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Println(err.Error(), "Error reading request body")
|
||||
}
|
||||
|
||||
params := make(map[string]null.String)
|
||||
json.Unmarshal(body, ¶ms)
|
||||
server.Hostname = params["hostname"]
|
||||
server.OsId = params["os_id"]
|
||||
dbConn := db.GetDatabaseConnection()
|
||||
defer dbConn.Close()
|
||||
err = db.CreateServer(server, dbConn)
|
||||
if err != nil {
|
||||
log.Println(err.Error(), "Error creating server")
|
||||
status = 500
|
||||
}
|
||||
logRequest(t, r, status)
|
||||
w.WriteHeader(status)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user