wertet die rtcinfo files aus
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.

176 lines
4.6 KiB

2 years ago
package main
import (
2 years ago
"encoding/json"
2 years ago
"fmt"
"io/ioutil"
2 years ago
"log"
2 years ago
"net/http"
2 years ago
"os"
2 years ago
"strconv"
2 years ago
2 years ago
"gitlab.dedyn.io/AUDIAG/rtcusertr/database"
"gitlab.dedyn.io/AUDIAG/rtcusertr/domain"
2 years ago
"github.com/gorilla/mux"
2 years ago
"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() {
2 years ago
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)
2 years ago
res, err := database.CreateKnoten(knoten)
2 years ago
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)
}
2 years ago
func deleteKnotenEHandler(writer http.ResponseWriter, req *http.Request) {
2 years ago
writer.Header().Set("Access-Control-Allow-Origin", "*")
if req.Method == http.MethodOptions {
return
}
2 years ago
knotenId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0)
logrus.Info("Deleting tool with ID", knotenId)
_, err := database.DeleteKnoten(uint(knotenId))
2 years ago
if err != nil {
2 years ago
logrus.Fatal("Error: Delete could not be executed", err)
2 years ago
}
writer.WriteHeader(http.StatusNoContent)
}
2 years ago
func showKnotenEHandler(writer http.ResponseWriter, req *http.Request) {
2 years ago
writer.Header().Set("Access-Control-Allow-Origin", "*")
if req.Method == http.MethodOptions {
return
}
2 years ago
res, err := database.ShowKnoten()
2 years ago
if err != nil {
2 years ago
logrus.Fatal("Error: Knoteneintrag can't be shown ", err)
2 years ago
writer.WriteHeader(http.StatusInternalServerError)
}
b, err := json.Marshal(res)
if err != nil {
2 years ago
logrus.Fatal("Error: ", err)
2 years ago
}
location := req.URL.String()
writer.Header().Set("Location:", location)
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
writer.Write(b)
}
2 years ago
func getKnotenEHandler(writer http.ResponseWriter, req *http.Request) {
2 years ago
writer.Header().Set("Access-Control-Allow-Origin", "*")
if req.Method == http.MethodOptions {
return
}
2 years ago
knotenId, _ := strconv.ParseUint(mux.Vars(req)["id"], 10, 0)
res, err := database.GetKnoten(uint(knotenId))
2 years ago
if err != nil {
2 years ago
logrus.Fatal("Error: Knoteneintrag can't be shown ", err)
2 years ago
writer.WriteHeader(http.StatusInternalServerError)
}
b, err := json.Marshal(res)
if err != nil {
2 years ago
logrus.Fatal("Error: ", err)
2 years ago
}
location := req.URL.String()
writer.Header().Set("Location:", location)
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
writer.Write(b)
}
2 years ago
func updateKnotenEHandler(writer http.ResponseWriter, req *http.Request) {
2 years ago
writer.Header().Set("Access-Control-Allow-Origin", "*")
if req.Method == http.MethodOptions {
return
2 years ago
}
2 years ago
body, err := ioutil.ReadAll(req.Body)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
}
2 years ago
var knoten domain.KnotenE
json.Unmarshal(body, &knoten)
logrus.Println("Name = ", knoten.Fachbereich)
logrus.Info("updating Knoteneintrag", knoten.Fachbereich)
res, err := database.UpdateKnoten(knoten)
2 years ago
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)
2 years ago
}