116 lines
2.9 KiB
Go
116 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"runtime"
|
|
"time"
|
|
|
|
"git.nochill.in/nochill/excel_import_playground/internal"
|
|
"git.nochill.in/nochill/excel_import_playground/internal/repository"
|
|
"git.nochill.in/nochill/excel_import_playground/internal/rest"
|
|
"github.com/gorilla/mux"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func main() {
|
|
runtime.GOMAXPROCS(5)
|
|
var wait time.Duration
|
|
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
|
|
flag.Parse()
|
|
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
|
|
_ = os.Getenv("JWT_SALT")
|
|
DATABASE_URL := os.Getenv("DATABASE_URL")
|
|
APP_HOST := os.Getenv("APP_HOST")
|
|
|
|
poolConfig, err := pgxpool.ParseConfig(DATABASE_URL)
|
|
if err != nil {
|
|
log.Fatal("ENV DATABASE_URL NOT FOUND", err)
|
|
}
|
|
dbConn, err := pgxpool.NewWithConfig(context.Background(), poolConfig)
|
|
|
|
if err != nil {
|
|
log.Fatal("cannot connect to db: ", err)
|
|
}
|
|
|
|
store := repository.NewStore(dbConn)
|
|
|
|
server, err := rest.NewServer(store)
|
|
|
|
if err != nil {
|
|
log.Fatal("Somethng wrong while try to start Server", err)
|
|
}
|
|
|
|
r := mux.NewRouter()
|
|
r.Use(internal.AuthMiddleware)
|
|
r.HandleFunc("/", homeRoute).Methods("GET")
|
|
r.HandleFunc("/import-pasien", server.ImportPatientHandler).Methods("POST")
|
|
r.HandleFunc("/update-pasien/{id}", server.UpdatePatient).Methods("PATCH")
|
|
|
|
// r.HandleFunc("/foo", fooHandler).Methods(http.MethodGet, http.MethodPut, http.MethodPatch, http.MethodOptions)
|
|
// r.Use(mux.CORSMethodMiddleware(r))
|
|
|
|
// val := internal.FindValueByLabel("PNS")
|
|
|
|
// fmt.Println(val.ID)
|
|
|
|
srv := &http.Server{
|
|
Handler: r,
|
|
Addr: APP_HOST,
|
|
// Good practice: enforce timeouts for servers you create!
|
|
WriteTimeout: 15 * time.Second,
|
|
ReadTimeout: 15 * time.Second,
|
|
}
|
|
|
|
go func() {
|
|
host := fmt.Sprintf("Server running on %s", APP_HOST)
|
|
fmt.Println(host)
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
log.Println(err)
|
|
}
|
|
}()
|
|
|
|
c := make(chan os.Signal, 1)
|
|
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
|
|
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
// Block until we receive our signal.
|
|
<-c
|
|
|
|
// Create a deadline to wait for.
|
|
ctx, cancel := context.WithTimeout(context.Background(), wait)
|
|
defer repository.DbPool.Close()
|
|
defer cancel()
|
|
// Doesn't block if no connections, but will otherwise wait
|
|
// until the timeout deadline.
|
|
srv.Shutdown(ctx)
|
|
// Optionally, you could run srv.Shutdown in a goroutine and block on
|
|
// <-ctx.Done() if your application should wait for other services
|
|
// to finalize based on context cancellation.
|
|
log.Println("shutting down")
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
func homeRoute(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
res := map[string]any{
|
|
"halo": "halo",
|
|
}
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|