fetch

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2020 License: MIT Imports: 14 Imported by: 0

README

Go Webserver

Go Report Card Go Doc Build Status Release

This is an experimental web server written in Go. Do not use in production.

How to get set up

Create a new Go project and put this in your main.go file:

package main

import (
	"fmt"
	"fetch/core"
)

func main() {
	app := core.Server{}

	app.Setup()

	app.GetRouter().Get("/", homeHandler)

	app.Start()
}

func homeHandler(w core.Response, r core.Request) {
	fmt.Fprint(w, "Hello Go!\n")
}

You can change the port by setting the "_port" parameter on the Server struct:

app := core.Server{Port: "80"}

If you're using SSL or static file hosting, it's important to specify the projects base directory in the server struct:

baseDir, _ := os.Getwd()
app := core.Server{BaseDir: baseDir}

Documentation

Overview

Package fetch - core web server code

Index

Constants

This section is empty.

Variables

View Source
var (
	DB *gorm.DB
)

Functions

func Init

func Init(router *Router)

Init will set up the registers

Types

type DatabaseStruct

type DatabaseStruct struct {
	Database struct {
		Database string
		Driver   string
		Username string
		Password string
		Port     uint16
	}
}

DatabaseStruct stores necessary details for database connection

type Request

type Request struct {
	R  *http.Request
	DB *gorm.DB
}

Request type is a placeholder for *http.Request

type Response

type Response struct {
	http.ResponseWriter
}

Response type is a placeholder for http.ResponseWriter

func (Response) ToJSON

func (response Response) ToJSON(i interface{}) error

ToJSON writes an interface to screen as JSON

type Router

type Router struct {
	GetRegister    map[string]http.HandlerFunc
	HeadRegister   map[string]http.HandlerFunc
	PostRegister   map[string]http.HandlerFunc
	PutRegister    map[string]http.HandlerFunc
	DeleteRegister map[string]http.HandlerFunc
	OptionRegister map[string]http.HandlerFunc
}

Router struct to handle route registration

func NewRouter

func NewRouter() *Router

NewRouter creates a new instance of the router

func (*Router) Delete

func (router *Router) Delete(route string, h http.HandlerFunc) error

Delete registers a DELETE request route

func (*Router) Get

func (router *Router) Get(route string, h http.HandlerFunc) error

Get registers a GET request route

func (*Router) Head

func (router *Router) Head(route string, h http.HandlerFunc) error

Head registers a HEAD request route

func (*Router) Options

func (router *Router) Options(route string, h http.HandlerFunc) error

Options registers a Options request route

func (*Router) Post

func (router *Router) Post(route string, h http.HandlerFunc) error

Post registers a POST request route

func (*Router) Put

func (router *Router) Put(route string, h http.HandlerFunc) error

Put registers a PUT request route

func (*Router) SetupRoutes

func (router *Router) SetupRoutes(mux *http.ServeMux)

SetupRoutes takes care of initialising the routes from the register

type Server

type Server struct {
	BaseDir string
	Port    string
	Config  map[string]interface{}
}

Server is the... server

func (*Server) AddHeader

func (server *Server) AddHeader(key string, value string)

AddHeader sets a custom header

func (*Server) Cleanup

func (server *Server) Cleanup()

Cleanup cleans up any connections the server might have when it's terminated

func (*Server) GetDatabase

func (server *Server) GetDatabase() (*gorm.DB, error)

GetDatabase gets the active database connection

func (*Server) GetHeaders

func (server *Server) GetHeaders() map[string]string

GetHeaders returns all custom set headers

func (*Server) GetRouter

func (server *Server) GetRouter() *Router

GetRouter returns the router

func (*Server) RemoveHeader

func (server *Server) RemoveHeader(key string) error

RemoveHeader removes a custom set header

func (*Server) SetRouter

func (server *Server) SetRouter(router *Router)

SetRouter sets the current router

func (*Server) Setup

func (server *Server) Setup() error

Setup sets up defaults

func (*Server) Start

func (server *Server) Start()

Start starts the webserver

Directories

Path Synopsis
cmd
pkg
log

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL