Skip to content
Wesley Yan Soares Brehmer edited this page Oct 8, 2023 · 22 revisions

Scalbi by Wesley Yan Soares Brehmer

Starting with Scalbi

First of all, you need to have HTML files. If you do, you'll need to get the name of each one, as you'll need to pass this on to Scalbi's settings. Scalbi is compatible with any and all Go frameworks, and your application won't change much in different frameworks, but in this example we're going to use net/http. Here's how to get started:

package main

import (
	"net/http"

	scalbi "github.com/simplyYan/Scalbi" // import scalbi
)

func main() {
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		data := map[string]string{
			"home": "static/test.html",
		}
		s := scalbi.NewScal()
		s.Server(w, data)
	})

	http.ListenAndServe(":8080", nil)
}

Now it's ready. You can now access the files/directories using the names you passed in. For example, in the example above, we created a directory called "home" which carries "index.html", so we could use our localhost, which in this case is "http://localhost:3030" and access this "index.html" with "http://localhost:3030/#home".

Sending requests

By adding Scalbi's JavaScript API to your HTML, you can create requests more easily, securely and quickly. Here's an example:

// Create a Scalbi instance with the defined server URL
const sca = new Scalbi('http://localhost:8080/api/endpoint');

// Configure targets based on hashes
sca.set({
  home: 'index.html',
  log: 'login.html',
  dash: 'board.html',
});

// Send data to the server using the "send" function (if the server URL is set)
if (sca.send) {
  const dados = {
    campo: 'Valor de exemplo', // Your details here
  };
  sca.send('POST', dados);
}

Reading values from forms/requests

Here's a well-explained example of getting data from requests or forms (POST):

    scal := NewScalbi()

    app := fiber.New()

    app.Post("/", func(c *fiber.Ctx) error {
        name := c.FormValue("name")
        pwd := c.FormValue("pwd")

        // Render values using Scalbi
        nome := scal.Render("nome", name)
        password := scal.Render("senha", pwd)

        // Defines the values in the context to be used in the HTML template
        c.Locals("Nome", nome)
        c.Locals("Senha", password)

        // Render the HTML model
        return c.Render("template.html", fiber.Map{})
    })

    // Route to render the HTML model
    app.Get("/view", func(c *fiber.Ctx) error {
        return c.Render("template.html", fiber.Map{})
    })

    app.Static("/", "./public") // Pasta "public" contém o arquivo template.html

    // Starts the server on port 3030
    app.Listen(":3030")

Obtaining URL/Server information

To obtain information such as Connection Time to a URL, Network Ping, and Network Availability, you first need to create an instance of the Scaltrack type using the New() function. After that, you can use the methods associated with the Scaltrack fields.

Here are some examples:

package main

import (
	"fmt"
)

func main() {
	// Create a new instance of Scaltrack
	s := Scaltrack.New()

	// Uses the Time method of the URL field to measure the response time of a URL
	duration := s.url.Time("http://example.com")
	fmt.Printf("Response time: %v\n", duration)

	// Uses the Ping method of the Net field to check internet connectivity
	pingStatus := s.net.Ping()
	fmt.Printf("Ping Status: %s\n", pingStatus)

	// Uses the Status method of the URL field to check the status of a URL
	urlStatus := s.url.Status("http://example.com")
	fmt.Printf("URL Status: %s\n", urlStatus)
}

Examples with other frameworks

Scalbi is compatible with any framework, that is, it is universal. Scalbi is built with just pure Go and its native libraries, without using external libraries, and this makes it even faster, in addition to being based only on one file. Scalbi works flawlessly with any Go framework, and most of the time, it is faster than the framework itself, so it is a solid option for working with any Go framework. But, Scalbi certainly works better with Fiber, since Fiber in my opinion is the best Go framework, and working together with Scalbi, there is no better option. See some examples with Gin, Echo, Fiber, Chi, Beego, Revel, Iris, Buffalo, Gorilla Mux, and Martini:

  • 🏆 FIBER:
package main

import (
	"github.com/gofiber/fiber/v2"
	scalbi "github.com/simplyYan/Scalbi"
)

func main() {
	app := fiber.New()
	app.Static("/", "./")

	app.Get("/", func(c *fiber.Ctx) error {
		s := scalbi.NewScal()
		data := map[string]string{
			"home": "test.html", // define your html files here and the route names
		}
		err := s.Server(c.SendString, data)
		c.Type("html") // Sets the response content type to text/html
		return err
	})

	app.Listen(":3000")
}

The Fiber code is tested and 100% created by me, while the codes below with other frameworks were generated with an AI based on GPT-4 that received training on the Scalbi framework, and are not tested.

  • GIN:
package main

import (
	"github.com/gin-gonic/gin"
	scalbi "github.com/simplyYan/Scalbi"
)

func main() {
	r := gin.Default()
	r.Static("/static", "./static")
	r.GET("/", func(c *gin.Context) {
		data := map[string]string{
			"home":  "static/test.html",
			"about": "static/about.html",
		}
		s := scalbi.NewScal()
		s.Server(c.Writer, data)
	})
	r.Run(":8080")
}
  • ECHO:
package main

import (
	"github.com/labstack/echo"
	scalbi "github.com/simplyYan/Scalbi"
)

func main() {
	e := echo.New()
	e.Static("/static", "static")
	e.GET("/", func(c echo.Context) error {
		data := map[string]string{
			"home":  "static/test.html",
			"about": "static/about.html",
		}
		s := scalbi.NewScal()
		return s.Server(c.Response().Writer, data)
	})
	e.Start(":8080")
}
  • CHI:
package main

import (
	"net/http"

	"github.com/go-chi/chi"
	scalbi "github.com/simplyYan/Scalbi"
)

func main() {
	r := chi.NewRouter()
	r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		data := map[string]string{
			"home":  "static/test.html",
			"about": "static/about.html",
		}
		s := scalbi.NewScal()
		s.Server(w, data)
	})
	http.ListenAndServe(":8080", r)
}
  • BEEGO:
package main

import (
	"github.com/astaxie/beego"
	scalbi "github.com/simplyYan/Scalbi"
)

type MainController struct {
	beego.Controller
}

func (this *MainController) Get() {
	data := map[string]string{
		"home":  "static/test.html",
		"about": "static/about.html",
	}
	s := scalbi.NewScal()
	s.Server(this.Ctx.ResponseWriter, data)
}

func main() {
	beego.SetStaticPath("/static", "static")
	beego.Router("/", &MainController{})
	beego.Run()
}
  • REVEL:
// controllers/app.go
package controllers

import (
	"github.com/revel/revel"
	scalbi "github.com/simplyYan/Scalbi"
)

type App struct {
	*revel.Controller
}

func (c App) Index() revel.Result {
	data := map[string]string{
		"home":  "public/test.html",
		"about": "public/about.html",
	}
	s := scalbi.NewScal()
	s.Server(c.Response.Out, data)
	return nil
}
  • IRIS:
package main

import (
	"github.com/kataras/iris/v12"
	scalbi "github.com/simplyYan/Scalbi"
)

func main() {
	app := iris.New()
	app.HandleDir("/static", iris.Dir("./static"))
	app.Get("/", func(ctx iris.Context) {
		data := map[string]string{
			"home":  "static/test.html",
			"about": "static/about.html",
		}
		s := scalbi.NewScal()
		s.Server(ctx.ResponseWriter(), data)
	})
	app.Run(iris.Addr(":8080"))
}
  • BUFFALO:
// actions/app.go
package actions

import (
	"github.com/gobuffalo/buffalo"
	scalbi "github.com/simplyYan/Scalbi"
)

func HomeHandler(c buffalo.Context) error {
	data := map[string]string{
		"home":  "public/test.html",
		"about": "public/about.html",
	}
	s := scalbi.NewScal()
	s.Server(c.Response(), data)
	return nil
}

// actions/render.go
func init() {
	buffalo.Grifts(actions.App())
}

// app.go
func App() *buffalo.App {
	if app == nil {
		app = buffalo.New(buffalo.Options{})
		app.GET("/", HomeHandler)
	}
	return app
}
  • GORILLA MUX:
package main

import (
	"net/http"

	"github.com/gorilla/mux"
	scalbi "github.com/simplyYan/Scalbi"
)

func main() {
	r := mux.NewRouter()
	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		data := map[string]string{
			"home":  "static/test.html",
			"about": "static/about.html",
		}
		s := scalbi.NewScal()
		s.Server(w, data)
	})
	http.ListenAndServe(":8080", r)
}