Ana sawetara cara sing murah kanggo dadi tuan rumah situs web statis sing digawe karo generator situs statis kaya Jekyll, Hugo, utawa Pelican: Kabeh blog iki digawe kanthi statis nggunakake Jekyll. Nanging, aku ora bisa nggunakake opsi ing ndhuwur, amarga, sajrone umur blog iki, aku wis ngganti jeneng domain, ngganti skema URL, lan ngganti jeneng kiriman, lan aku pengin. tetep urip kabeh URL lawas Aku wis hosting blog iki nggunakake Apache lan, luwih anyar, nginx ing mesin virtual siji, lan fitur redirection saka salah siji Piece saka piranti lunak bisa uga apik, nanging aku wis siyap kanggo tuan rumah nang endi wae anyar lan beda. Kiriman sadurunge nerangake carane aku ngarahake URL saka domain lawas menyang domain anyar nggunakake Google App Engine lan Python, nanging saiki aku butuh cara kanggo ngladeni konten statis. **lan** pangalihan URL saka domain sing padha. Persyaratan pangalihan domain sing padha, mula aku ora bisa nggunakake fitur statis mung Google App Engine (disambungake ing dhaptar ing ndhuwur). Nanging, aku bisa nggunakake Google App Engine kanthi kombinasi aplikasi Golang sing prasaja kanggo nglayani konten statis **lan** pangalihan domain sing padha ## Kenapa Google App Engine? Sadurunge nyilem menyang liyane saka kiriman, mbok menawa sampeyan kepingin weruh, kok host blog ing Google App Engine? Punika alasan kula: - Yen lalu lintas sampeyan cocog karo tingkat gratis App Engine 28 jam instan lan lalu lintas metu 1 GB saben dina, hosting blog kasebut gratis. - Pushing nganyari wis rampung karo siji printah - Logging lan ngawasi digabungake nggunakake Stackdriver - Skala munggah lan mudhun otomatis adhedhasar pola lalu lintas - Kanthi sawetara klik, log web bisa gampang didorong menyang kaya BigQuery kanggo panyimpenan jangka panjang lan analisis ad hoc - Ngatur sertifikat SSL nggunakake LetâÃÂÃÂs Encrypt ## Prasyarat Posting iki nganggep ing ngisor iki: - Sampeyan wis kenal karo Google Cloud Platform (GCP) lan wis nggawe Proyek GCP - Sampeyan wis nginstal Google Cloud SDK - Sampeyan wis keasliane ing gcloudcommand marang Akun Google sampeyan Nggawe Proyek GCP Yen sampeyan durung nggawe a **Proyek GCP tindakake langkah iki: - Bukak browser web, banjur gawe utawa mlebu menyang Akun Google - Navigasi menyang GCP Console - Yen iki proyek GCP pisanan sampeyan, sampeyan bakal dijaluk nggawe Proyek GCP. Saben Akun Google entuk kredit $300 kanggo digunakake sajrone 12 wulan menyang GCP. Sampeyan kudu ngetik kertu kredit kanggo nggawe Proyek GCP, nanging ora bakal dikenani biaya nganti kredit $300 digunakake utawa 12 sasi kadaluwarsa - Yen iki Proyek GCP anyar, sampeyan kudu ngaktifake Compute Engine API kanthi navigasi menyang bagean Compute Engine ing GCP Console lan ngenteni wiwitan rampung. Instal Google Cloud SDK Yen sampeyan durung nginstal ** Google Cloud SDK tindakake pandhuan ing kene Otentikasi gcloud Sawise sampeyan nggawe Proyek GCP lan nginstal Google Cloud SDK, langkah pungkasan yaiku kanggo keasliane printah gcloud menyang Akun Google sampeyan. Bukak aplikasi terminal lan jalanake printah ing ngisor iki: gcloud auth login Kaca web bakal mbukak ing browser web sampeyan. Pilih Akun Google lan wenehi idin kanggo ngakses GCP. Sawise rampung, sampeyan bakal keasliane lan siap maju ## Nggawe Direktori Sabanjure, gawe direktori nang endi wae ing workstation kanggo nyimpen aplikasi Google App Engine: mkdir ~/Sites/example.com/app_engine Ganti menyang direktori kasebut: cd ~/Sites/example.com/app_engine Sisa kiriman iki bakal nganggep sampeyan lagi nggarap direktori iki Kajaba iku, nggawe direktori ing njero ** app_engine ** direktori disebut ** statis mkdir ~/Sites/example.com/app_engine/static Sampeyan bakal bali maneh direktori iki mengko ## Gawe app.yaml Google App Engine biasane mbutuhake rong file: **app.yaml** lan **file aplikasi** sing ditulis nganggo Python, Golang, Java, utawa PHP - ing kasus iki bakal dadi Golang. **app.yaml** nyedhiyakake konfigurasi sing dibutuhake kanggo mbukak aplikasi sampeyan. Ana macem-macem parameter sing bisa ana ing **app.yaml Parameter kasebut bisa beda-beda adhedhasar basa pamrograman sing digunakake. Kanggo kirim iki, Golang bakal digunakake, lan sampeyan bisa nemokake kabeh parameter Golang sing kasedhiya ing kene Nggawe file **app.yaml** kanthi isi ing ngisor iki: runtime: go api_version: go1 handler: - url:script: _go_app secure: always redirect_http_response_code: 301 Wigati sing ** aman: tansah ** wis disetel. Iki tegese aplikasi Golang bakal tansah dilayani liwat HTTPS. Yen pangguna pungkasan navigasi menyang aplikasi web liwat HTTP, kanthi standar bakal 302 dialihake menyang versi HTTPS. Mulane **redirect_http_response_code: 301** uga wis disetel. Aku tansah pengin aplikasi web dilayani liwat HTTPS, lan aku ora pengin mesin telusuran nerjemahake pangalihan saka HTTP menyang HTTPS minangka pangalihan sementara; iku pangalihan permanen Yen sampeyan duwe aset statis, lan sampeyan bisa uga duwe, praktik paling apik kanggo ngandhani App Engine babagan iki lan ngidini aset kasebut saka panyimpenan obyek tinimbang saka aplikasi sampeyan. Nindakake iki gampang lan uga ditindakake liwat **app.yaml** file Contone, yen sampeyan duwe file favicon, direktori CSS, direktori Javascript, lan direktori gambar, gunakake ing ngisor iki. **app.yaml** berkas: runtime: go api_version: go1 handler: - url: /favicon.png$ static_files: static/favicon.png upload: static/favicon.png - url: /css static_dir: static/css - url: /js static_dir: static/js - url: /images static_dir: static/images - url:script: _go_app aman: tansah redirect_http_response_code: 301 ## Gawe main.go Sabanjure, sampeyan butuh file aplikasi Golang Kanggo kode ing ngisor iki kanggo nyukupi kabutuhan sampeyan, gawe file **main.go salin lan tempel kode ing ngisor iki, lan gawe modifikasi ing ngisor iki: - Ing domainvariable, ngganti nilai kanggo cocog jeneng domain karo protokol HTTP bener - Ing urlsmap, ganti kabeh pasangan nilai kunci kanggo cocog karo pangalihan sing sampeyan butuhake. Ganti saben tombol nganggo mung bagean path ( /example-post-1.html tinimbang httpsexample.com/example-post-1.html) saka domain saiki URL lawas sing sampeyan pengin tetep urip. Banjur ganti saben nilai karo bagean path URL anyar domain saiki sing pengin dialihake. Kabeh pangalihan bakal dadi 301 pangalihan. Iki bisa diowahi kanthi ngganti **301** ing kode ing ngisor iki menyang kode status pangalihan HTTP sing beda kayata **302** impor utama paket ("net/http""os""strings") func init() { http.HandleFunc handler) } func handler(w http.ResponseWriter, r *http.Request) {// True (ok) yen dijaluk path ana ing peta url yen nilai, ok: = urls [r.URL.Path]; ok {nilai = domain + nilai http.Redirect(w, r, value, 301) } else {path := "static/"+ r.URL.Path // Return 403 yen HTTP request menyang direktori sing ana lan ora ora ngemot file index.html yen f, err: = os.Stat(path); err == nil&& f.IsDir() { index := strings.TrimSuffix(path+ "/index.html"if _, err := os.Open(index); err != nil { w.WriteHeader (403) w.Writebytehtml> 403 Dilarang

