first commit
This commit is contained in:
88
cmd/api/api.go
Normal file
88
cmd/api/api.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/rs/cors"
|
||||
httpSwagger "github.com/swaggo/http-swagger/v2"
|
||||
"sismedika.com/sas/westone/services/auth"
|
||||
"sismedika.com/sas/westone/services/doctor"
|
||||
"sismedika.com/sas/westone/services/error_log"
|
||||
"sismedika.com/sas/westone/services/patient"
|
||||
"sismedika.com/sas/westone/services/person"
|
||||
"sismedika.com/sas/westone/services/terminology"
|
||||
"sismedika.com/sas/westone/services/user"
|
||||
)
|
||||
|
||||
type APIServer struct {
|
||||
addr string
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewAPIServer(addr string, db *sqlx.DB) *APIServer {
|
||||
return &APIServer{
|
||||
addr: addr,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *APIServer) Run() error {
|
||||
router := mux.NewRouter()
|
||||
subrouter := router.PathPrefix("/westone/api/v1").Subrouter()
|
||||
|
||||
subrouter.PathPrefix("/swagger/").Handler(httpSwagger.Handler())
|
||||
|
||||
// Error Log
|
||||
errorLogStore := error_log.NewStore(s.db)
|
||||
|
||||
// terminology
|
||||
terminologyStore := terminology.NewStore(s.db)
|
||||
terminologyHandler := terminology.NewHandler(terminologyStore)
|
||||
terminologyHandler.RegisterRoutes(subrouter)
|
||||
|
||||
// user
|
||||
userStore := user.NewStore(s.db)
|
||||
|
||||
// auth
|
||||
oauthStore := auth.NewStore(s.db)
|
||||
oauthHandler := auth.NewHandler(oauthStore, errorLogStore, userStore)
|
||||
oauthHandler.RegisterRoutes(subrouter)
|
||||
|
||||
// md person
|
||||
personStore := person.NewStore(s.db)
|
||||
personHandler := person.NewHandler(personStore, terminologyStore)
|
||||
personHandler.RegisterRoutes(subrouter)
|
||||
|
||||
// md patient
|
||||
patientStore := patient.NewStore(s.db)
|
||||
patientHandler := patient.NewHandler(patientStore)
|
||||
patientHandler.RegisterRoutes(subrouter)
|
||||
|
||||
// md doctor
|
||||
doctorStore := doctor.NewStore(s.db)
|
||||
doctorHandler := doctor.NewHandler(doctorStore)
|
||||
doctorHandler.RegisterRoutes(subrouter)
|
||||
|
||||
// userHandler := user.NewHandler(userStore, errorLogStore)
|
||||
// userHandler.RegisterRoutes(subrouter)
|
||||
|
||||
// productStore := product.NewStore(s.db)
|
||||
// productHandler := product.NewHandler(productStore, userStore)
|
||||
// productHandler.RegisterRoutes(subrouter)
|
||||
|
||||
// orderStore := order.NewStore(s.db)
|
||||
|
||||
// cartHandler := cart.NewHandler(productStore, orderStore, userStore)
|
||||
// cartHandler.RegisterRoutes(subrouter)
|
||||
|
||||
// Serve static files
|
||||
router.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))
|
||||
|
||||
log.Println("Listening on", s.addr)
|
||||
|
||||
corsHandler := cors.Default().Handler(router)
|
||||
return http.ListenAndServe(s.addr, corsHandler)
|
||||
}
|
||||
54
cmd/main.go
Normal file
54
cmd/main.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"sismedika.com/sas/westone/cmd/api"
|
||||
"sismedika.com/sas/westone/configs"
|
||||
"sismedika.com/sas/westone/db"
|
||||
_ "sismedika.com/sas/westone/docs"
|
||||
"sismedika.com/sas/westone/services/auth"
|
||||
)
|
||||
|
||||
// @title WESTONE API
|
||||
// @version 1.0
|
||||
// @description westone api documentation test
|
||||
// @termsOfService http://swagger.io/terms/
|
||||
// @license.name Apache 2.0
|
||||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
func main() {
|
||||
cfg := mysql.Config{
|
||||
User: configs.Envs.DBUser,
|
||||
Passwd: configs.Envs.DBPassword,
|
||||
Addr: configs.Envs.DBAddress,
|
||||
DBName: configs.Envs.DBName,
|
||||
Net: "tcp",
|
||||
AllowNativePasswords: true,
|
||||
ParseTime: true,
|
||||
}
|
||||
|
||||
db, err := db.NewMySQLStorage(cfg)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
auth.NewOAuth()
|
||||
initStorage(db)
|
||||
|
||||
server := api.NewAPIServer(fmt.Sprintf(":%s", configs.Envs.Port), db)
|
||||
if err := server.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func initStorage(db *sqlx.DB) {
|
||||
err := db.Ping()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("DB: Successfully connected!")
|
||||
}
|
||||
61
cmd/migrate/main.go
Normal file
61
cmd/migrate/main.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql" // mysql driver
|
||||
mysqlDriver "github.com/go-sql-driver/mysql"
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
mysqlMigrate "github.com/golang-migrate/migrate/v4/database/mysql"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"sismedika.com/sas/westone/configs"
|
||||
"sismedika.com/sas/westone/db"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := mysqlDriver.Config{
|
||||
User: configs.Envs.DBUser,
|
||||
Passwd: configs.Envs.DBPassword,
|
||||
Addr: configs.Envs.DBAddress,
|
||||
DBName: configs.Envs.DBName,
|
||||
Net: "tcp",
|
||||
AllowNativePasswords: true,
|
||||
ParseTime: true,
|
||||
}
|
||||
|
||||
db, err := db.NewMySQLStorage(cfg)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
driver, err := mysqlMigrate.WithInstance(db.DB, &mysqlMigrate.Config{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
m, err := migrate.NewWithDatabaseInstance(
|
||||
"file://cmd/migrate/migrations",
|
||||
"mysql",
|
||||
driver,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
v, d, _ := m.Version()
|
||||
log.Printf("Version: %d, dirty: %v", v, d)
|
||||
|
||||
cmd := os.Args[len(os.Args)-1]
|
||||
if cmd == "up" {
|
||||
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
if cmd == "down" {
|
||||
if err := m.Down(); err != nil && err != migrate.ErrNoChange {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS users;
|
||||
11
cmd/migrate/migrations/20240318192427_add-user-table.up.sql
Normal file
11
cmd/migrate/migrations/20240318192427_add-user-table.up.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`firstName` VARCHAR(255) NOT NULL,
|
||||
`lastName` VARCHAR(255) NOT NULL,
|
||||
`email` VARCHAR(255) NOT NULL,
|
||||
`password` VARCHAR(255) NOT NULL,
|
||||
`createdAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY (email)
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS products;
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(255) NOT NULL,
|
||||
`description` TEXT NOT NULL,
|
||||
`image` VARCHAR(255) NOT NULL,
|
||||
`price` DECIMAL(10, 2) NOT NULL,
|
||||
`quantity` INT UNSIGNED NOT NULL,
|
||||
`createdAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS orders;
|
||||
@@ -0,0 +1,12 @@
|
||||
CREATE TABLE IF NOT EXISTS orders (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`userId` INT UNSIGNED NOT NULL,
|
||||
`total` DECIMAL(10, 2) NOT NULL,
|
||||
`status` ENUM('pending', 'completed', 'cancelled') NOT NULL DEFAULT 'pending',
|
||||
`address` TEXT NOT NULL,
|
||||
`createdAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
FOREIGN KEY (`userId`) REFERENCES users(`id`)
|
||||
);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS order_items;
|
||||
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE IF NOT EXISTS `order_items` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`orderId` INT UNSIGNED NOT NULL,
|
||||
`productId` INT UNSIGNED NOT NULL,
|
||||
`quantity` INT NOT NULL,
|
||||
`price` DECIMAL(10, 2) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
FOREIGN KEY (`orderId`) REFERENCES orders(`id`),
|
||||
FOREIGN KEY (`productId`) REFERENCES products(`id`)
|
||||
);
|
||||
Reference in New Issue
Block a user