From 34df1ce682c4dc3aadba2a5980fa101bfb90feae Mon Sep 17 00:00:00 2001 From: Marcel Date: Tue, 4 Dec 2018 19:15:41 +0100 Subject: [PATCH] =?UTF-8?q?Refraktoryzacja=20kodu.=20Dodano=20po=C5=82?= =?UTF-8?q?=C4=85czenie=20z=20baz=C4=85=20danych.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- databaseConnection.go | 30 ++++++++++++++++++++++++++++++ handlers.go | 27 +++++++++++++++++++++++++++ index.go | 34 +++++----------------------------- routes.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 29 deletions(-) create mode 100644 databaseConnection.go create mode 100644 handlers.go create mode 100644 routes.go diff --git a/databaseConnection.go b/databaseConnection.go new file mode 100644 index 0000000..d01c3e4 --- /dev/null +++ b/databaseConnection.go @@ -0,0 +1,30 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + + _ "github.com/denisenkom/go-mssqldb" +) + +var connectionString = "server=192.168.1.3;Port=62262;database=DB_s439397;trusted_connection=yes;encrypt=disable" + +//var connectionString = "server=MARCEL\\SQLEXPRESS;user id=;password=" +var db *sql.DB +var err error + +func testConnection() { + db, err = sql.Open("sqlserver", connectionString) + if err != nil { + log.Fatal("Error creating connection pool: ", err.Error()) + } + //ctx := context.Background() + err = db.Ping() + //err = db.PingContext(ctx) + if err != nil { + log.Fatal(err.Error()) + } + fmt.Printf("Connected!\n") + fmt.Println("..I..") +} diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..3efb330 --- /dev/null +++ b/handlers.go @@ -0,0 +1,27 @@ +package main + +import ( + "encoding/json" + "fmt" + "html" + "net/http" + "time" + + "github.com/gorilla/mux" +) + +func index(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) +} +func getAll(w http.ResponseWriter, r *http.Request) { + patientsList := patients{ + patient{"kss", "dasda", "asdasd a", time.Now(), patientStates(critical), sex(m), "xxx@yyy.zz"}, + patient{"00112245789", "Adam", "Marcel", time.Now(), patientStates(stable), sex(k), "xxxx@yyy.zz"}, + } + json.NewEncoder(w).Encode(patientsList) +} +func getIndex(w http.ResponseWriter, r *http.Request) { + args := mux.Vars(r) + pk := args["primaryKey"] + fmt.Fprintf(w, "Tutaj bedzie wynik dla PK = %s", pk) +} diff --git a/index.go b/index.go index f828dc5..e02dd1d 100644 --- a/index.go +++ b/index.go @@ -1,50 +1,26 @@ package main import ( - "encoding/json" - "fmt" - "html" "log" "net/http" "time" - - "github.com/gorilla/mux" ) // Zrobione ostatnio: -// Podstawowe struktury modeli; niezbędne enumy; +// prosta refraktoryzacja; // Zrobione: -// Podstawowy serwer; +// Podstawowy serwer; Podstawowe struktury modeli; niezbędne enumy; // // Do zrobienia: // Funkcje modeli; Interfejsy json; łączenie z bazą danych; Rozne metody do tworzenia komend sql; // Komunikacja z aplikacja desktopowa; Walidacja danych!! ; func main() { - xx := getXxx() - xx.Pesel = "ssss" - muxRouter := mux.NewRouter().StrictSlash(true) - muxRouter.HandleFunc("/", index) - muxRouter.HandleFunc("/getfromdb", getAll) - muxRouter.HandleFunc("/getfromdb/{primaryKey}", getIndex) - log.Fatal(http.ListenAndServe(":8080", muxRouter)) + router := newRouter() + testConnection() + log.Fatal(http.ListenAndServe(":8080", router)) } -func index(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) -} -func getAll(w http.ResponseWriter, r *http.Request) { - patientsList := patients{ - patient{"kss", "dasda", "asdasd a", time.Now(), patientStates(critical), sex(m), "xxx@yyy.zz"}, - patient{"00112245789", "Adam", "Marcel", time.Now(), patientStates(stable), sex(k), "xxxx@yyy.zz"}, - } - json.NewEncoder(w).Encode(patientsList) -} -func getIndex(w http.ResponseWriter, r *http.Request) { - args := mux.Vars(r) - pk := args["primaryKey"] - fmt.Fprintf(w, "Tutaj bedzie wynik dla PK = %s", pk) -} func getXxx() patient { pat := patient{"kss", "dasda", "asdasd a", time.Now(), patientStates(critical), sex(m), "xxx@yyy.zz"} diff --git a/routes.go b/routes.go new file mode 100644 index 0000000..08bfeed --- /dev/null +++ b/routes.go @@ -0,0 +1,33 @@ +package main + +import ( + "net/http" + + "github.com/gorilla/mux" +) + +type route struct { + Name string + Adres string + HandlerFunction http.HandlerFunc +} + +type routes []route + +var registredRoutes = routes{ + route{"Index", "/", index}, + route{"GetAll", "/getfromdb", getAll}, + route{"GetOne", "/getfromdb/{primaryKey}", getIndex}, +} + +func newRouter() *mux.Router { + + router := mux.NewRouter().StrictSlash(true) + for _, r := range registredRoutes { + router. + Path(r.Adres). + Name(r.Name). + Handler(r.HandlerFunction) + } + return router +}