403 Forbidden

return } } // Return custom 404 page if HTTP request is to a non-existent file if _, err := os.Stat(path); os.IsNotExist(err) { w.WriteHeader(404) http.ServeFile(w, r, "static/404.html") return // Withoutreturn, a "404 page not found" string will be displayed at the bottom of your custom 404 page } http.ServeFile(w, r, path) return } } var domain string = "httpsexample.com" var urls = map[string]string{ "/example-post-1.html": "/post/example-post-1.html", "/example-post-2.html": "/post/example-post-2.html", "/example-post-3.html": "/post/example-post-3.html", } ## Generate the Static Content With **app.yaml** and **main.go** saved, the last piece is to generate your static content and store it in the **static** directory you created earlier How you do this entirely depends on what static site generator you are using If you are using Jekyll, you can configure the **destination** parameter in Jekyll’s _ **config.yml** file to save your static content in any directory on your workstation. So, you could set the **destination** parameter to Sites/example.com/app_engine/static and, every time you run jekyll build, the static content will be saved in that directory ## Deploy to App Engine With **app.yaml **main.go and your static content generated, you are ready to deploy your Google App Engine application Assuming gcloud is already pointed at the Google Cloud Project you want to deploy to, verify with gcloud config list project, run the following command: gcloud app deploy The command will output the appspot URL your application will be deployed to and ask if you want to continue. Typically, the appspot URL is **httpsyour-project-id.appspot.com This is also a useful self-check to make sure you are not deploying your application to the wrong Google Cloud Project. If everything looks okay, type **Y** and **Enter** to deploy your application. Depending on how large your static content is, your application should be deployed within about one minute ## Setup DNS At this point, your application is deployed under URL **httpsyour-project-id.appspot.com Unless your website uses that as its domain name, you will probably want to setup a custom domain that uses your actual current domain name The App Engine section of the Google Cloud Console can be used to do this. Go here and follow the instructions to configure your custom domain Once that is complete and DNS has had time to propagate, you should be able to navigate in your web browser to one of your current domain’s old URLs, for example **httpsexample.com/example-post-1.html and have it redirect to your current domain’s new URLs, for example **httpsexample.com/post/example-post-1.html** ## Pushing Updates To push updates, make the necessary changes in your static site’s source directory, regenerate the static content, and redeploy to Google App Engine by changing into the Sites/example.com/app_engine** directory and running gcloud app deploy ## References - A Surprising Feature of Golang that Colored Me Impressed - How to check if a map contains a key in go? - Disable directory listing with http.FileServer - 3 Ways to Disable http.FileServer Directory Listings - Handling HTTP Request Errors in GO - HTTP and Error management in Go - please add ability to set custom 404 notFoundHandler for http.FileServer