Compare commits
2 Commits
b582e5d1c2
...
8a71f9fffe
| Author | SHA1 | Date | |
|---|---|---|---|
| 8a71f9fffe | |||
| dd2fb9a79f |
@@ -29,23 +29,26 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func timeCreateHandler(w http.ResponseWriter, r *http.Request) {
|
func timeCreateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
setCors(w)
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "PUT":
|
case "PUT":
|
||||||
createBooking(w, r)
|
createBooking(w, r)
|
||||||
|
case "OPTIONS":
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
default:
|
default:
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func timeHandler(w http.ResponseWriter, r *http.Request) {
|
func timeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if os.Getenv("DEBUG") == "true" {
|
setCors(w)
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
}
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
getBookings(w, r)
|
getBookings(w, r)
|
||||||
case "PUT":
|
case "PUT":
|
||||||
updateBooking(w, r)
|
updateBooking(w, r)
|
||||||
|
case "OPTIONS":
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
default:
|
default:
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
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)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
json.NewEncoder(w).Encode(booking)
|
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)
|
http.Error(w, "Invalid bookingID query parameter", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
booking, err := (*models.Booking).GetBookingById(nil, booking_id)
|
_booking, err := (*models.Booking).GetBookingById(nil, booking_id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error getting booking: ", err)
|
log.Println("Error getting booking: ", err)
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if card_id := r.URL.Query().Get("cardID"); card_id != "" {
|
var booking models.Booking
|
||||||
booking.CardID = card_id
|
dec := json.NewDecoder(r.Body)
|
||||||
|
dec.DisallowUnknownFields()
|
||||||
|
err = dec.Decode(&booking)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error parsing booking: ", err)
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if reader_id := r.URL.Query().Get("readerID"); reader_id != "" {
|
if booking.Id != 0 && booking.Id != _booking.Id {
|
||||||
booking.ReaderID = reader_id
|
log.Println("Booking Ids do not match")
|
||||||
|
http.Error(w, "Booking Ids do not match", http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if _booking_type := r.URL.Query().Get("bookingType"); _booking_type != "" {
|
_booking.Update(booking)
|
||||||
booking_type, err := strconv.Atoi(_booking_type)
|
_booking.Save()
|
||||||
if err != nil {
|
w.Header().Set("Content-Type", "application/json")
|
||||||
http.Error(w, "Invalid booking_type query parameter", http.StatusInternalServerError)
|
json.NewEncoder(w).Encode(_booking)
|
||||||
return
|
|
||||||
}
|
|
||||||
booking.BookingType = booking_type
|
|
||||||
}
|
|
||||||
booking.Save()
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(booking)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// func getBooking(w http.ResponseWriter, r *http.Request)
|
// func getBooking(w http.ResponseWriter, r *http.Request)
|
||||||
@@ -131,3 +136,11 @@ func getEnv(key, fallback string) string {
|
|||||||
}
|
}
|
||||||
return fallback
|
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", "*")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ func (e SameBookingError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Booking struct {
|
type Booking struct {
|
||||||
CardID string `json:"cradID"`
|
CardID string `json:"cardID"`
|
||||||
ReaderID string `json:"readerID"`
|
ReaderID string `json:"readerID"`
|
||||||
BookingType int `json:"bookingTyp"`
|
BookingType int `json:"bookingType"`
|
||||||
LoggedTime time.Time `json:"loggedTime"`
|
LoggedTime time.Time `json:"loggedTime"`
|
||||||
Id int `json:"id"`
|
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 {
|
func checkLastBooking(b Booking) bool {
|
||||||
var booking_type int
|
var booking_type int
|
||||||
stmt, err := DB.Prepare((`SELECT booking_type FROM "zeiten" WHERE "card_id" = $1 ORDER BY "logged_time" DESC LIMIT 1;`))
|
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
37
Jenkinsfile
vendored
Normal 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
38
Makefile
Normal 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_$*
|
||||||
Reference in New Issue
Block a user