restserver

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2023 License: MIT Imports: 10 Imported by: 0

README

Restserver

Go Report Card PkgGoDev

Overview

This is a sample REST server application inspired by the tutorial here. I have added an SQLite3 database as a backing store.

Installation

  • Clone the git repository:
git clone git@github.com:philhanna/restserver.git
  • Create a config.yml file in $HOME/.config/restserver (on Windows, %appdata%\restserver)
host: localhost
port: 10000
dbname: /tmp/articles.db
dbsql: |
  DROP TABLE IF EXISTS articles;
  CREATE TABLE articles (
      id text,
      title text,
      description text,
      content text
  );
  BEGIN;
  INSERT INTO articles VALUES("1", "Hello 1", "Article 1 description", "Article 1 content");
  INSERT INTO articles VALUES("2", "Hello 2", "Article 2 description", "Article 2 content");
  COMMIT;

Adjust the port and dbname values as needed.

References

Documentation

Index

Constants

View Source
const (
	PACKAGE_NAME   = "restserver"
	YAML_FILE_NAME = "config.yml"
)

Variables

This section is empty.

Functions

func ConfigurationFile

func ConfigurationFile() (string, error)

ConfigurationFile returns the fully qualified name of the YAML file.

func Connect

func Connect() (*sql.DB, error)

Connect opens a connection to the database.

func Create

func Create(w http.ResponseWriter, r *http.Request)

Create adds a new article to the collection, based on JSON data contained in the request. This is a POST method.

func Delete

func Delete(w http.ResponseWriter, r *http.Request)

Delete gets an article number from the request and deletes the corresponding article in the collection. This is a DELETE method.

func Get

func Get(w http.ResponseWriter, r *http.Request)

Get gets an article number from the request and returns the corresponding article in the collection.

func GetAll

func GetAll(w http.ResponseWriter, r *http.Request)

GetAll returns the entire articles collection

func HandleArticle added in v1.1.0

func HandleArticle(w http.ResponseWriter, r *http.Request)

func HandleRequests

func HandleRequests()

HandleRequests registers all the routers and starts the server.

func HomePage

func HomePage(w http.ResponseWriter, r *http.Request)

HomePage serves the home page

func Update

func Update(w http.ResponseWriter, r *http.Request)

Update gets an article number from the request and updates the corresponding article in the collection with new data. This is a PUT method.

Types

type Article

type Article struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Description string `json:"desc"`
	Content     string `json:"content"`
}

type Configuration

type Configuration struct {
	HOST   string `json:"host"`
	PORT   int    `json:"port"`
	DBNAME string `json:"dbname"`
	DBSQL  string `json:"dbsql"`
}

func NewConfiguration

func NewConfiguration() (*Configuration, error)

NewConfiguration creates a configuration structure from the YAML file in the user configuration directory.

Directories

Path Synopsis
Tutorial source is https://tutorialedge.net/golang/creating-restful-api-with-golang/
Tutorial source is https://tutorialedge.net/golang/creating-restful-api-with-golang/

Jump to

Keyboard shortcuts

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