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.
175 lines
4.6 KiB
175 lines
4.6 KiB
package main |
|
|
|
import ( |
|
"encoding/json" |
|
"fmt" |
|
"io/ioutil" |
|
"log" |
|
"net/http" |
|
"os" |
|
"strconv" |
|
|
|
"gitlab.dedyn.io/AUDIAG/rtcusertr/database" |
|
"gitlab.dedyn.io/AUDIAG/rtcusertr/domain" |
|
|
|
"github.com/gorilla/mux" |
|
"github.com/sirupsen/logrus" |
|
) |
|
|
|
const MaxBT = 4 |
|
const MaxED = 186 |
|
const MinED = 100 |
|
const MaxEK = 85 |
|
const MaxGQ = 32 |
|
const MaxPG = 200 |
|
|
|
type Master struct { |
|
} |
|
|
|
func main() { |
|
|
|
fmt.Println("This is rtcusertrd!!!") |
|
logFile, err := os.OpenFile("rtcusertr.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) |
|
if err != nil { |
|
logrus.Fatal(err) |
|
} |
|
|
|
defer logFile.Close() |
|
|
|
logrus.SetOutput(logFile) |
|
logrus.SetLevel(logrus.InfoLevel) |
|
r := mux.NewRouter() |
|
|
|
r.HandleFunc("/api/kn", createKnotenEHandler).Methods("POST", "OPTIONS") |
|
r.HandleFunc("/api/kn/{id:[0-9]+}", deleteKnotenEHandler).Methods("DELETE", "OPTIONS") |
|
r.HandleFunc("/api/kn", showKnotenEHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/kn/{id:[0-9]+}", getKnotenEHandler).Methods("GET", "OPTIONS") |
|
r.HandleFunc("/api/kn/{id:[0-9]+}", updateKnotenEHandler).Methods("PUT", "OPTIONS") |
|
|
|
http.Handle("/", r) |
|
r.Use(mux.CORSMethodMiddleware(r)) |
|
http.ListenAndServe(":8081", r) |
|
logrus.Info("running on localhost, Port 8081") |
|
|
|
} |
|
|
|
func createKnotenEHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var knoten domain.KnotenE |
|
json.Unmarshal(body, &knoten) |
|
logrus.Println("Name= ", knoten.Fachbereich) |
|
logrus.Info("creating new Knoten-Eintrag", knoten.Fachbereich) |
|
res, err := database.CreateKnoten(knoten) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
logrus.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 deleteKnotenEHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
knotenId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
logrus.Info("Deleting tool with ID", knotenId) |
|
_, err := database.DeleteKnoten(uint(knotenId)) |
|
if err != nil { |
|
logrus.Fatal("Error: Delete could not be executed", err) |
|
} |
|
writer.WriteHeader(http.StatusNoContent) |
|
} |
|
|
|
func showKnotenEHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
res, err := database.ShowKnoten() |
|
if err != nil { |
|
logrus.Fatal("Error: Knoteneintrag can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
logrus.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 getKnotenEHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
knotenId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0) |
|
res, err := database.GetKnoten(uint(knotenId)) |
|
if err != nil { |
|
logrus.Fatal("Error: Knoteneintrag can't be shown ", err) |
|
writer.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
b, err := json.Marshal(res) |
|
if err != nil { |
|
logrus.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 updateKnotenEHandler(writer http.ResponseWriter, req *http.Request) { |
|
writer.Header().Set("Access-Control-Allow-Origin", "*") |
|
if req.Method == http.MethodOptions { |
|
return |
|
} |
|
body, err := ioutil.ReadAll(req.Body) |
|
if err != nil { |
|
writer.WriteHeader(http.StatusBadRequest) |
|
} |
|
var knoten domain.KnotenE |
|
json.Unmarshal(body, &knoten) |
|
logrus.Println("Name = ", knoten.Fachbereich) |
|
logrus.Info("updating Knoteneintrag", knoten.Fachbereich) |
|
res, err := database.UpdateKnoten(knoten) |
|
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) |
|
}
|
|
|