DEV Community

Cover image for Slide, a Go web framework for developers coming from Express.
sai umesh
sai umesh

Posted on

Slide, a Go web framework for developers coming from Express.

Slide is one of the fastest frameworks, built on top of fasthttp. People coming from express will feel at home.

Motivation

While playing around with the Go’s net/http, one thing that we missed most was the lack of middleware support and the problems it solves. After a little reading, we decided to write our own web framework which would solve the issues like middleware support with next, handle a wide range of files, Upload, Download, etc.

🚀 Example

package main

import (
    "log"
    "github.com/go-slide/slide"
    "github.com/go-playground/validator/v10"
)

func main() {
    validate := validator.New()
    config := slide.Config{
        Validator: validate,
    }
    app := slide.InitServer(&config)
    app.Get("/", func(ctx *slide.Ctx) error {
        return ctx.Send(http.StatusOK, "Hello, World")
    })
    log.Fatal(app.Listen("localhost:4321"))
}

Features

Slide supports Middleware with Next function

Express

var express = require('express')
var app = express()

app.use(function (req, res, next) {
  console.log('Time:', Date.now())
  next()
})

Slide

app := slide.InitServer(&config)

## Application level
// you can multiple middlewares also
app.Use(func(ctx *slide.Ctx) error {
    fmt.Println("this will run for all URL(s)")
    return ctx.Next()
})

Nested (Grouped) Routes

Express

var express = require('express')
var router = express.Router()
var auth = require('./auth')

router.use('/auth', auth)

// auth.js
var express = require('express')
var router = express.Router()

// GET /auth/login
router.get('/login', function (req, res, next) {
  res.send('this is the index auth/login')
});

module.exports = router

Slide

auth := app.Group("/auth")
auth.Use(func(ctx *slide.Ctx) error {
    fmt.Println("this will run for all /auth URL(s)")
    return ctx.Next()
})
auth.Get("/login", func(ctx *slide.Ctx) error {
    return ctx.Send(http.StatusOK, "Hello, World")
})

Files

Slide supports wide range of helpers for file

// serve dir
app.ServerDir("/static", "static")

// single file
app.ServeFile("/js", "static/login.js")

// Downloading file
app.Get("/download", func(ctx *slide.Ctx) error {
    return ctx.SendAttachment("static/login.js", "login.js")
})

// uploading file
app.Post("/upload", func(ctx *slide.Ctx) error {
    return ctx.UploadFile("static/login.js", "login.js")
})

// serve single from route
app.Get("/servefile", func(ctx *slide.Ctx) error {
   return ctx.ServeFile("static/index.html")
})

We would love to hear feedback from you. Thank you for reading.

Top comments (0)