Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • fb6-wp11-devops/webservice
  • maha1056/devops-pipeline
  • ludo8147/webservice
  • s52888/webservice
  • masi9606/webservice
  • kibu5600/webservice
  • s78689/webservice
  • s50860/webservice
  • s92604/devops-webservice
  • s76867/webservice-devops
  • s92274/webservice
  • s80066/webservice
  • masa1998/webservice
  • s91190/app-service
  • s84985/webservice
  • gjahn/webservice-ws-2425
  • s75359/webservice
  • ouch4861/webservice-ws-24-oc
  • s92274/webservice-msws-24
  • ewbo4360/webservice
20 results
Show changes
Commits on Source (15)
workflow:
rules:
- if: >-
$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "main"
when: 'always'
- when: 'never'
job_trigger-pipeline:
trigger:
project: 'fb6-wp11-devops/webservice-build-and-publish'
......@@ -37,6 +37,7 @@ $(BIN_DIR)/artifact.bin:
cd $(SRC_DIR) \
&& go build \
-o $(@) \
-ldflags "-X webservice/configuration.version=0.0.1" \
$(SRC_DIR)/*.go
.PHONY: build-linux
......@@ -44,6 +45,7 @@ build-linux: export GOOS := linux
build-linux: export GOARCH := amd64
build-linux: export CGO_ENABLED := 0
build-linux: $(BIN_DIR)/artifact.bin
sha256sum $(BIN_DIR)/artifact.bin
.PHONY: test
......
Webservice
==========
A Go-based simple web service meant to be the subject of any tutorial
or even used the project work.
A Go-based simple web service meant to be the subject of any exercise
or even used in the project work.
#### Prerequisites:
* Go toolchain (install via system package manager or [by hand](https://go.dev/doc/install))
* minimal [required version](./go.mod#L3) of the Go toolchain (install via
system package manager or [by hand](https://go.dev/doc/install))
* [optional] [Redis](https://redis.io/docs/install/) to persist state
......@@ -24,9 +25,11 @@ information checkout the [configuration code](./configuration/config.go).
3. Execute unit tests: `go test -race -v ./...`
4. Build artifact: `go build -o ./artifact.bin ./*.go`
To build for another platform, set `GOOS` and `GOARCH`. To yield a static binary (fully
self-contained, no dynamic linking) set `CGO_ENABLED=0`. For more details, please refer
to the [Makefile](./Makefile).
To build for another platform, set `GOOS` and `GOARCH`. To yield a static
binary (fully self-contained, no dynamic linking) set `CGO_ENABLED=0`.
To set a version during build time, add the following CLI option
`-ldflags "-X webservice/configuration.version=${VERSION}"`.
For more information, please refer to the [Makefile](./Makefile).
#### Run:
......@@ -84,6 +87,13 @@ curl \
http://localhost:8080/state/bar
```
Find out MIME type and size of an entry:
```bash
curl \
-X HEAD \
http://localhost:8080/state/bar
```
Obtain an entry:
```bash
curl \
......@@ -102,8 +112,8 @@ curl \
List all existing entries (returns JSON or plain text, depending on the `Accept` header):
```bash
curl \
-X GET
--header 'Accept: text/plain'\
-X GET \
--header 'Accept: text/plain' \
http://localhost:8080/states
```
......
......@@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"os"
"log/slog"
"unicode"
fp "path/filepath"
configParser "github.com/caarlos0/env/v9"
......@@ -12,9 +14,13 @@ import (
const BODY_SIZE_LIMIT = 32 * 1024 * 1024 // 32 MB, in bytes
var version string = "n/a"
type Config struct {
Version string `env:"VERSION" envDefault:"N/A"`
Version string
FontColor string `env:"FONT_COLOR" envDefault:""`
LogLevel string `env:"LOG_LEVE" envDefault:"error"`
......@@ -31,7 +37,9 @@ type Config struct {
func New() ( *Config, error ){
cfg := Config{}
cfg := Config{
Version: version,
}
if err := configParser.Parse( &cfg ); err != nil {
return nil, err
......@@ -51,17 +59,10 @@ func New() ( *Config, error ){
if cfg.Environment == "development" { cfg.LogLevel = "debug" }
possibleLogLevels := map[ string ] bool {
"error": true,
"debug": true,
}
if _, ok := possibleLogLevels[ cfg.LogLevel ]; !ok {
return nil, errors.New(
fmt.Sprintf( "Invalid log level: %s", cfg.LogLevel ),
)
if _, err := cfg.GetLogLevel(); err != nil {
return nil, err
}
if len( cfg.DatabaseHost ) >= 1 && len( cfg.DatabasePassword ) >= 2 {
if ! fp.IsLocal( cfg.DatabasePassword ) && ! fp.IsAbs( cfg.DatabasePassword ) {
return nil, errors.New(
......@@ -81,6 +82,38 @@ func New() ( *Config, error ){
}
}
if len( cfg.FontColor ) >= 1 {
if len( cfg.FontColor ) >= 21 {
return nil, errors.New(
fmt.Sprintln( "Font color too long" ),
)
}
for _, r := range cfg.FontColor {
if ! unicode.IsLetter( r ) {
return nil, errors.New(
fmt.Sprintln( "Invalid character in font color" ),
)
}
}
}
return &cfg, nil
}
func ( cfg *Config ) GetLogLevel() ( slog.Level, error ){
possibleLogLevels := map[ string ] slog.Level {
"error": slog.LevelError,
"debug": slog.LevelDebug,
}
level, ok := possibleLogLevels[ cfg.LogLevel ]
if !ok {
return slog.LevelError, errors.New(
fmt.Sprintf( "Invalid log level: %s", cfg.LogLevel ),
)
}else{
return level, nil
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"syscall"
......@@ -20,9 +21,22 @@ import (
func main() {
config, err := configuration.New()
if err != nil {
log.Fatalf( "HTTP server failed to start: %v", err )
slog.Error( fmt.Sprintf( "HTTP server failed to start: %v", err ) )
os.Exit( 1 )
}
level, _ := config.GetLogLevel()
slog.SetDefault(
slog.New(
slog.NewTextHandler(
os.Stdout,
&slog.HandlerOptions{
Level: level,
},
),
),
)
server := fiber.New( fiber.Config{
AppName: "webservice",
DisableStartupMessage: config.Environment != "development",
......@@ -40,13 +54,15 @@ func main() {
err = routing.SetRoutes( server, config, store, &isHealthy )
if err != nil {
log.Fatalf( "HTTP server failed to start: %v", err )
slog.Error( fmt.Sprintf( "HTTP server failed to start: %v", err ) )
os.Exit( 1 )
}
go func(){
err := server.Listen( fmt.Sprintf( "%s:%d", config.Host, config.Port ) )
if err != nil {
log.Fatalf( "HTTP server failed to start: %v", err )
slog.Error( fmt.Sprintf( "HTTP server failed to start: %v", err ) )
os.Exit( 1 )
}
}()
......
......@@ -7,7 +7,7 @@ import (
"strings"
"net/http"
"html/template"
"log"
log "log/slog"
"bytes"
"mime"
......@@ -25,13 +25,20 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
return err
}
metricsTextTemplate, err := template.New( "metrics" ).Parse( metricsText )
if err != nil {
return err
}
if config.LogLevel == "debug" {
router.All( "*", func( c *f.Ctx ) error {
log.Printf( "%s %s mime:%s agent:%s",
c.Method(),
c.Path(),
c.Get( f.HeaderContentType ),
c.Get( f.HeaderUserAgent ),
log.Debug(
fmt.Sprintf( "%s %s mime:%s agent:%s",
c.Method(),
c.Path(),
c.Get( f.HeaderContentType, c.Get( f.HeaderAccept, "" ) ),
c.Get( f.HeaderUserAgent ),
),
)
return c.Next()
})
......@@ -48,7 +55,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
data := indexHtmlData{
Version: config.Version,
Color: "",
Color: config.FontColor,
}
buffer := &bytes.Buffer{}
......@@ -90,6 +97,36 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
})
router.Get( "/metrics", func( c *f.Ctx ) error {
headers := c.GetReqHeaders()
acceptHeader := strings.Join( headers[ "Accept" ], " " )
buffer := &bytes.Buffer{}
if strings.Contains( acceptHeader , "json" ) {
// FUTUREWORK: implement https://opentelemetry.io/docs/specs/otlp/#otlphttp
return c.SendStatus( http.StatusNotAcceptable )
} else {
names, err := store.List()
if err != nil {
log.Debug( err.Error() )
return c.SendStatus( http.StatusInternalServerError )
}
data := metricsTextData{
Count: len( names ),
}
err = metricsTextTemplate.Execute( buffer, data )
if err != nil {
return err
}
c.Set( "Content-Type", "text/plain; charset=utf-8" )
return c.Send( buffer.Bytes() )
}
})
router.Get( "/env", func( c *f.Ctx ) error {
c.Type( "txt", "utf-8" )
......@@ -101,6 +138,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
for _, envVar := range os.Environ() {
_, err := c.WriteString( fmt.Sprintln( envVar ) )
if err != nil {
log.Debug( err.Error() )
c.Status( http.StatusInternalServerError )
return err
}
......@@ -114,9 +152,27 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
statePathGroup := router.Group( "/state" )
statePathGroup.Options( "/:name", func( c *f.Ctx ) error {
name := strings.Clone( c.Params( "name" ) )
existingItem, err := store.Fetch( name )
if err != nil {
log.Debug( err.Error() )
return c.SendStatus( http.StatusInternalServerError )
}
if existingItem == nil {
return c.SendStatus( http.StatusNotFound )
}
c.Set( "Allow", "OPTIONS, GET, PUT, DELETE, HEAD" )
return c.SendStatus( http.StatusNoContent )
})
statePathGroup.Get( "/:name", func( c *f.Ctx ) error {
existingItem, err := store.Fetch( c.Params( "name" ) )
if err != nil {
log.Debug( err.Error() )
c.Status( http.StatusInternalServerError )
return c.Send( nil )
}
......@@ -143,6 +199,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
name := strings.Clone( c.Params( "name" ) )
existingItem, err := store.Fetch( name )
if err != nil {
log.Debug( err.Error() )
c.Status( http.StatusInternalServerError )
return c.Send( nil )
}
......@@ -166,6 +223,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
)
if err = store.Add( newItem ); err != nil {
log.Debug( err.Error() )
c.Status( http.StatusInternalServerError )
return c.Send( nil )
}
......@@ -179,6 +237,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
name := strings.Clone( c.Params( "name" ) )
existingItem, err := store.Fetch( name )
if err != nil {
log.Debug( err.Error() )
return c.SendStatus( http.StatusInternalServerError )
}
......@@ -187,6 +246,7 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
}
if err = store.Remove( name ); err != nil {
log.Debug( err.Error() )
return c.SendStatus( http.StatusInternalServerError )
}
......@@ -194,26 +254,40 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
})
statePathGroup.Use( "*", func( c *f.Ctx ) error {
if method := c.Method(); method == "OPTIONS" {
c.Set( "Allow", "GET, PUT, DELETE, OPTIONS" )
return c.SendStatus( http.StatusNoContent )
statePathGroup.Head( "/:name", func( c *f.Ctx ) error {
name := strings.Clone( c.Params( "name" ) )
existingItem, err := store.Fetch( name )
if err != nil {
log.Debug( err.Error() )
return c.SendStatus( http.StatusInternalServerError )
}
if existingItem == nil {
return c.SendStatus( http.StatusNotFound )
}
c.Set( "Content-Type", existingItem.MimeType() )
c.Set( "Content-Length", fmt.Sprintf( "%d", len( existingItem.Data() ) ) )
return c.SendStatus( http.StatusOK )
})
statePathGroup.Use( "*", func( c *f.Ctx ) error {
return c.SendStatus( http.StatusNotFound )
})
router.Get( "/states", func( c *f.Ctx ) error {
states, err := store.Show()
names, err := store.List()
if err != nil {
log.Debug( err.Error() )
return c.SendStatus( http.StatusInternalServerError )
}
const pathPrefix string = "/state"
paths := make ( []string, len( states ) )
for i, state := range states {
paths[ i ] = fmt.Sprintf( "%s/%s", pathPrefix, state )
paths := make( []string, len( names ) )
for i, name := range names {
paths[ i ] = fmt.Sprintf( "%s/%s", pathPrefix, name )
}
headers := c.GetReqHeaders()
......
......@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"math/rand"
......@@ -167,7 +168,7 @@ func TestState( t *testing.T ){
const statePath2 = "/state/another-test"
const statePath2Mime = "application/octet-stream"
const statePath2BodySize = 64
const statePath2BodySize = 128
statePath2Body := generateRandomBytes( statePath2BodySize )
req := ht.NewRequest( "GET", statePath1, nil )
......@@ -211,6 +212,15 @@ func TestState( t *testing.T ){
res, _ = router.Test( req, -1 )
assert.Equal( t, http.StatusCreated, res.StatusCode )
req = ht.NewRequest( "HEAD", statePath2, nil )
res, _ = router.Test( req, -1 )
contentLength, err := strconv.ParseInt( res.Header[ "Content-Length" ][0], 10, 64 )
assert.Nil( t, err )
assert.Equal( t, http.StatusOK, res.StatusCode )
assert.Equal( t, statePath2Mime, res.Header[ "Content-Type" ][0] )
assert.Equal( t, int64( statePath2BodySize ), contentLength )
assert.IsType( t, res.Body, http.NoBody )
req = ht.NewRequest( "GET", statePath2, nil )
res, _ = router.Test( req, -1 )
bodyBytes, err := io.ReadAll( res.Body )
......
......@@ -20,3 +20,14 @@ type indexHtmlData struct {
Version string
Color string
}
const metricsText = `
# HELP state_entries_quantity The current number of state entries being stored
# TYPE state_entries_quantity gauge
state_entries_quantity {{ .Count }}
`
type metricsTextData struct {
Count int
}
......@@ -64,15 +64,15 @@ func ( e *Ephemeral ) Fetch( name string ) ( *Item, error ) {
}
func ( e *Ephemeral ) Show() ( []string, error ) {
func ( e *Ephemeral ) List() ( []string, error ) {
if e.store == nil {
return nil, errors.New( "ephemeral storage not available" )
}
e.mux.Lock()
names := make( []string, 0, len( e.store ) )
for k := range e.store {
names = append( names, k )
for _, item := range e.store {
names = append( names, item.Name() )
}
e.mux.Unlock()
......
package state
import (
"testing"
"mime"
"sync"
"github.com/stretchr/testify/assert"
)
var testItems = [] Item {
NewItem( "foo", "bar", []byte( "fasel" ) ),
NewItem( "qwertyASDFGH", mime.TypeByExtension( ".html" ), []byte{} ),
Item{
name: "Som!_🎵nam3",
mimeType: "any kind of string",
data: []byte{ 1, 2, 3, 4, 5, 6, 7, 8 },
},
}
func TestEphemeralAdd( t *testing.T ){
es := NewEphemeralStore()
wg := &sync.WaitGroup{}
for _, item := range testItems {
wg.Add( 1 )
go func( i Item ){
defer wg.Done()
es.Add( i )
}( item )
}
wg.Wait()
assert.Len( t, es.store, len( testItems ) )
}
......@@ -5,6 +5,8 @@ import (
"runtime"
"context"
"time"
"os"
log "log/slog"
"webservice/configuration"
......@@ -21,11 +23,18 @@ type Persistent struct {
func NewPersistentStore( c *configuration.Config ) *Persistent {
content, err := os.ReadFile( c.DatabasePassword )
if err != nil {
log.Error( fmt.Sprintf( "Database password not able to be read: %v", err ) )
os.Exit( 1 )
}
dbPassword := string( content )
return &Persistent{
client: db.NewClient( &db.Options{
Addr: fmt.Sprintf( "%s:%d", c.DatabaseHost, c.DatabasePort ),
Username: c.DatabaseUsername,
Password: c.DatabasePassword,
Password: dbPassword,
DB: c.DatabaseName,
DialTimeout: time.Second * 3,
......@@ -51,8 +60,8 @@ func ( e *Persistent ) Add( i Item ) error {
name := i.Name()
if err := e.client.HSet(
ctx, name,
"data", i.Data(),
"mime", i.MimeType(),
"data", i.Data(),
).Err(); err != nil {
return err
}
......@@ -89,7 +98,7 @@ func ( e *Persistent ) Fetch( name string ) ( *Item, error ) {
}
func ( e *Persistent ) Show() ( []string, error ) {
func ( e *Persistent ) List() ( []string, error ) {
ctx, cancel := context.WithTimeout( context.TODO(), e.timeout )
defer cancel()
......
......@@ -6,7 +6,7 @@ type Store interface {
Add( i Item ) error
Remove( name string ) error
Fetch( name string ) ( *Item, error )
Show() ( []string, error )
List() ( []string, error )
Disconnect() error
}