server

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: Apache-2.0 Imports: 12 Imported by: 6

README

Obvious Service Framework

A simple library for quickly developing web services. Supports HTTP, HTTPS, AWS Gateway Lambda, and AWS Lambda.

The goal is simple, enable a development of Service APIs - not the scaffolding.

How to Use

go get github.com/go-obvious/server
Example Usage

main.go

package main

import (
    "context"
    "flag"

    "github.com/go-obvious/server"
    "github.com/myapp/hello"
)

func parseFlags() (mode *string, port *uint, domain *string) {
    mode = flag.String("mode", "http", "Mode to run the server in (http or lambda)")
    port = flag.Uint("port", 8080, "Port to run the server on")
    domain = flag.String("domain", "example.com", "Domain for the server")
    flag.Parse()
    return
}

func main() {
    mode, port, domain := parseFlags()

    server.New(
        &server.Config{
            Domain: *domain,
            Port:   *port,
            Mode:   *mode,
        },
        hello.NewService("/"),
    ).Run(context.Background())
}

hello.go service

import (
    "net/http"

    "github.com/go-chi/chi"

    "github.com/go-obvious/pkg/httpapi"
    "github.com/go-obvious/pkg/server"
    "github.com/go-obvious/pkg/server/api"
)

type API struct {
    api.Service
}

func NewService(base string) *API {
    a := &API{}
    a.APIName = "hello"
    a.Mounts = map[string]*chi.Mux{}
    a.Mounts[base] = a.Routes()
    return a
}

func (a *API) Register(app server.Server) error {
    if err := a.Service.Register(app); err != nil {
        return err
    }
    return nil
}

func (a *API) Routes() *chi.Mux {
    r := chi.NewRouter()
    r.Get("/", a.Handler)
    return r
}

type Response struct {
    Message string `json:"message"`
}

func (a *API) Handler(w http.ResponseWriter, r *http.Request) {
    httpapi.Reply(r, w, &Response{Message: "hello"}, http.StatusOK)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API interface {
	Name() string
	Register(app Server) error
}

type Certificate

type Certificate struct {
	Cert string `envconfig:"CERTIFICATE_CERT" default:""`
	Key  string `envconfig:"CERTIFICATE_KEY" default:""`
}

type Config

type Config struct {
	Mode   string `envconfig:"SERVICE_MODE" default:"http"`
	Domain string `envconfig:"SERVICE_DOMAIN" default:""`
	Port   uint   `envconfig:"SERVICE_PORT" default:"4444"`
	*Certificate
}

type Server

type Server interface {
	Config() interface{}
	Router() interface{}
	Run(ctx context.Context)
	Shutdown()
}

func New

func New(
	cfg *Config,
	version *ServerVersion,
	apis ...API,
) Server

type ServerVersion

type ServerVersion = about.ServerVersion

Jump to

Keyboard shortcuts

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