Refraktoryzacja kodu. Dodano połączenie z bazą danych.

This commit is contained in:
Marcel 2018-12-04 19:15:41 +01:00
parent 8b9daa4f2c
commit 34df1ce682
4 changed files with 95 additions and 29 deletions

30
databaseConnection.go Normal file
View File

@ -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..")
}

27
handlers.go Normal file
View File

@ -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)
}

View File

@ -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"}

33
routes.go Normal file
View File

@ -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
}