refacto os
This commit is contained in:
parent
0663f41f66
commit
29e1a09219
@ -61,7 +61,7 @@ func GetDistributionList(db *sql.DB) ([]null.String, error) {
|
|||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetVersionByDistributionList(db *sql.DB, d string) ([]null.String, error) {
|
func GetVersionsByDistributionList(db *sql.DB, d string) ([]null.String, error) {
|
||||||
var list []null.String
|
var list []null.String
|
||||||
rows, err := db.Query("SELECT DISTINCT version FROM dashboard_os where distribution = ?", d)
|
rows, err := db.Query("SELECT DISTINCT version FROM dashboard_os where distribution = ?", d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
119
Http/os.go
Normal file
119
Http/os.go
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
db "infra-dashboard/Database"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"gopkg.in/guregu/null.v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetOS(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var list []db.OS
|
||||||
|
var err error
|
||||||
|
|
||||||
|
t := time.Now()
|
||||||
|
status := 200
|
||||||
|
|
||||||
|
db_conn := db.GetDatabaseConnection()
|
||||||
|
defer db_conn.Close()
|
||||||
|
list, err = db.GetOS(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 GetOSbyID(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var os db.OS
|
||||||
|
var err error
|
||||||
|
|
||||||
|
t := time.Now()
|
||||||
|
status := 200
|
||||||
|
|
||||||
|
params := mux.Vars(r)
|
||||||
|
db_conn := db.GetDatabaseConnection()
|
||||||
|
defer db_conn.Close()
|
||||||
|
os, err = db.GetOSbyID(db_conn, params["id"])
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error getting OS")
|
||||||
|
status = 500
|
||||||
|
}
|
||||||
|
logRequest(t, r, status)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(os)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDistributionList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var list []null.String
|
||||||
|
var err error
|
||||||
|
|
||||||
|
t := time.Now()
|
||||||
|
status := 200
|
||||||
|
|
||||||
|
db_conn := db.GetDatabaseConnection()
|
||||||
|
defer db_conn.Close()
|
||||||
|
list, err = db.GetDistributionList(db_conn)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error getting distribution list")
|
||||||
|
status = 500
|
||||||
|
}
|
||||||
|
logRequest(t, r, status)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(list)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetVersionsByDistributionList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var list []null.String
|
||||||
|
var err error
|
||||||
|
|
||||||
|
t := time.Now()
|
||||||
|
status := 200
|
||||||
|
|
||||||
|
params := mux.Vars(r)
|
||||||
|
db_conn := db.GetDatabaseConnection()
|
||||||
|
defer db_conn.Close()
|
||||||
|
list, err = db.GetVersionsByDistributionList(db_conn, params["distribution"])
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error getting distribution list")
|
||||||
|
status = 500
|
||||||
|
}
|
||||||
|
logRequest(t, r, status)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(list)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateOS(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var os db.OS
|
||||||
|
var err error
|
||||||
|
|
||||||
|
t := time.Now()
|
||||||
|
status := 204
|
||||||
|
|
||||||
|
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()
|
||||||
|
defer db_conn.Close()
|
||||||
|
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)
|
||||||
|
}
|
||||||
111
Http/utils.go
111
Http/utils.go
@ -1,16 +1,11 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
db "infra-dashboard/Database"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"gopkg.in/guregu/null.v4"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -24,112 +19,6 @@ func HealthHandler(w http.ResponseWriter, _ *http.Request) {
|
|||||||
_, _ = io.WriteString(w, "OK")
|
_, _ = io.WriteString(w, "OK")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOS(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var list []db.OS
|
|
||||||
var err error
|
|
||||||
|
|
||||||
t := time.Now()
|
|
||||||
status := 200
|
|
||||||
|
|
||||||
db_conn := db.GetDatabaseConnection()
|
|
||||||
defer db_conn.Close()
|
|
||||||
list, err = db.GetOS(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 GetOSbyID(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var os db.OS
|
|
||||||
var err error
|
|
||||||
|
|
||||||
t := time.Now()
|
|
||||||
status := 200
|
|
||||||
|
|
||||||
params := mux.Vars(r)
|
|
||||||
db_conn := db.GetDatabaseConnection()
|
|
||||||
defer db_conn.Close()
|
|
||||||
os, err = db.GetOSbyID(db_conn, params["id"])
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error getting OS")
|
|
||||||
status = 500
|
|
||||||
}
|
|
||||||
logRequest(t, r, status)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
json.NewEncoder(w).Encode(os)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetDistributionList(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var list []null.String
|
|
||||||
var err error
|
|
||||||
|
|
||||||
t := time.Now()
|
|
||||||
status := 200
|
|
||||||
|
|
||||||
db_conn := db.GetDatabaseConnection()
|
|
||||||
defer db_conn.Close()
|
|
||||||
list, err = db.GetDistributionList(db_conn)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error getting distribution list")
|
|
||||||
status = 500
|
|
||||||
}
|
|
||||||
logRequest(t, r, status)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
json.NewEncoder(w).Encode(list)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetVersionByDistributionList(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var list []null.String
|
|
||||||
var err error
|
|
||||||
|
|
||||||
t := time.Now()
|
|
||||||
status := 200
|
|
||||||
|
|
||||||
params := mux.Vars(r)
|
|
||||||
db_conn := db.GetDatabaseConnection()
|
|
||||||
defer db_conn.Close()
|
|
||||||
list, err = db.GetVersionByDistributionList(db_conn, params["distribution"])
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error getting distribution list")
|
|
||||||
status = 500
|
|
||||||
}
|
|
||||||
logRequest(t, r, status)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
json.NewEncoder(w).Encode(list)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateOS(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var os db.OS
|
|
||||||
var err error
|
|
||||||
|
|
||||||
t := time.Now()
|
|
||||||
status := 204
|
|
||||||
|
|
||||||
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()
|
|
||||||
defer db_conn.Close()
|
|
||||||
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,
|
||||||
|
|||||||
4
main.go
4
main.go
@ -31,8 +31,8 @@ func main() {
|
|||||||
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("/os/create", h.CreateOS).Methods("POST")
|
||||||
router.HandleFunc("/distribution", h.GetDistributionList).Methods("GET")
|
router.HandleFunc("/os/distribution", h.GetDistributionList).Methods("GET")
|
||||||
router.HandleFunc("/distribution/{distribution:[a-zA-Z]+}/version", h.GetVersionByDistributionList).Methods("GET")
|
router.HandleFunc("/os/distribution/{distribution:[a-zA-Z]+}/versions", h.GetVersionsByDistributionList).Methods("GET")
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(":8080", router))
|
log.Fatal(http.ListenAndServe(":8080", router))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user