init project

This commit is contained in:
2025-03-19 13:47:29 +07:00
parent 80d1f95f66
commit f6a2cf3a5b
22 changed files with 677 additions and 24 deletions

View File

@@ -9,7 +9,7 @@ import (
"syscall"
"time"
"blueprint_fix/internal/server"
"template_blueprint/internal/server"
)
func gracefulShutdown(apiServer *http.Server, done chan bool) {

View File

@@ -0,0 +1,30 @@
/*! tailwindcss v4.0.13 | MIT License | https://tailwindcss.com */
.container {
width: 100%;
}
.mx-auto {
margin-inline: auto;
}
.block {
display: block;
}
.h-screen {
height: 100vh;
}
.resize {
resize: both;
}
.border {
border-style: var(--tw-border-style);
border-width: 1px;
}
.transition {
transition-property: color, background-color, border-color, outline-color, text-decoration-color, fill, stroke, --tw-gradient-from, --tw-gradient-via, --tw-gradient-to, opacity, box-shadow, transform, translate, scale, rotate, filter, -webkit-backdrop-filter, backdrop-filter;
transition-timing-function: var(--tw-ease, ease);
transition-duration: var(--tw-duration, 0s);
}
@property --tw-border-style {
syntax: "*";
inherits: false;
initial-value: solid;
}

View File

@@ -8,9 +8,9 @@ services:
- 8803:8803
environment:
TZ: Asia/Jakarta
BLUEPRINT_DB_HOST: 10.10.123.206
BLUEPRINT_DB_PORT: 27017
BLUEPRINT_DB_USER: admin
BLUEPRINT_DB_PASS: stim*rs54
BLUEPRINT_DB_MASTER: master
BLUEPRINT_DB_LOCAL: local
MONGODB_DEV_HOST: 10.10.123.206
MONGODB_DEV_PORT: 27017
MONGODB_DEV_USER: admin
MONGODB_DEV_PASS: stim*rs54
MONGODB_DEV_MASTER: master
MONGODB_DEV_LOCAL: local

5
go.mod
View File

@@ -1,4 +1,4 @@
module blueprint_fix
module template_blueprint
go 1.23.1
@@ -6,10 +6,10 @@ require (
github.com/a-h/templ v0.3.833
github.com/coder/websocket v1.8.12
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/testcontainers/testcontainers-go/modules/mongodb v0.35.0
go.mongodb.org/mongo-driver v1.17.3
nhooyr.io/websocket v1.8.17
)
require (
@@ -41,7 +41,6 @@ require (
github.com/goccy/go-json v0.10.5 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect

2
go.sum
View File

@@ -280,6 +280,4 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y=
nhooyr.io/websocket v1.8.17/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=

View File

@@ -23,10 +23,10 @@ type service struct {
}
var (
host = os.Getenv("BLUEPRINT_DB_HOST")
port = os.Getenv("BLUEPRINT_DB_PORT")
user = os.Getenv("BLUEPRINT_DB_USER")
pass = os.Getenv("BLUEPRINT_DB_PASS")
host = os.Getenv("MONGODB_DEV_HOST")
port = os.Getenv("MONGODB_DEV_PORT")
user = os.Getenv("MONGODB_DEV_USER")
pass = os.Getenv("MONGODB_DEV_PASS")
)
func New(database string) Service {

View File

@@ -9,11 +9,13 @@ import (
"github.com/gin-gonic/gin"
"blueprint_fix/cmd/web"
"github.com/a-h/templ"
"io/fs"
"template_blueprint/cmd/web"
"github.com/coder/websocket"
patientHandler "template_blueprint/pkg/handlers/patient"
)
func (s *Server) RegisterRoutes() http.Handler {
@@ -21,8 +23,6 @@ func (s *Server) RegisterRoutes() http.Handler {
r.GET("/", s.HelloWorldHandler)
r.GET("/health", s.healthHandler)
r.GET("/websocket", s.websocketHandler)
staticFiles, _ := fs.Sub(web.Files, "assets")
@@ -36,6 +36,13 @@ func (s *Server) RegisterRoutes() http.Handler {
web.HelloWebHandler(c.Writer, c.Request)
})
api := r.Group("/api")
patient := api.Group("/patient")
{
patient.POST("/insertpatient", patientHandler.InsertPatient)
patient.GET("/getallpatient", patientHandler.GetAllPatient)
}
return r
}
@@ -46,10 +53,6 @@ func (s *Server) HelloWorldHandler(c *gin.Context) {
c.JSON(http.StatusOK, resp)
}
func (s *Server) healthHandler(c *gin.Context) {
c.JSON(http.StatusOK, s.db.Health())
}
func (s *Server) websocketHandler(c *gin.Context) {
w := c.Writer
r := c.Request

View File

@@ -9,7 +9,7 @@ import (
_ "github.com/joho/godotenv/autoload"
"blueprint_fix/internal/database"
"template_blueprint/internal/database"
)
type Server struct {

View File

@@ -0,0 +1,13 @@
package mongo
import (
"go.mongodb.org/mongo-driver/mongo"
)
type DatabaseService struct {
DB *mongo.Database
}
func NewDatabaseService(db *mongo.Database) *DatabaseService {
return &DatabaseService{DB: db}
}

View File

@@ -0,0 +1,24 @@
package mongo
import (
"context"
"log"
"template_blueprint/pkg/models/master_data"
"time"
)
func (s *DatabaseService) InsertDataMaster(req master_data.ReqInsertData) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := s.DB.Collection(req.Table).InsertOne(ctx, master_data.ReqInsertDataMaster{
ID: req.ID,
System: req.System,
Code: req.Code,
Display: req.Display,
})
if err != nil {
log.Println(err)
return err
}
return nil
}

View File

@@ -0,0 +1,29 @@
package mongo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"log"
"template_blueprint/pkg/models/local"
"time"
)
func (s *DatabaseService) GetDataLog() ([]*local.StartUpLog, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
dataLog, err := s.DB.Collection("startup_log").Find(ctx, bson.M{})
if err != nil {
log.Println("MASUK SINI")
log.Println(err)
return nil, err
}
log.Println("Data", dataLog.Current)
var logs []*local.StartUpLog
errDecode := dataLog.All(ctx, &logs)
if errDecode != nil {
log.Println(errDecode)
return nil, errDecode
}
log.Println("LOGS :", logs)
return logs, nil
}

View File

@@ -0,0 +1,44 @@
package mongo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"log"
"template_blueprint/pkg/models/patient"
"time"
)
func (s *DatabaseService) InsertPatient(req *patient.Patient) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := s.DB.Collection("patient").InsertOne(ctx, patient.Patient{
ID: req.ID,
NoRM: req.NoRM,
Name: req.Name,
Telecom: req.Telecom,
Gender: req.Gender,
BirthDate: req.BirthDate,
Address: req.Address,
})
if err != nil {
log.Println(err)
return err
}
return nil
}
func (s *DatabaseService) GetAllDataPatient() ([]*patient.Patient, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
dataUser, err := s.DB.Collection("patient").Find(ctx, bson.D{})
if err != nil {
log.Println(err)
}
var users []*patient.Patient
err = dataUser.All(ctx, &users)
if err != nil {
log.Println(err)
return nil, err
}
return users, nil
}

View File

@@ -0,0 +1,88 @@
package mongo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"log"
"template_blueprint/pkg/models/user"
"time"
)
func (s *DatabaseService) InsertUser(req user.ReqInsertUser) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := s.DB.Collection("user").InsertOne(ctx, user.ReqInsertUser{
ID: req.ID,
Name: req.Name,
Age: req.Age,
Address: req.Address,
Gender: req.Gender,
Religion: req.Religion,
})
if err != nil {
log.Println(err)
return err
}
return nil
}
func (s *DatabaseService) GetAllDataUser() ([]*user.User, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
dataUser, err := s.DB.Collection("user").Find(ctx, bson.D{})
if err != nil {
log.Println(err)
}
var users []*user.User
err = dataUser.All(ctx, &users)
if err != nil {
log.Println(err)
return nil, err
}
return users, nil
}
func (s *DatabaseService) GetUserById(id string) (*user.User, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
dataUser := s.DB.Collection("user").FindOne(ctx, bson.D{{Key: "_id", Value: id}})
var user *user.User
err := dataUser.Decode(&user)
if err != nil {
log.Println(err)
return nil, err
}
return user, nil
}
func (s *DatabaseService) UpdateUser(reqUpdate *user.ReqUpdateUser) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
log.Println(reqUpdate.ID)
filter := bson.M{"_id": reqUpdate.ID}
update := bson.M{"$set": bson.M{
"name": reqUpdate.Name,
"age": reqUpdate.Age,
"address": reqUpdate.Address,
"gender": reqUpdate.Gender,
"religion": reqUpdate.Religion,
}}
updatedData, err := s.DB.Collection("user").UpdateOne(ctx, filter, update)
if err != nil {
log.Println(err)
return err
}
log.Println(updatedData)
return nil
}
func (s *DatabaseService) DeleteUserById(id string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := s.DB.Collection("user").DeleteOne(ctx, bson.D{{Key: "_id", Value: id}})
if err != nil {
log.Println(err)
return err
}
return nil
}

