Working post to Create OS
This commit is contained in:
parent
45c6cebc2c
commit
c84ea161f3
@ -77,3 +77,15 @@ func GetVersionByDistributionList(db *sql.DB, d string) ([]null.String, error) {
|
|||||||
}
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateOS(os OS, db *sql.DB) error {
|
||||||
|
q, err := db.Prepare("INSERT INTO `dashboard_os` (`distribution`,`version`,`end_of_support`) VALUES (?,?,?)")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error creating query", err)
|
||||||
|
}
|
||||||
|
_, err = q.Exec(&os.Distribution, &os.Version, &os.End_of_support)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error inserting OS", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@ -102,6 +102,33 @@ func GetVersionByDistributionList(w http.ResponseWriter, r *http.Request) {
|
|||||||
json.NewEncoder(w).Encode(list)
|
json.NewEncoder(w).Encode(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateOS(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var os db.OS
|
||||||
|
var err error
|
||||||
|
|
||||||
|
t := time.Now()
|
||||||
|
status := 200
|
||||||
|
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error reading request body", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
params := make(map[string]null.String)
|
||||||
|
json.Unmarshal(body, ¶ms)
|
||||||
|
os.Distribution = params["distribution"]
|
||||||
|
os.Version = params["version"]
|
||||||
|
os.End_of_support = params["end_of_support"]
|
||||||
|
db_conn := db.GetDatabaseConnection()
|
||||||
|
err = db.CreateOS(os, db_conn)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error creating new OS", err)
|
||||||
|
status = 500
|
||||||
|
}
|
||||||
|
logRequest(t, r, status)
|
||||||
|
w.WriteHeader(status)
|
||||||
|
}
|
||||||
|
|
||||||
func logRequest(t time.Time, r *http.Request, s int) {
|
func logRequest(t time.Time, r *http.Request, s int) {
|
||||||
log.Printf("%s - - %s \"%s %s %s\" %d 0 \"-\" \"%s\" %d\n",
|
log.Printf("%s - - %s \"%s %s %s\" %d 0 \"-\" \"%s\" %d\n",
|
||||||
r.Host,
|
r.Host,
|
||||||
|
|||||||
1
main.go
1
main.go
@ -30,6 +30,7 @@ func main() {
|
|||||||
router.HandleFunc("/healthcheck", h.HealthHandler)
|
router.HandleFunc("/healthcheck", h.HealthHandler)
|
||||||
router.HandleFunc("/os", h.GetOS).Methods("GET")
|
router.HandleFunc("/os", h.GetOS).Methods("GET")
|
||||||
router.HandleFunc("/os/{id:[0-9]+}", h.GetOSbyID).Methods("GET")
|
router.HandleFunc("/os/{id:[0-9]+}", h.GetOSbyID).Methods("GET")
|
||||||
|
router.HandleFunc("/os/create", h.CreateOS).Methods("POST")
|
||||||
router.HandleFunc("/distribution", h.GetDistributionList).Methods("GET")
|
router.HandleFunc("/distribution", h.GetDistributionList).Methods("GET")
|
||||||
router.HandleFunc("/distribution/{distribution:[a-zA-Z]+}/version", h.GetVersionByDistributionList).Methods("GET")
|
router.HandleFunc("/distribution/{distribution:[a-zA-Z]+}/version", h.GetVersionByDistributionList).Methods("GET")
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user