step 8 : configurasi url path selfie, load file, image not found, and deploy to devone

This commit is contained in:
sindhu
2024-01-23 14:54:36 +07:00
parent d3fe3234eb
commit f83fe2dab0
8 changed files with 80 additions and 13 deletions

View File

@@ -42,6 +42,68 @@ func main() {
srv := handler.NewDefaultServer(
generated.NewExecutableSchema(generated.Config{Resolvers: &resolver.Resolver{}}))
// get file selfie
router.Get("/img/{typeAbsensi}/{subfolder}/{imgname}", func(w http.ResponseWriter, r *http.Request) {
// inisialisasi
var urlPath string
// get from url param
typeAbsensi := chi.URLParam(r, "typeAbsensi")
subfolder := chi.URLParam(r, "subfolder")
imgname := chi.URLParam(r, "imgname")
// check param empty
if typeAbsensi == "" {
w.Write([]byte("Invalid typeAbsensi"))
return
}
if subfolder == "" {
w.Write([]byte("Invalid subfolder"))
return
}
if imgname == "" {
// w.Write([]byte("Invalid imgname"))
http.ServeFile(w, r, config.Data.Get("notFoundImage"))
// return
}
// klu clockin
if typeAbsensi == "clockin" {
// urlPath = "./selfie_attachment/" + subfolder + "/" + imgname
urlPath = config.Data.Get("clockInFolder") + subfolder + "/" + imgname
}
if typeAbsensi == "clockout" {
// urlPath = "./selfie_attachment/" + subfolder + "/" + imgname
urlPath = config.Data.Get("clockOutFolder") + subfolder + "/" + imgname
}
if typeAbsensi == "cuti" {
// urlPath = "./cuti/" + subfolder + "/" + imgname
urlPath = config.Data.Get("cutiFolder") + subfolder + "/" + imgname
}
if typeAbsensi == "ijin" {
// urlPath = "./ijin/" + subfolder + "/" + imgname
urlPath = config.Data.Get("ijinFolder") + subfolder + "/" + imgname
}
_, err := os.ReadFile(urlPath)
// gambar tidak ditemukan akan return image not found
if err != nil {
// w.Write([]byte("Invalid Attachment Clock In"))
// return
http.ServeFile(w, r, config.Data.Get("notFoundImage"))
return
}
http.ServeFile(w, r, urlPath)
})
router.Handle("/", playground.Handler("GraphQL playground", "/query"))
router.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)