View File

@@ -0,0 +1,33 @@
package master_data
import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"net/http"
"os"
"template_blueprint/internal/database"
"template_blueprint/pkg/database/mongo"
"template_blueprint/pkg/models/master_data"
)
func InsertDataMaster(c *gin.Context) {
master := os.Getenv("BLUEPRINT_DB_MASTER")
var ReqInsertData master_data.ReqInsertData
errBind := c.Bind(&ReqInsertData)
if errBind != nil {
c.JSON(400, gin.H{
"code": 400,
})
}
db := database.New(master).GetDB()
mongoDB := mongo.NewDatabaseService(db)
ReqInsertData.ID = uuid.New().String()
errInsert := mongoDB.InsertDataMaster(ReqInsertData)
if errInsert != nil {
c.JSON(400, gin.H{
"message": "Failed Insert User",
})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Successfully Inserted User"})
}

View File

@@ -0,0 +1,42 @@
package mongo
import (
"context"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"log"
"net/http"
"os"
"template_blueprint/internal/database"
"template_blueprint/pkg/database/mongo"
"template_blueprint/pkg/models/local"
"time"
)
func GetDataLog(c *gin.Context) {
locals := os.Getenv("BLUEPRINT_DB_LOCAL")
db := database.New(locals).GetDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseService(db)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cursor, err := mongoDB.DB.Collection("startup_log").Find(ctx, bson.M{})
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database query failed"})
return
}
var dataLog []*local.StartUpLog
errDecode := cursor.All(ctx, &dataLog)
if errDecode != nil {
log.Println(errDecode)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database query failed"})
return
}
c.JSON(http.StatusOK, dataLog)
}

