DEV Community

ynwd
ynwd

Posted on • Updated on

Low cost cloud web app with fastrex-serverless

Google provides a very efficient serverless service. Cloud function will serve if someone uses it and will iddle if no one is using it. You pay nothing when your function is idle. So cheap, right?

With fastrex-serverless you can develop a golang based fullstack web application: create multiple routes, handlers, middlewares, handle static files, and render html templates.

After that, you can associate the app with the entry point in the cloud function. The entry point is then redirected to firebase hosting according to the domain and url you want.

Live demo: https://phonic-altar-274306.web.app
Source code: https://github.com/fastrodev/serverless

Getting started

Create serverless webapp folder: serverless

mkdir serverless && cd serverless
go mod init github.com/fastrodev/serverless
go get github.com/fastrodev/fastrex
Enter fullscreen mode Exit fullscreen mode

You can rename the module github.com/fastrodev/serverless to anything.

Create web application

create an app folder: internal

mkdir internal
Enter fullscreen mode Exit fullscreen mode

create a webapp file: internal/app.go

package internal

import "github.com/fastrodev/fastrex"

func CreateApp() fastrex.App {
    app := fastrex.New()
    app.Get("/", handler)
    return app
}

func handler(req fastrex.Request, res fastrex.Response) {
    res.Json(`{"message":"ok"}`)
}

Enter fullscreen mode Exit fullscreen mode

You can add new routes, handlers, templates and static files later. See the full source code at: internal/app.go

Localhost entrypoint

To test locally, you can create a localhost webapp entrypoint file: cmd/main.go

package main

import (
    "github.com/fastrodev/serverless/internal"
)

func main() {
    internal.CreateApp().Listen(9000)
}

Enter fullscreen mode Exit fullscreen mode

You can see the full source code at: cmd/main.go

You can run by this command:

go run cmd/main.go
Enter fullscreen mode Exit fullscreen mode

You can access it via url:

http://localhost:9000
Enter fullscreen mode Exit fullscreen mode

Serverless entrypoint

To see the serverless endpoint, you must create a serverless webapp entrypoint file: serverless.go

package serverless

import (
    "net/http"

    "github.com/fastrodev/serverless/internal"
)

func Main(w http.ResponseWriter, r *http.Request) {
    internal.CreateApp().ServeHTTP(w, r)
}
Enter fullscreen mode Exit fullscreen mode

You can see the full source code at: serverless.go

Cloud function deployment

Prerequisite: install cloud sdk

To see live serverless endpoint, you must deploy to google cloud function:

gcloud functions deploy Main --runtime go113 --trigger-http --allow-unauthenticated
Enter fullscreen mode Exit fullscreen mode

Live demo: https://us-central1-phonic-altar-274306.cloudfunctions.net/Main

Firebase domain setup

Prerequisite: install firebase-cli

We will redirect above url to firebase domain. You can change it with your own from firebase dashboard.

Init hosting files:

firebase init hosting
Enter fullscreen mode Exit fullscreen mode

Remove public folder:

rm -rf public
Enter fullscreen mode Exit fullscreen mode

Change firebase config:

{
  "hosting": {
    "rewrites": [
      {
        "source": "**",
        "function": "Main"
      }
    ],
    "ignore": [
      "**/cmd/**",
      "**/internal/**",
      "**/static/**",
      "**/template/**",
      ".gitignore",
      "cloudbuild.yaml",
      "firebase.json",
      "go.mod",
      "go.sum",
      "README.md",
      "serverless.go",
      "**/.*"
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Deploy to firebase:

firebase deploy
Enter fullscreen mode Exit fullscreen mode

Live demo: https://phonic-altar-274306.web.app

Top comments (0)