Procyon|Docs

Quick Start

Create and run a small Procyon HTTP application.

Introduction

Procyon is an application framework for Go. In this guide, you create a small HTTP controller, register it as a component, map a route, and let the runtime start the application.

If the package is not installed yet, add it first:

Terminal
go mod init example.com/hello
go get -u codnect.io/procyon/...

Create a controller

Create a component that owns the endpoint behavior:

hello.go
package main

import (
    "codnect.io/procyon/component"
    "codnect.io/procyon/http"
)

type HelloController struct{}

func NewHelloController() *HelloController {
    return &HelloController{}
}

func (h *HelloController) ConfigureEndpoints(endpoints http.Endpoints) {
    endpoints.MapGet("/hello", http.Handle(h.sayHello))
}

func (h *HelloController) sayHello(ctx *http.Context) error {
    _, err := ctx.Response().Writer().Write([]byte("Hello, World!"))
    return err
}

func init() {
    component.Register(NewHelloController)
}

ConfigureEndpoints is called by the HTTP runtime. The controller maps GET /hello to sayHello, and component.Register makes the controller part of the application component graph.

Start the application

Create a small entrypoint and hand startup to Procyon:

main.go
package main

import (
    "os"

    "codnect.io/procyon"
)

func main() {
    if err := procyon.Run(); err != nil {
        os.Exit(1)
    }
}

procyon.Run() prepares configuration, creates the runtime context, loads registered components, maps endpoints, and keeps the process running when a server is available.

Run it

Run the application from your module:

Terminal
go run .

Then request the endpoint:

Terminal
curl http://localhost:8080/hello

What happened?

The quick start uses the same pieces as a larger application:

  • component.Register registers constructor-based components.
  • http.EndpointConfigurer lets a component contribute routes.
  • http.Endpoints maps paths and methods to handlers.
  • http.Context gives the handler access to the request and response.
  • procyon.Run coordinates startup and shutdown.

Continue with What is Procyon? when you want the bigger runtime model, or HTTP docs when you want to build more routes.