You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this libary is currently not finished and still being developed.
if you have any errors please report to make this libary better.
About
Simple Http libary made in go-lang from scratch. As of 3/4/24 it supports:
routing,
GET, POST, DELETE, PATCH, PUT
JSON request
Full header access
html rendering
css rendering
js support
Image support
The goal of this project is to be useable in an actual application.
Future Plans:
99% success rate
hot reloading
unit test
[somewhat done] Easy json responses
Large request acceptance
Support for large raw data streaming
[somewhat done] Easy searching of request & responses
and more...
installation
go get github.com/minij147/scratch-http/
Demo
package main
import"github.com/minij147/scratch-http/server"funcmain(){
typemsgstruct {
Messagestring`json:"Message"`
}
serv:=server.CreateHttpServer()
serv.Get("/", func(req*server.HttpRequest, res server.HttpResponse) {
res.WriteStatus(200, "OK")
res.SendJSON(msg{
Message: "HELLO FROM BASE ROUTE!",
})
})
serv.Listen("localhost", "3000")
}
Creating Server / running
initilizes thehttpserver. Thiswillbeusedtocommunicatewithhttp.
//returns *server.HttpServer{}serv:=server.CreateHttpServer()
//turns on server to listen on ip and portserv.Listen(host, port)
Method Functions
//this is how to create routes assigned to specfic methods. //Func will be called when route is called.serv.Get("route", func, middleware...func)
serv.Post("route", func, middleware...func)
serv.Delete("route",func, middleware...func)
serv.Put("route",func, middleware...func)
serv.Patch("route",func, middleware...func)
Response
typeHttpResponsestruct {
statusLinestringheadermap[string]stringconn net.Conn
}
//return parsed filehttpResponse.SendFile(filename)
//sends string to browserhttpResponse.Send(string)
//sends json to browserhttpResponse.SendJSON(interface{})
//sends error to the browser with code and messagehttpResponse.SendError(codeint, msgstring)
//sets header with the key and gives it the valuehttpResponse.WriteHeader("key", "value")
//writes response code to the browserhttpResponse.WriteStatus(codeint, msgstring)
//will redirect to a new route after function callinghttpResponse.Redirect("route")
Request
//requests data will automatically be prased//this is how to accesstypeHttpRequeststruct {
MethodstringRoutestringHttpVersionstringBodyinterface{}
Metadata*HeaderDataQuery*HeaderData
}
Accessing Query or Metadata using HeaderData
typeHeaderDatastruct {
datamap[string]string
}
//value and if it passed //up to user to handle bool//searches map for you //returns "" and False if not found//returns value and True if foundfuncFind("key") (string, bool)
//inserts into map based off value to setfuncInsert("key", "value")
Middleware
/*Important notemiddleware will return a bool this is the status of the function call.False -> means no redirect we can continue to next middlewareTrue -> means redirect and we must stop the middleware and all future callsMiddleware gets executed in order 0 --> n*///To declarefuncexampleMiddleware(req*server.HttpRequest, res server.HttpResponse) bool {
fmt.Println("From Middleware")
returnfalse
}
//passing it inserv.Get("/",func, exampleMiddleware)
//example flow of middleware//the flow would look like//mid1 -> mid2 -> mid3 -> logicserv.Get("/",logicfunc, mid1func, mid2, func, mid3func)