-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Your Name
committed
Jul 4, 2023
1 parent
c3e2a47
commit 7802b53
Showing
13 changed files
with
309 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package api | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
sentryfiber "github.com/aldy505/sentry-fiber" | ||
"github.com/getsentry/sentry-go" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/fiber/v2/middleware/cors" | ||
"github.com/gofiber/fiber/v2/middleware/logger" | ||
"github.com/gofiber/fiber/v2/middleware/recover" | ||
"github.com/pablodz/sopro/internal/api/v2/routes" | ||
) | ||
|
||
func HandleRequest() { | ||
err := sentry.Init(sentry.ClientOptions{ | ||
Dsn: os.Getenv("SENTRY_DSN"), | ||
}) | ||
if err != nil { | ||
log.Fatalln("sentry initialization failed") | ||
} | ||
|
||
log.Printf("Fiber cold start") | ||
app := fiber.New() | ||
|
||
// Middleware: cors | ||
app.Use(cors.New(cors.Config{ | ||
AllowOrigins: "*", | ||
AllowHeaders: "authorization,Content-Type", | ||
AllowMethods: "GET,POST,PUT,DELETE,OPTIONS", | ||
AllowCredentials: true, | ||
})) | ||
|
||
// Middleware: sentry | ||
app.Use(sentryfiber.New(sentryfiber.Options{ | ||
WaitForDelivery: false, // don't block lambda execution | ||
})) | ||
|
||
// Middleare: Logger | ||
app.Use(logger.New(logger.Config{ | ||
Done: func(c *fiber.Ctx, logString []byte) { | ||
if c.Response().StatusCode() != fiber.StatusOK { | ||
sentry.CaptureMessage(string(logString)) | ||
} | ||
}, | ||
})) | ||
|
||
// Middleware: Recover | ||
app.Use(recover.New()) | ||
|
||
app = Router(app) | ||
|
||
log.Fatal(app.Listen(":9898")) | ||
} | ||
|
||
func Router(app *fiber.App) *fiber.App { | ||
routes.HealthRoutes(app) | ||
routes.TranscodingRoutes(app) | ||
return app | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package endpoints | ||
|
||
// Health | ||
const HEALTH = "/health" | ||
|
||
const TRANSCODING = "/api/v2/transcoding" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/pablodz/sopro/internal/api/v2/endpoints" | ||
"github.com/pablodz/sopro/internal/api/v2/services/health" | ||
) | ||
|
||
func HealthRoutes(app *fiber.App) { | ||
app.Get(endpoints.HEALTH, func(c *fiber.Ctx) error { return health.Ping(c) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/pablodz/sopro/internal/api/v2/endpoints" | ||
"github.com/pablodz/sopro/internal/api/v2/services/transcoding" | ||
) | ||
|
||
func TranscodingRoutes(app *fiber.App) { | ||
app.Post(endpoints.TRANSCODING, func(c *fiber.Ctx) error { return transcoding.TranscodeFile(c) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package health | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func Ping(c *fiber.Ctx) error { | ||
log.Println("[Health][Ping]") | ||
return c.SendString("Pong!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package transcoding | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/pablodz/sopro/pkg/audioconfig" | ||
"github.com/pablodz/sopro/pkg/cpuarch" | ||
"github.com/pablodz/sopro/pkg/encoding" | ||
"github.com/pablodz/sopro/pkg/fileformat" | ||
"github.com/pablodz/sopro/pkg/method" | ||
"github.com/pablodz/sopro/pkg/sopro" | ||
"github.com/pablodz/sopro/pkg/transcoder" | ||
) | ||
|
||
func TranscodeFile(c *fiber.Ctx) error { | ||
|
||
// get the uploaded file from the request | ||
file, err := c.FormFile("audio") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// open the uploaded file | ||
in, err := file.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer in.Close() | ||
|
||
// read in as buffer and pass to data | ||
buf := make([]byte, file.Size) | ||
if _, err := in.Read(buf); err != nil { | ||
return err | ||
} | ||
|
||
// convert to bytes buffer | ||
data := bytes.NewBuffer(buf) | ||
|
||
// Create the output file | ||
out, err := os.Create("./internal/samples/output.wav") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer out.Close() | ||
|
||
// create a transcoder | ||
t := &transcoder.Transcoder{ | ||
MethodT: method.BIT_LOOKUP_TABLE, | ||
InConfigs: sopro.AudioConfig{ | ||
Endianness: cpuarch.LITTLE_ENDIAN, | ||
}, | ||
OutConfigs: sopro.AudioConfig{ | ||
Endianness: cpuarch.LITTLE_ENDIAN, | ||
}, | ||
SizeBuffer: 1024, | ||
Verbose: true, | ||
} | ||
|
||
// Transcode the file | ||
err = t.Mulaw2Wav( | ||
&sopro.In{ | ||
Data: data, | ||
AudioFileGeneral: sopro.AudioFileGeneral{ | ||
Format: fileformat.AUDIO_MULAW, | ||
Config: audioconfig.MulawConfig{ | ||
BitDepth: 8, | ||
Channels: 1, | ||
Encoding: encoding.SPACE_LOGARITHMIC, // ulaw is logarithmic | ||
SampleRate: 8000, | ||
}, | ||
}, | ||
}, | ||
&sopro.Out{ | ||
Data: out, | ||
AudioFileGeneral: sopro.AudioFileGeneral{ | ||
Format: fileformat.AUDIO_WAV, | ||
Config: audioconfig.WavConfig{ | ||
BitDepth: 8, | ||
Channels: 1, | ||
Encoding: encoding.SPACE_LOGARITHMIC, | ||
SampleRate: 8000, | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// set the response header to indicate that a file is being returned | ||
c.Set(fiber.HeaderContentType, "audio/mpeg") | ||
c.Set(fiber.HeaderContentDisposition, "attachment; filename=processed-audio.wav") | ||
|
||
// send the processed audio file to the client | ||
if err := c.SendFile(out.Name()); err != nil { | ||
return err | ||
} | ||
|
||
// return nil to indicate success | ||
return c.SendStatus(fiber.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package server | ||
|
||
import "github.com/pablodz/sopro/internal/api" | ||
|
||
func Serve() { | ||
api.HandleRequest() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/pablodz/sopro/internal/server" | ||
|
||
func main() { | ||
server.Serve() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package soprocli | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func TranscodeFile() { | ||
// create a new HTTP client | ||
client := &http.Client{} | ||
|
||
// open the audio file to be uploaded | ||
file, err := os.Open("audio.mp3") | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
// create a new multipart form | ||
body := &bytes.Buffer{} | ||
writer := multipart.NewWriter(body) | ||
|
||
// add the audio file to the form | ||
part, err := writer.CreateFormFile("audio", "audio.mp3") | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
if _, err := io.Copy(part, file); err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
// close the multipart form | ||
if err := writer.Close(); err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
// create a new HTTP request | ||
req, err := http.NewRequest("POST", "http://localhost:3000/process-audio", body) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
req.Header.Set("Content-Type", writer.FormDataContentType()) | ||
|
||
// send the HTTP request and get the response | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
// save the processed audio file to disk | ||
file, err = os.Create("processed-audio.mp3") | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
// read the response body and write it to the processed audio file | ||
if _, err := io.Copy(file, resp.Body); err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println("Audio file processed successfully") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters