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.
181 lines
5.9 KiB
181 lines
5.9 KiB
package main |
|
|
|
import ( |
|
"encoding/json" |
|
"fmt" |
|
"io" |
|
"net/http" |
|
"os" |
|
"strconv" |
|
|
|
"github.com/gorilla/mux" |
|
log "github.com/sirupsen/logrus" |
|
) |
|
|
|
const ( |
|
Author string = "Georg Spar" |
|
Version string = "0.1.0" |
|
ReleaseDate string = "2023-06-01" |
|
) |
|
|
|
func main() { |
|
fmt.Println("This is WardrobeMaster Version " + Version) |
|
file, err := os.OpenFile("wdmapi.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
defer file.Close() |
|
log.SetOutput(file) |
|
log.SetLevel(log.InfoLevel) |
|
r := mux.NewRouter() |
|
|
|
r.HandleFunc("/api/v1/typ", createTypHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/typ/{id:[0-9]+}", deleteTypHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/typ", showTypHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/typ/{id:[0-9]+}", getTypHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/typ/{id:[0-9]+}", updateTypHandler).Methods("PUT", "OPTIONS") |
|
|
|
/* r.HandleFunc("/api/v1/pho", createPhotoHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/pho/{id:[0-9]+}", deletePhotoHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/pho", showPhotoHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/pho/{id:[0-9]+}", getPhotoHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/pho/{id:[0-9]+}", updatePhotoHandler).Methods("PUT", "OPTIONS") |
|
|
|
r.HandleFunc("/api/v1/meas", createMeasureHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/meas/{id:[0-9]+}", deleteMeasureHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/meas", showMeasureHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/meas/{id:[0-9]+}", getMeasureHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/meas/{id:[0-9]+}", updateMeasureHandler).Methods("PUT", "OPTIONS") |
|
|
|
r.HandleFunc("/api/v1/itb", createItemBHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/itb/{id:[0-9]+}", deleteItemBHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/itb", showItemBHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/itb/{id:[0-9]+}", getItemBHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/itb/{id:[0-9]+}", updateItemBHandler).Methods("PUT", "OPTIONS") |
|
|
|
r.HandleFunc("/api/v1/ite", createItemExtHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/v1/ite/{id:[0-9]+}", deleteItemExtHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/v1/ite", showItemExtHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/ite/{id:[0-9]+}", getItemExtHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/v1/ite/{id:[0-9]+}", updateItemExtHandler).Methods("PUT", "OPTIONS") */ |
|
|
|
http.Handle("/", r) |
|
r.Use(mux.CORSMethodMiddleware(r)) |
|
http.ListenAndServe(":8081", r) |
|
log.Info("running on localhost, Port 8081") |
|
} |
|
|
|
// Typ Funktionen |
|
func createTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var typ Typ |
|
json.Unmarshal(body, &typ) |
|
log.Println(typ) |
|
log.Println("Name= ", typ.Bezeichnung) |
|
log.Info("creating new Typ", typ.Bezeichnung) |
|
res, err := CreateTyp(typ) |
|
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 deleteTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
typId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
log.Info("Deleting Instance with ID", typId) |
|
_, err := DeleteTyp(uint(typId)) |
|
if err != nil { |
|
log.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
res, err := ShowTyp() |
|
if err != nil { |
|
log.Fatal("Error: Typ 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 getTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
typId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
res, err := GetTyp(uint(typId)) |
|
if err != nil { |
|
log.Fatal("Error: Typ 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 updateTypHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
body, err := io.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var typ Typ |
|
json.Unmarshal(body, &typ) |
|
log.Println("Name = ", typ.Bezeichnung) |
|
log.Info("updating Typ", typ.Bezeichnung) |
|
res, err := UpdateTyp(typ) |
|
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) |
|
}
|
|
|