You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
519 lines
14 KiB
519 lines
14 KiB
package main |
|
|
|
import ( |
|
"encoding/json" |
|
"fmt" |
|
"io/ioutil" |
|
"net/http" |
|
"os" |
|
"spargcom/senuma/database" |
|
"spargcom/senuma/domain" |
|
"strconv" |
|
|
|
log "github.com/sirupsen/logrus" |
|
|
|
"github.com/gorilla/mux" |
|
) |
|
|
|
func main() { |
|
file, err := os.OpenFile("senuma.log", os.O_CREATE|os.O_APPEND, 0644) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
|
|
defer file.Close() |
|
|
|
log.SetOutput(file) |
|
log.SetLevel(log.InfoLevel) |
|
r := mux.NewRouter() |
|
r.Use(CORS) |
|
|
|
// Handler-Funktionscalls |
|
|
|
r.HandleFunc("/api/artikel", createArtikelHandler).Methods("POST") |
|
r.HandleFunc("/api/artikel/{id:[0-9]+}", deleteArtikelHandler).Methods("DELETE") |
|
r.HandleFunc("/api/artikel", showArtikelHandler).Methods("GET") |
|
r.HandleFunc("/api/artikel/{id:[0-9]+}", getArtikelHandler).Methods("GET") |
|
r.HandleFunc("/api/artikel/{id:[0-9]+}", updateArtikelHandler).Methods("PUT") |
|
|
|
r.HandleFunc("/api/kunden", createKundeHandler).Methods("POST") |
|
r.HandleFunc("/api/kunden/{id:[0-9]+}", deleteKundeHandler).Methods("DELETE") |
|
r.HandleFunc("/api/kunden", showKundeHandler).Methods("GET") |
|
r.HandleFunc("/api/kunden/{id:[0-9]+}", getKundeHandler).Methods("GET") |
|
r.HandleFunc("/api/kunden/{id:[0-9]+}", updateKundeHandler).Methods("PUT") |
|
|
|
r.HandleFunc("/api/lieferanten", createLieferantHandler).Methods("POST") |
|
r.HandleFunc("/api/lieferanten/{id:[0-9]+}", deleteLieferantHandler).Methods("DELETE") |
|
r.HandleFunc("/api/lieferanten", showLieferantHandler).Methods("GET") |
|
r.HandleFunc("/api/lieferanten/{id:[0-9]+}", getLieferantHandler).Methods("GET") |
|
r.HandleFunc("/api/lieferanten/{id:[0-9]+}", updateLieferantHandler).Methods("PUT") |
|
|
|
r.HandleFunc("/api/serial", createSerialHandler).Methods("POST") |
|
r.HandleFunc("/api/serial/{id:[0-9]+}", deleteSerialHandler).Methods("DELETE") |
|
r.HandleFunc("/api/serial", showSerialHandler).Methods("GET") |
|
r.HandleFunc("/api/serial/{id:[0-9]+}", getSerialHandler).Methods("GET") |
|
r.HandleFunc("/api/serial/{id:[0-9]+}", updateSerialHandler).Methods("PUT") |
|
|
|
//r.HandleFunc("/groups", createGroupHandler).Methods("POST") |
|
|
|
//r.HandleFunc("/users", createUserHandler).Methods("POST") |
|
|
|
//r.HandleFunc("/accounts", createAccountHandler).Methods("POST") |
|
|
|
//r.HandleFunc("/tunnels", createTunnelHandler).Methods("POST") |
|
http.Handle("/", r) |
|
http.ListenAndServe(":8081", r) |
|
log.Info("running on localhost, Port 8081") |
|
|
|
} |
|
|
|
// Artikel |
|
|
|
func createArtikelHandler(writer http.ResponseWriter, req *http.Request) { |
|
|
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var art domain.Artikel |
|
json.Unmarshal(body, &art) |
|
log.Println("Bezeichnung = ", art.Bezeichnung) |
|
log.Println("externe Art-Nr = ", art.ArtNrExt) |
|
log.Info("creating new Artikel", art.Bezeichnung) |
|
res, err := database.CreateArtikel(art) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := fmt.Sprintf("%s/%d", req.URL.String(), res.Id) |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusCreated) |
|
writer.Write(b) |
|
} |
|
|
|
func deleteArtikelHandler(writer http.ResponseWriter, req *http.Request) { |
|
artikelId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 64) |
|
log.Info("Deleting artikel with ID", artikelId) |
|
_, err := database.DeleteArtikel(artikelId) |
|
if err != nil { |
|
log.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showArtikelHandler(writer http.ResponseWriter, req *http.Request) { |
|
|
|
res, err := database.ShowArtikel() |
|
if err != nil { |
|
log.Fatal("Error: Artikel can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func getArtikelHandler(writer http.ResponseWriter, req *http.Request) { |
|
artikelId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 64) |
|
res, err := database.GetArtikel(artikelId) |
|
if err != nil { |
|
log.Fatal("Error: Artikel can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func updateArtikelHandler(writer http.ResponseWriter, req *http.Request) { |
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var art domain.Artikel |
|
json.Unmarshal(body, &art) |
|
log.Println("Bezeichnung = ", art.Bezeichnung) |
|
log.Println("externe Art-Nr = ", art.ArtNrExt) |
|
log.Info("updating Artikel", art.Bezeichnung) |
|
res, err := database.UpdateArtikel(art) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := fmt.Sprintf("%s/%d", req.URL.String(), res.Id) |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
// Kunde |
|
|
|
func createKundeHandler(writer http.ResponseWriter, req *http.Request) { |
|
|
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var kd domain.Kunde |
|
json.Unmarshal(body, &kd) |
|
log.Println("Name = ", kd.Name) |
|
log.Println("externe Kd-Nr = ", kd.KdNrExt) |
|
log.Info("creating new Kunde", kd.Name) |
|
res, err := database.CreateKunde(kd) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := fmt.Sprintf("%s/%d", req.URL.String(), res.Id) |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusCreated) |
|
writer.Write(b) |
|
} |
|
|
|
func deleteKundeHandler(writer http.ResponseWriter, req *http.Request) { |
|
kundeId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 64) |
|
log.Info("Deleting Kunde with ID", kundeId) |
|
_, err := database.DeleteKunde(uint(kundeId)) |
|
if err != nil { |
|
log.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showKundeHandler(writer http.ResponseWriter, req *http.Request) { |
|
|
|
res, err := database.ShowKunde() |
|
if err != nil { |
|
log.Fatal("Error: Kunde can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func getKundeHandler(writer http.ResponseWriter, req *http.Request) { |
|
kundeId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 64) |
|
res, err := database.GetKunde(uint(kundeId)) |
|
if err != nil { |
|
log.Fatal("Error: Kunde can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func updateKundeHandler(writer http.ResponseWriter, req *http.Request) { |
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var kunde domain.Kunde |
|
json.Unmarshal(body, &kunde) |
|
log.Println("Name = ", kunde.Name) |
|
log.Println("externe Kd-Nr = ", kunde.KdNrExt) |
|
log.Info("updating Kunde", kunde.Name) |
|
res, err := database.UpdateKunde(kunde) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := fmt.Sprintf("%s/%d", req.URL.String(), res.Id) |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
// Lieferanten |
|
|
|
func createLieferantHandler(writer http.ResponseWriter, req *http.Request) { |
|
|
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var lief domain.Lieferant |
|
json.Unmarshal(body, &lief) |
|
log.Println("Name = ", lief.Name) |
|
log.Println("externe Lieferanten-Nr = ", lief.LiefNrExt) |
|
log.Info("creating new Lieferant", lief.Name) |
|
res, err := database.CreateLieferant(lief) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := fmt.Sprintf("%s/%d", req.URL.String(), res.Id) |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusCreated) |
|
writer.Write(b) |
|
} |
|
|
|
func deleteLieferantHandler(writer http.ResponseWriter, req *http.Request) { |
|
lieferantId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 64) |
|
log.Info("Deleting Kunde with ID", lieferantId) |
|
_, err := database.DeleteLieferant(uint(lieferantId)) |
|
if err != nil { |
|
log.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showLieferantHandler(writer http.ResponseWriter, req *http.Request) { |
|
|
|
res, err := database.ShowLieferant() |
|
if err != nil { |
|
log.Fatal("Error: Lieferant can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func getLieferantHandler(writer http.ResponseWriter, req *http.Request) { |
|
lieferId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 64) |
|
res, err := database.GetLieferant(uint(lieferId)) |
|
if err != nil { |
|
log.Fatal("Error: Lieferant can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func updateLieferantHandler(writer http.ResponseWriter, req *http.Request) { |
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var lieferant domain.Lieferant |
|
json.Unmarshal(body, &lieferant) |
|
log.Println("Name = ", lieferant.Name) |
|
log.Println("externe Lieferanten-Nr = ", lieferant.LiefNrExt) |
|
log.Info("updating Lieferant", lieferant.Name) |
|
res, err := database.UpdateLieferant(lieferant) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := fmt.Sprintf("%s/%d", req.URL.String(), res.Id) |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
// Seriennummer |
|
|
|
func createSerialHandler(writer http.ResponseWriter, req *http.Request) { |
|
|
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var serial domain.Seriennummer |
|
json.Unmarshal(body, &serial) |
|
log.Println("Seriennummer = ", serial.SerienNr) |
|
//log.Println("externe Lieferanten-Nr = ", lief.LiefNrExt) |
|
log.Info("creating new Serial", serial.SerienNr) |
|
res, err := database.CreateSerial(serial) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := fmt.Sprintf("%s/%d", req.URL.String(), res.Id) |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusCreated) |
|
writer.Write(b) |
|
} |
|
|
|
func deleteSerialHandler(writer http.ResponseWriter, req *http.Request) { |
|
serialId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 64) |
|
log.Info("Deleting Serial with ID", serialId) |
|
_, err := database.DeleteSerial(uint64(serialId)) |
|
if err != nil { |
|
log.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showSerialHandler(writer http.ResponseWriter, req *http.Request) { |
|
|
|
res, err := database.ShowSerial() |
|
if err != nil { |
|
log.Fatal("Error: Serial can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func getSerialHandler(writer http.ResponseWriter, req *http.Request) { |
|
lieferId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 64) |
|
res, err := database.GetSerial(uint64(lieferId)) |
|
if err != nil { |
|
log.Fatal("Error: Serial can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := req.URL.String() |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
func updateSerialHandler(writer http.ResponseWriter, req *http.Request) { |
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var serial domain.Seriennummer |
|
json.Unmarshal(body, &serial) |
|
log.Println("Seriennummer = ", serial.SerienNr) |
|
//log.Println("externe Lieferanten-Nr = ", lieferant.LiefNrExt) |
|
log.Info("updating Serial", serial.SerienNr) |
|
res, err := database.UpdateSerial(serial) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
log.Fatal("Error: ", err) |
|
} |
|
|
|
location := fmt.Sprintf("%s/%d", req.URL.String(), res.Id) |
|
writer.Header().Set("Location:", location) |
|
writer.Header().Set("Content-Type", "application/json") |
|
writer.WriteHeader(http.StatusOK) |
|
writer.Write(b) |
|
} |
|
|
|
// General |
|
|
|
func CORS(next http.Handler) http.Handler { |
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
|
|
// Set headers |
|
w.Header().Set("Access-Control-Allow-Headers:", "*") |
|
w.Header().Set("Access-Control-Allow-Origin", "*") |
|
w.Header().Set("Access-Control-Allow-Methods", "*") |
|
|
|
if r.Method == "OPTIONS" { |
|
w.WriteHeader(http.StatusOK) |
|
return |
|
} |
|
|
|
fmt.Println("ok") |
|
|
|
// Next |
|
next.ServeHTTP(w, r) |
|
return |
|
}) |
|
}
|
|
|