2 Commits

Author SHA1 Message Date
tom
8a71f9fffe ADD: Jenkinsfile 2024-09-08 15:58:37 +02:00
tom
dd2fb9a79f CHANGE: added Cors 2024-09-07 08:54:52 +02:00
5 changed files with 122 additions and 21 deletions

View File

@@ -29,23 +29,26 @@ func main() {
}
func timeCreateHandler(w http.ResponseWriter, r *http.Request) {
setCors(w)
switch r.Method {
case "PUT":
createBooking(w, r)
case "OPTIONS":
w.WriteHeader(http.StatusOK)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func timeHandler(w http.ResponseWriter, r *http.Request) {
if os.Getenv("DEBUG") == "true" {
w.Header().Set("Access-Control-Allow-Origin", "*")
}
setCors(w)
switch r.Method {
case "GET":
getBookings(w, r)
case "PUT":
updateBooking(w, r)
case "OPTIONS":
w.WriteHeader(http.StatusOK)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
@@ -65,6 +68,7 @@ func createBooking(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(booking)
}
@@ -98,29 +102,30 @@ func updateBooking(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid bookingID query parameter", http.StatusBadRequest)
return
}
booking, err := (*models.Booking).GetBookingById(nil, booking_id)
_booking, err := (*models.Booking).GetBookingById(nil, booking_id)
if err != nil {
log.Println("Error getting booking: ", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
if card_id := r.URL.Query().Get("cardID"); card_id != "" {
booking.CardID = card_id
}
if reader_id := r.URL.Query().Get("readerID"); reader_id != "" {
booking.ReaderID = reader_id
}
if _booking_type := r.URL.Query().Get("bookingType"); _booking_type != "" {
booking_type, err := strconv.Atoi(_booking_type)
var booking models.Booking
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&booking)
if err != nil {
http.Error(w, "Invalid booking_type query parameter", http.StatusInternalServerError)
log.Println("Error parsing booking: ", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
booking.BookingType = booking_type
if booking.Id != 0 && booking.Id != _booking.Id {
log.Println("Booking Ids do not match")
http.Error(w, "Booking Ids do not match", http.StatusBadRequest)
return
}
booking.Save()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(booking)
_booking.Update(booking)
_booking.Save()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(_booking)
}
// func getBooking(w http.ResponseWriter, r *http.Request)
@@ -131,3 +136,11 @@ func getEnv(key, fallback string) string {
}
return fallback
}
func setCors(w http.ResponseWriter) {
if os.Getenv("DEBUG") == "true" {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
}
}

View File

@@ -16,9 +16,9 @@ func (e SameBookingError) Error() string {
}
type Booking struct {
CardID string `json:"cradID"`
CardID string `json:"cardID"`
ReaderID string `json:"readerID"`
BookingType int `json:"bookingTyp"`
BookingType int `json:"bookingType"`
LoggedTime time.Time `json:"loggedTime"`
Id int `json:"id"`
}
@@ -123,6 +123,18 @@ func (b Booking) Save() {
}
}
func (b *Booking) Update(nb Booking) {
if b.BookingType != nb.BookingType && nb.BookingType != 0 {
b.BookingType = nb.BookingType
}
if b.CardID != nb.CardID && nb.CardID != "" {
b.CardID = nb.CardID
}
if b.ReaderID != nb.ReaderID && nb.ReaderID != "" {
b.ReaderID = nb.ReaderID
}
}
func checkLastBooking(b Booking) bool {
var booking_type int
stmt, err := DB.Prepare((`SELECT booking_type FROM "zeiten" WHERE "card_id" = $1 ORDER BY "logged_time" DESC LIMIT 1;`))

37
Jenkinsfile vendored Normal file
View File

@@ -0,0 +1,37 @@
pipeline {
environment {
DOCKER_USERNAME = 'tom'
DOCKER_PASSWORD = credentials('dgitea_tom')
}
agent any
stages {
stage ("Building image arbeitszeit-backend"){
when {
anyOf{
changeset 'Jenkinsfile'
changeset 'Makefile'
changeset 'Backend/**'
}
}
steps {
sh 'make build_backend'
}
}
stage('Deploying image arbeitszeit-backend') {
when {
anyOf{
changeset 'Jenkinsfile'
changeset 'Makefile'
changeset 'Backend/**'
}
}
steps {
sh 'make push_backend'
}
}
}
}

38
Makefile Normal file
View File

@@ -0,0 +1,38 @@
DOCKER_USERNAME ?= tom
DOCKER_PASSWORD ?= $(shell echo "YOUR_DEFAULT_PASSWORD")
IMAGE_REGISTRY ?= git.letsstein.de
APPLICATION_NAME ?= arbeitszeit
_BUILD_ARGS_APP_PART ?= .
_BUILD_ARGS_TAG ?= latest
TAG ?= latest
GIT_COMMIT ?= $(shell git rev-parse --short HEAD)
capitalize = $(shell echo $(1) | sed 's/.*/\u&/')
login_registry:
ifdef JENKINS_HOME
echo "Logging in to Docker registry..."
echo ${DOCKER_PASSWORD} | docker login -u $(DOCKER_USERNAME) --password-stdin $(IMAGE_REGISTRY)
endif
_builder:
docker build --tag ${IMAGE_REGISTRY}/${DOCKER_USERNAME}/${APPLICATION_NAME}-${_BUILD_ARGS_APP_PART}:${GIT_COMMIT} -f $(call capitalize, ${_BUILD_ARGS_APP_PART})/Dockerfile $(call capitalize, ${_BUILD_ARGS_APP_PART})
_pusher: login_registry
docker push ${IMAGE_REGISTRY}/${DOCKER_USERNAME}/${APPLICATION_NAME}-${_BUILD_ARGS_APP_PART}:${GIT_COMMIT}
docker tag ${IMAGE_REGISTRY}/${DOCKER_USERNAME}/${APPLICATION_NAME}-${_BUILD_ARGS_APP_PART}:${GIT_COMMIT} ${IMAGE_REGISTRY}/${DOCKER_USERNAME}/${APPLICATION_NAME}-${_BUILD_ARGS_APP_PART}:${_BUILD_ARGS_TAG}
docker push ${IMAGE_REGISTRY}/${DOCKER_USERNAME}/${APPLICATION_NAME}-${_BUILD_ARGS_APP_PART}:${_BUILD_ARGS_TAG}
build_%:
$(MAKE) _builder \
-e _BUILD_ARGS_APP_PART="$*" \
push_%:
$(MAKE) _pusher \
-e _BUILD_ARGS_APP_PART="$*" \
-e _BUILD_ARGS_TAG="${TAG}"
full_%:
make build_$*
make push_$*

View File

@@ -14,6 +14,7 @@ docker compose up -d
## API
Nutzung der API
wenn die `dev-docker-compose.yml` Datei gestartet wird, ist direkt ein SwaggerUI Server mit entsprechender Datei inbegriffen.
### Buchungen [/time]