Kaydet (Commit) a4ac2642 authored tarafından Volkan Önder's avatar Volkan Önder

Son Submit

üst 26b0f85c
<<<<<<< HEAD
# Go ile Basit web Ugulaması
=======
# Go ile Basit Web Uygulaması
>>>>>>> 5ab74f2d0e3b4b08d3531095829651e78a973b60
Bugün Go dili ile nasıl basit bir web uygulaması geliştiririz bunu öğreneceğiz.
......@@ -87,19 +84,17 @@ if err := server.ListenAndServe(); err != nil {
Bu kısım ile uygulamayı ayağa kaldırıp dinlemeye alıyoruz. Hata Yakalama ile herhangi bir problemin gerçekleşmesine karşı logluyoruz.
##### Şimdi de ana fonksiyonun çalışması için gerekli yan fonksiyonları inceleyelim.
```go
func logging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
req := fmt.Sprintf("%s %s", r.Method, r.URL)
log.Println(req)
next.ServeHTTP(w, r)
log.Println(req, "completed in", time.Now().Sub(start))
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
req := fmt.Sprintf("%s %s", r.Method, r.URL)
log.Println(req)
next.ServeHTTP(w, r)
log.Println(req, "completed in", time.Now().Sub(start))
})
}
```
......@@ -107,7 +102,7 @@ Burada istek atıldığında istek tipi zamanı ve ne kadar sürede tamamlandı
```go
func public() http.Handler {
return http.StripPrefix("/public/", http.FileServer(http.Dir("./public")))
return http.StripPrefix("/public/", http.FileServer(http.Dir("./public")))
}
```
......@@ -118,30 +113,28 @@ var templates = template.Must(template.ParseFiles("./templates/base.html", "./te
// index is the handler responsible for rending the index page for the site.
func index() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b := struct {
Title template.HTML
BusinessName string
Slogan string
}{
Title: template.HTML("Business &verbar; Landing"),
BusinessName: "Business,",
Slogan: "we get things done.",
}
err := templates.ExecuteTemplate(w, "base", &b)
if err != nil {
http.Error(w, fmt.Sprintf("index: couldn't parse template: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b := struct {
Title template.HTML
BusinessName string
Slogan string
}{
Title: template.HTML("Business &verbar; Landing"),
BusinessName: "Business,",
Slogan: "we get things done.",
}
err := templates.ExecuteTemplate(w, "base", &b)
if err != nil {
http.Error(w, fmt.Sprintf("index: couldn't parse template: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
})
}
```
Bu kısımda ise ana sayfayı yapılandırıyor olacağız. Ana sayfa çağırıldığında /templates/base.html dosyasına yönlendiriyoruz. Altta tek rarda hata yakalama ile herhangi olası dosya kaybına karşı da kullanıcıya hatayı gösteriyoruz.
#### Sonuç
Son olarak belirtmeliyiz ki Go dili diğer dillere kıyasla daha basit bir şekilde Web uygulamasını ayağa kaldırabiliyor.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment