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.

67 lines
1.5 KiB

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"spargcom/senuma/database"
"spargcom/senuma/domain"
log "github.com/sirupsen/logrus"
"github.com/gorilla/mux"
)
func main() {
file, err := os.OpenFile("senuma.log", os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
log.SetOutput(file)
log.SetLevel(log.InfoLevel)
r := mux.NewRouter()
r.HandleFunc("/artikel", createArtikelHandler).Methods("POST")
//r.HandleFunc("/groups", createGroupHandler).Methods("POST")
//r.HandleFunc("/users", createUserHandler).Methods("POST")
//r.HandleFunc("/accounts", createAccountHandler).Methods("POST")
//r.HandleFunc("/tunnels", createTunnelHandler).Methods("POST")
http.ListenAndServe(":8081", r)
log.Info("running on localhost, Port 8081")
}
func createArtikelHandler(writer http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
}
var art domain.Artikel
json.Unmarshal(body, &art)
log.Println("Bezeichnung = ", art.Bezeichnung)
log.Println("externe Art-Nr = ", art.ArtNrExt)
res, err := database.CreateArtikel(art)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
}
b, err := json.Marshal(res)
if err != nil {
log.Println("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)
}