View File

@@ -0,0 +1,50 @@
package patient
import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"net/http"
"os"
"template_blueprint/internal/database"
"template_blueprint/pkg/database/mongo"
"template_blueprint/pkg/models/patient"
)
func InsertPatient(c *gin.Context) {
local := os.Getenv("MONGODB_DEV_LOCAL")
db := database.New(local).GetDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseService(db)
var reqInsert *patient.Patient
err := c.Bind(&reqInsert)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}
reqInsert.ID = uuid.New().String()
errInsert := mongoDB.InsertPatient(reqInsert)
if errInsert != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errInsert.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Patient successfully inserted"})
}
func GetAllPatient(c *gin.Context) {
local := os.Getenv("MONGODB_DEV_LOCAL")
db := database.New(local).GetDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseService(db)
dataPatient, errSelect := mongoDB.GetAllDataPatient()
if errSelect != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
return
}
c.JSON(http.StatusOK, dataPatient)
}

168
pkg/handlers/user/user.go Normal file
View File

@@ -0,0 +1,168 @@
package user
import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"log"
"net/http"
"os"
"template_blueprint/internal/database"
"template_blueprint/pkg/database/mongo"
"template_blueprint/pkg/models/user"
)
// InsertUser godoc
// @Summary Insert a new user
// @Description Adds a new user to the database
// @Tags users
// @Accept json
// @Produce json
// @Param request body user.ReqInsertUser true "User Data"
// @Success 200 {object} map[string]string "Successfully Inserted User"
// @Failure 400 {object} map[string]string "Bad Request"
// @Failure 500 {object} map[string]string "Database connection failed"
// @Router /api/localinsertuser [post]
func InsertUser(c *gin.Context) {
local := os.Getenv("BLUEPRINT_DB_LOCAL")
db := database.New(local).GetDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseService(db)
var reqInsert user.ReqInsertUser
errBind := c.Bind(&reqInsert)
if errBind != nil {
c.JSON(400, gin.H{
"message": errBind.Error(),
})
return
}
id := uuid.New().String()
reqInsert.ID = id
errInsert := mongoDB.InsertUser(reqInsert)
if errInsert != nil {
c.JSON(400, gin.H{
"message": errInsert.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Successfully Inserted User"})
}
// GetAllUser godoc
// @Summary Get all users
// @Description Retrieves all users from the database
// @Tags users
// @Produce json
// @Success 200 {array} user.User "List of users"
// @Failure 500 {object} map[string]string "Database connection failed"
// @Router /api/local/getalluser [get]
func GetAllUser(c *gin.Context) {
local := os.Getenv("BLUEPRINT_DB_LOCAL")
db := database.New(local).GetDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseService(db)
dataUser, errSelect := mongoDB.GetAllDataUser()
if errSelect != nil {
c.JSON(400, gin.H{
"message": errSelect.Error(),
})
return
}
c.JSON(http.StatusOK, dataUser)
}
// GetUserByID godoc
// @Summary Get user by ID
// @Description Retrieves a user by their ID
// @Tags users
// @Produce json
// @Param id path string true "User ID"
// @Success 200 {object} user.User "User data"
// @Failure 400 {object} map[string]string "Bad Request"
// @Router /api/local/getuser/{id} [get]
func GetUserByID(c *gin.Context) {
local := os.Getenv("BLUEPRINT_DB_LOCAL")
db := database.New(local).GetDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
id := c.Param("id")
log.Println("ID", id)
mongoDB := mongo.NewDatabaseService(db)
dataUser, errSelect := mongoDB.GetUserById(id)
if errSelect != nil {
c.JSON(400, gin.H{
"message": errSelect.Error(),
})
return
}
c.JSON(http.StatusOK, dataUser)
}
// UpdateUser godoc
// @Summary Update a user
// @Description Updates user information
// @Tags users
// @Accept json
// @Produce json
// @Param request body user.User true "User Data"
// @Success 200 {object} map[string]string "Successfully Updated User"
// @Failure 400 {object} map[string]string "Bad Request"
// @Failure 500 {object} map[string]string "Database connection failed"
// @Router /users [put]
func UpdateUser(c *gin.Context) {
local := os.Getenv("BLUEPRINT_DB_LOCAL")
db := database.New(local).GetDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseService(db)
var reqUpdate *user.ReqUpdateUser
errBind := c.Bind(&reqUpdate)
if errBind != nil {
c.JSON(400, gin.H{
"message": errBind.Error(),
})
return
}
log.Println("REQ UPDATE", reqUpdate)
errUpdate := mongoDB.UpdateUser(reqUpdate)
if errUpdate != nil {
c.JSON(400, gin.H{
"message": errUpdate.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Successfully Updated User"})
}
func DeleteUser(c *gin.Context) {
local := os.Getenv("BLUEPRINT_DB_LOCAL")
db := database.New(local).GetDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseService(db)
var reqDelete *user.ReqDeleteUser
errBind := c.Bind(&reqDelete)
if errBind != nil {
c.JSON(400, gin.H{})
return
}
log.Println("REQ DELETE", reqDelete)
errDelete := mongoDB.DeleteUserById(reqDelete.ID)
if errDelete != nil {
c.JSON(400, gin.H{"message": "Failed to Delete User"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Successfully Deleted User"})
}

72
pkg/models/local/local.go Normal file
View File

@@ -0,0 +1,72 @@
package local
type StartUpLog struct {
ID string `bson:"_id"`
HostName string `bson:"hostname"`
StartTime string `bson:"start_time"`
StartTimeLocal string `bson:"start_time_local"`
CmdLine CmdLine `bson:"cmdline"`
Pid int `bson:"pid"`
BuildInfo BuildInfo `bson:"buildinfo"`
}
type CmdLine struct {
Net Net `bson:"net"`
ProcessManagement ProcessManagement `bson:"processManagement"`
SystemLog SystemLog `bson:"systemLog"`
}
type Net struct {
BindIP string `bson:"bindIp"`
Port int `bson:"port"`
Tls TLS `bson:"tls"`
}
type TLS struct {
Mode string `bson:"mode"`
}
type ProcessManagement struct {
Fork bool `bson:"fork"`
PidFilePath string `bson:"pidFilePath"`
}
type SystemLog struct {
Destination string `bson:"destination"`
LogAppend bool `bson:"logAppend"`
Path string `bson:"path"`
}
type BuildInfo struct {
Version string `bson:"version"`
GitVersion string `bson:"gitVersion"`
Modules []string `bson:"modules"`
Allocator string `bson:"allocator"`
JavaScriptEngine string `bson:"javaScriptEngine"`
SysInfo string `bson:"sysInfo"`
VersionArray []int `bson:"versionArray"`
OpenSSL OpenSSL `bson:"openssl"`
BuildEnvironment BuildEnvironment `bson:"buildEnvironment"`
Bits int `bson:"bits"`
Debug bool `bson:"debug"`
MaxBsonObjectSize int `bson:"maxBsonObjectSize"`
StorageEngines []string `bson:"storageEngines"`
}
type OpenSSL struct {
Running string `bson:"running"`
Compiled string `bson:"compiled"`
}
type BuildEnvironment struct {
DistMod string `bson:"distmod"`
Distarch string `bson:"distarch"`
CC string `bson:"cc"`
CCFlags string `bson:"ccflags"`
CXX string `bson:"cxx"`
CXXFlags string `bson:"cxxflags"`
LinkFlags string `bson:"linkflags"`
TargetArch string `bson:"target_arch"`
TargetOS string `bson:"target_os"`
CPPDefines string `bson:"cppdefines"`
}

View File

@@ -0,0 +1,16 @@
package master_data
type ReqInsertData struct {
ID string `bson:"_id"`
Table string `bson:"table"`
System string `bson:"system"`
Code string `bson:"code"`
Display string `bson:"display"`
}
type ReqInsertDataMaster struct {
ID string `bson:"_id"`
System string `bson:"system"`
Code string `bson:"code"`
Display string `bson:"display"`
}

View File

@@ -0,0 +1,11 @@
package patient
type Patient struct {
ID string `bson:"_id"`
NoRM string `bson:"norm"`
Name string `bson:"name"`
Telecom string `bson:"telecom"`
Gender string `bson:"gender"`
BirthDate string `bson:"birthDate"`
Address string `bson:"address"`
}

33
pkg/models/user/user.go Normal file
View File

@@ -0,0 +1,33 @@
package user
type User struct {
ID string `bson:"_id"`
Name string `bson:"name"`
Age int `bson:"age"`
Address string `bson:"address"`
Gender string `bson:"gender"`
Religion string `bson:"religion"`
}
type ReqInsertUser struct {
ID string `bson:"_id"`
Name string `bson:"name"`
Age int `bson:"age"`
Address string `bson:"address"`
Gender string `bson:"gender"`
Religion string `bson:"religion"`
}
// USING INSERT AND UPDATE
type ReqUpdateUser struct {
ID string `json:"_id"`
Name string `json:"name"`
Age int `json:"age"`
Address string `json:"address"`
Gender string `json:"gender"`
Religion string `json:"religion"`
}
type ReqDeleteUser struct {
ID string `json:"_id"`
}

BIN
tailwindcss Normal file

Binary file not shown.